diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-10-23 21:30:26 -0200 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-10-23 21:34:07 -0200 |
commit | ff044c3c3a0a85d26d7c000abeeed835a3bee0bf (patch) | |
tree | b9a5c8a0f3caf46238b045c0fb5c93e7fb4136e7 | |
parent | f0323288da939f57641b82041cb1a86a1e526746 (diff) | |
download | rails-ff044c3c3a0a85d26d7c000abeeed835a3bee0bf.tar.gz rails-ff044c3c3a0a85d26d7c000abeeed835a3bee0bf.tar.bz2 rails-ff044c3c3a0a85d26d7c000abeeed835a3bee0bf.zip |
Use thor class_option to make the primary_key_type option work
Also move the method to the right class
6 files changed, 20 insertions, 16 deletions
diff --git a/activerecord/lib/rails/generators/active_record.rb b/activerecord/lib/rails/generators/active_record.rb index 425512cc43..dc29213235 100644 --- a/activerecord/lib/rails/generators/active_record.rb +++ b/activerecord/lib/rails/generators/active_record.rb @@ -12,14 +12,6 @@ module ActiveRecord def self.base_root File.dirname(__FILE__) end - - private - def primary_key_type - key_type = Rails::Generators.options[:active_record][:primary_key_type] - ", id: :#{key_type}" if key_type - end - - end end end diff --git a/activerecord/lib/rails/generators/active_record/migration.rb b/activerecord/lib/rails/generators/active_record/migration.rb index b7418cf42f..c2b2209638 100644 --- a/activerecord/lib/rails/generators/active_record/migration.rb +++ b/activerecord/lib/rails/generators/active_record/migration.rb @@ -13,6 +13,13 @@ module ActiveRecord ActiveRecord::Migration.next_migration_number(next_migration_number) end end + + private + + def primary_key_type + key_type = options[:primary_key_type] + ", id: :#{key_type}" if key_type + end end end end diff --git a/activerecord/lib/rails/generators/active_record/migration/migration_generator.rb b/activerecord/lib/rails/generators/active_record/migration/migration_generator.rb index 0d57de4d65..4e5872b585 100644 --- a/activerecord/lib/rails/generators/active_record/migration/migration_generator.rb +++ b/activerecord/lib/rails/generators/active_record/migration/migration_generator.rb @@ -5,6 +5,8 @@ module ActiveRecord class MigrationGenerator < Base # :nodoc: argument :attributes, :type => :array, :default => [], :banner => "field[:type][:index] field[:type][:index]" + class_option :primary_key_type, type: :string, desc: "The type for primary key" + def create_migration_file set_local_assigns! validate_file_name! diff --git a/activerecord/lib/rails/generators/active_record/model/model_generator.rb b/activerecord/lib/rails/generators/active_record/model/model_generator.rb index 7e8d68ce69..156f77e5eb 100644 --- a/activerecord/lib/rails/generators/active_record/model/model_generator.rb +++ b/activerecord/lib/rails/generators/active_record/model/model_generator.rb @@ -11,10 +11,9 @@ module ActiveRecord class_option :timestamps, :type => :boolean class_option :parent, :type => :string, :desc => "The parent class for the generated model" class_option :indexes, :type => :boolean, :default => true, :desc => "Add indexes for references and belongs_to columns" + class_option :primary_key_type, type: :string, desc: "The type for primary key" - # creates the migration file for the model. - def create_migration_file return unless options[:migration] && options[:parent].nil? attributes.each { |a| a.attr_options.delete(:index) if a.reference? && !a.has_index? } if options[:indexes] == false diff --git a/railties/test/generators/migration_generator_test.rb b/railties/test/generators/migration_generator_test.rb index 832bf8d2d4..199743a396 100644 --- a/railties/test/generators/migration_generator_test.rb +++ b/railties/test/generators/migration_generator_test.rb @@ -222,17 +222,12 @@ class MigrationGeneratorTest < Rails::Generators::TestCase end def test_add_uuid_to_create_table_migration - previous_value = Rails.application.config.generators.active_record[:primary_key_type] - Rails.application.config.generators.active_record[:primary_key_type] = :uuid - - run_generator ["create_books"] + run_generator ["create_books", "--primary_key_type=uuid"] assert_migration "db/migrate/create_books.rb" do |content| assert_method :change, content do |change| assert_match(/create_table :books, id: :uuid/, change) end end - - Rails.application.config.generators.active_record[:primary_key_type] = previous_value end def test_should_create_empty_migrations_if_name_not_start_with_add_or_remove_or_create diff --git a/railties/test/generators/model_generator_test.rb b/railties/test/generators/model_generator_test.rb index abd3ff50a4..64b9a480f3 100644 --- a/railties/test/generators/model_generator_test.rb +++ b/railties/test/generators/model_generator_test.rb @@ -374,6 +374,15 @@ class ModelGeneratorTest < Rails::Generators::TestCase end end + def test_add_uuid_to_create_table_migration + run_generator ["account", "--primary_key_type=uuid"] + assert_migration "db/migrate/create_accounts.rb" do |content| + assert_method :change, content do |change| + assert_match(/create_table :accounts, id: :uuid/, change) + end + end + end + def test_required_belongs_to_adds_required_association run_generator ["account", "supplier:references{required}"] |