From f0323288da939f57641b82041cb1a86a1e526746 Mon Sep 17 00:00:00 2001 From: Jon McCartie Date: Thu, 22 Oct 2015 09:11:42 -0700 Subject: [PATCH] Move default uuid generation to active_record --- activerecord/CHANGELOG.md | 5 +++-- activerecord/lib/rails/generators/active_record.rb | 8 ++++++++ .../migration/templates/create_table_migration.rb | 2 +- railties/lib/rails/generators/migration.rb | 5 ----- railties/test/generators/migration_generator_test.rb | 7 ++++--- 5 files changed, 16 insertions(+), 11 deletions(-) diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index e1f543c3a1..48005eee1a 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -7,8 +7,9 @@ * Add ability to default to `uuid` as primary key when generating database migrations - Set `Rails.application.config.active_record.primary_key = :uuid` - or `config.active_record.primary_key = :uuid` in config/application.rb + config.generators do |g| + g.orm :active_record, primary_key_type: :uuid + end *Jon McCartie* diff --git a/activerecord/lib/rails/generators/active_record.rb b/activerecord/lib/rails/generators/active_record.rb index dc29213235..425512cc43 100644 --- a/activerecord/lib/rails/generators/active_record.rb +++ b/activerecord/lib/rails/generators/active_record.rb @@ -12,6 +12,14 @@ 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/templates/create_table_migration.rb b/activerecord/lib/rails/generators/active_record/migration/templates/create_table_migration.rb index 4a7deb3c75..fadab2a1e6 100644 --- a/activerecord/lib/rails/generators/active_record/migration/templates/create_table_migration.rb +++ b/activerecord/lib/rails/generators/active_record/migration/templates/create_table_migration.rb @@ -1,6 +1,6 @@ class <%= migration_class_name %> < ActiveRecord::Migration def change - create_table :<%= table_name %><%= id_kind %> do |t| + create_table :<%= table_name %><%= primary_key_type %> do |t| <% attributes.each do |attribute| -%> <% if attribute.password_digest? -%> t.string :password_digest<%= attribute.inject_options %> diff --git a/railties/lib/rails/generators/migration.rb b/railties/lib/rails/generators/migration.rb index a755471a4b..87f2e1d42b 100644 --- a/railties/lib/rails/generators/migration.rb +++ b/railties/lib/rails/generators/migration.rb @@ -30,11 +30,6 @@ module Rails end end - def id_kind - kind = Rails.application.config.active_record.primary_key rescue nil - ", id: :#{kind}" if kind - end - def create_migration(destination, data, config = {}, &block) action Rails::Generators::Actions::CreateMigration.new(self, destination, block || data.to_s, config) end diff --git a/railties/test/generators/migration_generator_test.rb b/railties/test/generators/migration_generator_test.rb index a7f807b747..832bf8d2d4 100644 --- a/railties/test/generators/migration_generator_test.rb +++ b/railties/test/generators/migration_generator_test.rb @@ -222,8 +222,9 @@ class MigrationGeneratorTest < Rails::Generators::TestCase end def test_add_uuid_to_create_table_migration - previous_value = Rails.application.config.active_record.primary_key - Rails.application.config.active_record.primary_key = :uuid + 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"] assert_migration "db/migrate/create_books.rb" do |content| assert_method :change, content do |change| @@ -231,7 +232,7 @@ class MigrationGeneratorTest < Rails::Generators::TestCase end end - Rails.application.config.active_record.primary_key = previous_value + 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