aboutsummaryrefslogtreecommitdiffstats
path: root/railties
diff options
context:
space:
mode:
Diffstat (limited to 'railties')
-rw-r--r--railties/CHANGELOG2
-rw-r--r--railties/lib/commands/server.rb8
-rw-r--r--railties/lib/initializer.rb16
-rw-r--r--railties/lib/rails_generator/generators/components/model_subclass/USAGE13
-rw-r--r--railties/lib/rails_generator/generators/components/model_subclass/model_subclass_generator.rb32
-rw-r--r--railties/lib/rails_generator/generators/components/model_subclass/templates/model.rb3
-rw-r--r--railties/lib/rails_generator/generators/components/model_subclass/templates/unit_test.rb8
-rw-r--r--railties/lib/tasks/databases.rake26
-rw-r--r--railties/test/generators/rails_model_subclass_generator_test.rb15
-rw-r--r--railties/test/initializer_test.rb24
10 files changed, 130 insertions, 17 deletions
diff --git a/railties/CHANGELOG b/railties/CHANGELOG
index 8e7dfb38cc..782afd5aa4 100644
--- a/railties/CHANGELOG
+++ b/railties/CHANGELOG
@@ -1,5 +1,7 @@
*Edge*
+* Ruby 1.9: use UTF-8 for default internal and external encodings. [Jeremy Kemper]
+
* Added db/seeds.rb as a default file for storing seed data for the database. Can be loaded with rake db:seed (or created alongside the db with db:setup). (This is also known as the "Stop Putting Gawd Damn Seed Data In Your Migrations" feature) [DHH]
diff --git a/railties/lib/commands/server.rb b/railties/lib/commands/server.rb
index 91ac7752ef..01dd33fa8c 100644
--- a/railties/lib/commands/server.rb
+++ b/railties/lib/commands/server.rb
@@ -22,17 +22,17 @@ options = {
ARGV.clone.options do |opts|
opts.on("-p", "--port=port", Integer,
- "Runs Rails on the specified port.", "Default: 3000") { |v| options[:Port] = v }
+ "Runs Rails on the specified port.", "Default: #{options[:Port]}") { |v| options[:Port] = v }
opts.on("-b", "--binding=ip", String,
- "Binds Rails to the specified ip.", "Default: 0.0.0.0") { |v| options[:Host] = v }
+ "Binds Rails to the specified ip.", "Default: #{options[:Host]}") { |v| options[:Host] = v }
opts.on("-c", "--config=file", String,
"Use custom rackup configuration file") { |v| options[:config] = v }
opts.on("-d", "--daemon", "Make server run as a Daemon.") { options[:detach] = true }
opts.on("-u", "--debugger", "Enable ruby-debugging for the server.") { options[:debugger] = true }
opts.on("-e", "--environment=name", String,
"Specifies the environment to run this server under (test/development/production).",
- "Default: development") { |v| options[:environment] = v }
- opts.on("-P", "--path=/path", String, "Runs Rails app mounted at a specific path.", "Default: /") { |v| options[:path] = v }
+ "Default: #{options[:environment]}") { |v| options[:environment] = v }
+ opts.on("-P", "--path=/path", String, "Runs Rails app mounted at a specific path.", "Default: #{options[:path]}") { |v| options[:path] = v }
opts.separator ""
diff --git a/railties/lib/initializer.rb b/railties/lib/initializer.rb
index 3c0d5940ea..7273cea0c5 100644
--- a/railties/lib/initializer.rb
+++ b/railties/lib/initializer.rb
@@ -426,10 +426,14 @@ Run `rake gems:install` to install the missing gems.
# should override this behaviour and set the relevant +default_charset+
# on ActionController::Base.
#
- # For Ruby 1.9, this does nothing. Specify the default encoding in the Ruby
- # shebang line if you don't want UTF-8.
+ # For Ruby 1.9, UTF-8 is the default internal and external encoding.
def initialize_encoding
- $KCODE='u' if RUBY_VERSION < '1.9'
+ if RUBY_VERSION < '1.9'
+ $KCODE='u'
+ else
+ Encoding.default_internal = Encoding::UTF_8
+ Encoding.default_external = Encoding::UTF_8
+ end
end
# This initialization routine does nothing unless <tt>:active_record</tt>
@@ -445,7 +449,8 @@ Run `rake gems:install` to install the missing gems.
def initialize_database_middleware
if configuration.frameworks.include?(:active_record)
- if ActionController::Base.session_store == ActiveRecord::SessionStore
+ if configuration.frameworks.include?(:action_controller) &&
+ ActionController::Base.session_store.name == 'ActiveRecord::SessionStore'
configuration.middleware.insert_before :"ActiveRecord::SessionStore", ActiveRecord::ConnectionAdapters::ConnectionManagement
configuration.middleware.insert_before :"ActiveRecord::SessionStore", ActiveRecord::QueryCache
else
@@ -886,7 +891,7 @@ Run `rake gems:install` to install the missing gems.
# Enable threaded mode. Allows concurrent requests to controller actions and
# multiple database connections. Also disables automatic dependency loading
- # after boot, and disables reloading code on every request, as these are
+ # after boot, and disables reloading code on every request, as these are
# fundamentally incompatible with thread safety.
def threadsafe!
self.preload_frameworks = true
@@ -1129,3 +1134,4 @@ class Rails::OrderedOptions < Array #:nodoc:
return false
end
end
+
diff --git a/railties/lib/rails_generator/generators/components/model_subclass/USAGE b/railties/lib/rails_generator/generators/components/model_subclass/USAGE
new file mode 100644
index 0000000000..a4b558a401
--- /dev/null
+++ b/railties/lib/rails_generator/generators/components/model_subclass/USAGE
@@ -0,0 +1,13 @@
+Description:
+ Create a model subclass of parent, used for Single Table Inheritance.
+
+ Both subclass and parent name can be either CamelCased or under_scored.
+
+ This generates a model class in app/models and a unit test in test/unit.
+
+Examples:
+ `./script/generate model_subclass admin user`
+
+ creates an Admin model, which will inheritate from User model, test:
+ Model: app/models/admin.rb
+ Test: test/unit/admin_test.rb
diff --git a/railties/lib/rails_generator/generators/components/model_subclass/model_subclass_generator.rb b/railties/lib/rails_generator/generators/components/model_subclass/model_subclass_generator.rb
new file mode 100644
index 0000000000..e8ac3da2cd
--- /dev/null
+++ b/railties/lib/rails_generator/generators/components/model_subclass/model_subclass_generator.rb
@@ -0,0 +1,32 @@
+class ModelSubclassGenerator < Rails::Generator::NamedBase
+ default_options :skip_unit_test => false
+
+ def manifest
+ record do |m|
+ # Check for class naming collisions.
+ m.class_collisions class_name, "#{class_name}Test"
+
+ # Model and test directories.
+ m.directory File.join('app/models', class_path)
+ m.directory File.join('test/unit', class_path)
+
+ # Model class and unit test
+ m.template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb"), :assigns => assigns
+ m.template 'unit_test.rb', File.join('test/unit', class_path, "#{file_name}_test.rb"), :assigns => assigns
+
+ end
+ end
+
+ protected
+ def banner
+ "Usage: #{$0} #{spec.name} Subclass Parent"
+ end
+
+ def assigns
+ {:parent_class_name => parent_class_name}
+ end
+
+ def parent_class_name
+ @args.first.try(:camelize) || usage
+ end
+end
diff --git a/railties/lib/rails_generator/generators/components/model_subclass/templates/model.rb b/railties/lib/rails_generator/generators/components/model_subclass/templates/model.rb
new file mode 100644
index 0000000000..d0037b322b
--- /dev/null
+++ b/railties/lib/rails_generator/generators/components/model_subclass/templates/model.rb
@@ -0,0 +1,3 @@
+class <%= class_name %> < <%= parent_class_name %>
+
+end \ No newline at end of file
diff --git a/railties/lib/rails_generator/generators/components/model_subclass/templates/unit_test.rb b/railties/lib/rails_generator/generators/components/model_subclass/templates/unit_test.rb
new file mode 100644
index 0000000000..3e0bc29d3a
--- /dev/null
+++ b/railties/lib/rails_generator/generators/components/model_subclass/templates/unit_test.rb
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class <%= class_name %>Test < ActiveSupport::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end
diff --git a/railties/lib/tasks/databases.rake b/railties/lib/tasks/databases.rake
index cdab5d8bb0..0e256737f9 100644
--- a/railties/lib/tasks/databases.rake
+++ b/railties/lib/tasks/databases.rake
@@ -56,12 +56,28 @@ namespace :db do
when 'mysql'
@charset = ENV['CHARSET'] || 'utf8'
@collation = ENV['COLLATION'] || 'utf8_general_ci'
+ creation_options = {:charset => (config['charset'] || @charset), :collation => (config['collation'] || @collation)}
begin
ActiveRecord::Base.establish_connection(config.merge('database' => nil))
- ActiveRecord::Base.connection.create_database(config['database'], :charset => (config['charset'] || @charset), :collation => (config['collation'] || @collation))
+ ActiveRecord::Base.connection.create_database(config['database'], creation_options)
ActiveRecord::Base.establish_connection(config)
- rescue
- $stderr.puts "Couldn't create database for #{config.inspect}, charset: #{config['charset'] || @charset}, collation: #{config['collation'] || @collation} (if you set the charset manually, make sure you have a matching collation)"
+ rescue Mysql::Error => sqlerr
+ if sqlerr.errno == Mysql::Error::ER_ACCESS_DENIED_ERROR
+ print "#{sqlerr.error}. \nPlease provide the root password for your mysql installation\n>"
+ root_password = $stdin.gets.strip
+ grant_statement = "GRANT ALL PRIVILEGES ON #{config['database']}.* " \
+ "TO '#{config['username']}'@'localhost' " \
+ "IDENTIFIED BY '#{config['password']}' WITH GRANT OPTION;"
+ ActiveRecord::Base.establish_connection(config.merge(
+ 'database' => nil, 'username' => 'root', 'password' => root_password))
+ ActiveRecord::Base.connection.create_database(config['database'], creation_options)
+ ActiveRecord::Base.connection.execute grant_statement
+ ActiveRecord::Base.establish_connection(config)
+ else
+ $stderr.puts sqlerr.error
+ $stderr.puts "Couldn't create database for #{config.inspect}, charset: #{config['charset'] || @charset}, collation: #{config['collation'] || @collation}"
+ $stderr.puts "(if you set the charset manually, make sure you have a matching collation)" if config['charset']
+ end
end
when 'postgresql'
@encoding = config[:encoding] || ENV['CHARSET'] || 'utf8'
@@ -282,7 +298,9 @@ namespace :db do
ENV['PGPORT'] = abcs[RAILS_ENV]["port"].to_s if abcs[RAILS_ENV]["port"]
ENV['PGPASSWORD'] = abcs[RAILS_ENV]["password"].to_s if abcs[RAILS_ENV]["password"]
search_path = abcs[RAILS_ENV]["schema_search_path"]
- search_path = "--schema=#{search_path}" if search_path
+ unless search_path.blank?
+ search_path = search_path.split(",").map{|search_path| "--schema=#{search_path.strip}" }.join(" ")
+ end
`pg_dump -i -U "#{abcs[RAILS_ENV]["username"]}" -s -x -O -f db/#{RAILS_ENV}_structure.sql #{search_path} #{abcs[RAILS_ENV]["database"]}`
raise "Error dumping database" if $?.exitstatus == 1
when "sqlite", "sqlite3"
diff --git a/railties/test/generators/rails_model_subclass_generator_test.rb b/railties/test/generators/rails_model_subclass_generator_test.rb
new file mode 100644
index 0000000000..30066b5a3c
--- /dev/null
+++ b/railties/test/generators/rails_model_subclass_generator_test.rb
@@ -0,0 +1,15 @@
+require 'generators/generator_test_helper'
+
+class RailsModelSubclassGeneratorTest < GeneratorTestCase
+
+ def test_model_subclass_generates_resources
+ run_generator('model_subclass', %w(Car Product))
+
+ assert_generated_model_for :car, "Product"
+ assert_generated_unit_test_for :car
+ end
+
+ def test_model_subclass_must_have_a_parent_class_name
+ assert_raise(Rails::Generator::UsageError) { run_generator('model_subclass', %w(Car)) }
+ end
+end \ No newline at end of file
diff --git a/railties/test/initializer_test.rb b/railties/test/initializer_test.rb
index d77a045e56..987a5ada86 100644
--- a/railties/test/initializer_test.rb
+++ b/railties/test/initializer_test.rb
@@ -309,9 +309,10 @@ class InitializerSetupI18nTests < Test::Unit::TestCase
config.i18n.load_path << "my/other/locale.yml"
Rails::Initializer.run(:initialize_i18n, config)
- assert_equal [
+ assert_equal [
File.expand_path(File.dirname(__FILE__) + "/../../activesupport/lib/active_support/locale/en.yml"),
File.expand_path(File.dirname(__FILE__) + "/../../actionpack/lib/action_view/locale/en.yml"),
+ File.expand_path(File.dirname(__FILE__) + "/../../activemodel/lib/active_model/locale/en.yml"),
File.expand_path(File.dirname(__FILE__) + "/../../activerecord/lib/active_record/locale/en.yml"),
"my/test/locale.yml",
"my/other/locale.yml" ], I18n.load_path.collect { |path| path =~ /^\./ ? File.expand_path(path) : path }
@@ -363,17 +364,31 @@ class InitializerDatabaseMiddlewareTest < Test::Unit::TestCase
ensure
ActionController::Base.session_store = store
end
+
+ def test_ensure_database_middleware_doesnt_use_action_controller_on_initializing
+ @config.frameworks -= [:action_controller]
+ store = ActionController::Base.session_store
+ ActionController::Base.session_store = ActiveRecord::SessionStore
+
+ @config.middleware.expects(:use).with(ActiveRecord::ConnectionAdapters::ConnectionManagement)
+ @config.middleware.expects(:use).with(ActiveRecord::QueryCache)
+
+ Rails::Initializer.run(:initialize_database_middleware, @config)
+ ensure
+ ActionController::Base.session_store = store
+ @config.frameworks += [:action_controller]
+ end
end
class InitializerViewPathsTest < Test::Unit::TestCase
def setup
@config = Rails::Configuration.new
@config.frameworks = [:action_view, :action_controller, :action_mailer]
-
+
ActionController::Base.stubs(:view_paths).returns(stub)
ActionMailer::Base.stubs(:view_paths).returns(stub)
end
-
+
def test_load_view_paths_doesnt_perform_anything_when_action_view_not_in_frameworks
@config.frameworks -= [:action_view]
ActionController::Base.view_paths.expects(:load!).never
@@ -390,4 +405,5 @@ class RailsRootTest < Test::Unit::TestCase
def test_rails_dot_root_should_be_a_pathname
assert_equal File.join(RAILS_ROOT, 'app', 'controllers'), Rails.root.join('app', 'controllers').to_s
end
-end \ No newline at end of file
+end
+