From 2cb60abfecbad78046b0afde156c72a9ae766cf0 Mon Sep 17 00:00:00 2001 From: Fabien Jakimowicz Date: Sun, 24 May 2009 02:32:04 +0200 Subject: Add an model_subclass generator. This generator creates a new model as a subclass of an existing model and the unit test for that model. Lets users avoid having to manually delete the fixtures and migration or remember to pass those arguments. [#2702 state:committed] Signed-off-by: Michael Koziarski --- .../generators/components/model_subclass/USAGE | 13 +++++++++ .../model_subclass/model_subclass_generator.rb | 32 ++++++++++++++++++++++ .../components/model_subclass/templates/model.rb | 3 ++ .../model_subclass/templates/unit_test.rb | 8 ++++++ 4 files changed, 56 insertions(+) create mode 100644 railties/lib/rails_generator/generators/components/model_subclass/USAGE create mode 100644 railties/lib/rails_generator/generators/components/model_subclass/model_subclass_generator.rb create mode 100644 railties/lib/rails_generator/generators/components/model_subclass/templates/model.rb create mode 100644 railties/lib/rails_generator/generators/components/model_subclass/templates/unit_test.rb (limited to 'railties/lib') 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 -- cgit v1.2.3 From 746f3860c197d351ab8f2c860d857b139ce8cbf8 Mon Sep 17 00:00:00 2001 From: anupom syam Date: Wed, 27 May 2009 14:45:16 -0500 Subject: server command help inconsistency fix [#2685 state:resolved] Signed-off-by: Joshua Peek --- railties/lib/commands/server.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'railties/lib') diff --git a/railties/lib/commands/server.rb b/railties/lib/commands/server.rb index 91ac7752ef..1b6505f2dc 100644 --- a/railties/lib/commands/server.rb +++ b/railties/lib/commands/server.rb @@ -17,22 +17,22 @@ options = { :config => RAILS_ROOT + "/config.ru", :detach => false, :debugger => false, - :path => nil + :path => '/' } 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 "" -- cgit v1.2.3 From 0d9e904da35b41c8796b026d63675d4733505c91 Mon Sep 17 00:00:00 2001 From: calavera Date: Wed, 27 May 2009 14:56:14 -0500 Subject: ensure initialize_database_middleware doesn't use ActionController if action_controller framework is not enabled [#2680 state:resolved] Signed-off-by: Joshua Peek --- railties/lib/initializer.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'railties/lib') diff --git a/railties/lib/initializer.rb b/railties/lib/initializer.rb index 3c0d5940ea..7ae766f913 100644 --- a/railties/lib/initializer.rb +++ b/railties/lib/initializer.rb @@ -445,7 +445,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 == ActiveRecord::SessionStore configuration.middleware.insert_before :"ActiveRecord::SessionStore", ActiveRecord::ConnectionAdapters::ConnectionManagement configuration.middleware.insert_before :"ActiveRecord::SessionStore", ActiveRecord::QueryCache else @@ -886,7 +887,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 +1130,4 @@ class Rails::OrderedOptions < Array #:nodoc: return false end end + -- cgit v1.2.3 From d17fb9dc766936efc721fc3e55ef9289902dc34c Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Thu, 28 May 2009 09:16:29 -0500 Subject: Fix script/server's default mount path [#2731 state:resolved] --- railties/lib/commands/server.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'railties/lib') diff --git a/railties/lib/commands/server.rb b/railties/lib/commands/server.rb index 1b6505f2dc..01dd33fa8c 100644 --- a/railties/lib/commands/server.rb +++ b/railties/lib/commands/server.rb @@ -17,7 +17,7 @@ options = { :config => RAILS_ROOT + "/config.ru", :detach => false, :debugger => false, - :path => '/' + :path => nil } ARGV.clone.options do |opts| -- cgit v1.2.3 From cf3ccd7be0caae67dfddf5bc5056dcfdaab6f369 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Thu, 28 May 2009 15:54:13 -0500 Subject: Ruby 1.9: use UTF-8 for default internal and external encodings. --- railties/lib/initializer.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'railties/lib') diff --git a/railties/lib/initializer.rb b/railties/lib/initializer.rb index 7ae766f913..bbaa313fae 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 :active_record -- cgit v1.2.3 From 54984f0f245cd67999c23112dfa841a4d0bd66f2 Mon Sep 17 00:00:00 2001 From: Luke Melia Date: Sat, 30 May 2009 09:34:57 -0500 Subject: Avoid loading the ActiveRecord::SessionStore class on initialization if it is not in use [#2737 state:resolved] Signed-off-by: Joshua Peek --- railties/lib/initializer.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'railties/lib') diff --git a/railties/lib/initializer.rb b/railties/lib/initializer.rb index bbaa313fae..7273cea0c5 100644 --- a/railties/lib/initializer.rb +++ b/railties/lib/initializer.rb @@ -450,7 +450,7 @@ Run `rake gems:install` to install the missing gems. def initialize_database_middleware if configuration.frameworks.include?(:active_record) if configuration.frameworks.include?(:action_controller) && - ActionController::Base.session_store == ActiveRecord::SessionStore + 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 -- cgit v1.2.3 From 64756e8f718c90a17664f2d35993696d8bf0f81e Mon Sep 17 00:00:00 2001 From: Vladimir Dobriakov Date: Mon, 24 Nov 2008 14:17:39 +0100 Subject: better db:create for mysql - do not assume root user Signed-off-by: Michael Koziarski [#1459 state:committed] --- railties/lib/tasks/databases.rake | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) (limited to 'railties/lib') diff --git a/railties/lib/tasks/databases.rake b/railties/lib/tasks/databases.rake index cdab5d8bb0..f572e914e1 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' -- cgit v1.2.3 From b3839f1c98ab6824091c517f6e2e6e3a024c3e13 Mon Sep 17 00:00:00 2001 From: Jeff Dean Date: Fri, 22 May 2009 00:32:32 -0400 Subject: Updated the db:structure:dump task to properly format the pgdump command when you have multiple schemas in your schema search path. Signed-off-by: Michael Koziarski [#2695 state:committed] --- railties/lib/tasks/databases.rake | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'railties/lib') diff --git a/railties/lib/tasks/databases.rake b/railties/lib/tasks/databases.rake index f572e914e1..0e256737f9 100644 --- a/railties/lib/tasks/databases.rake +++ b/railties/lib/tasks/databases.rake @@ -298,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" -- cgit v1.2.3