提交 5a47639a 编写于 作者: R Rafael Mendonça França

Merge pull request #22033 from jmccartie/jm/uuid_nil

Guard clause for id_kind in generator
......@@ -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*
......
......@@ -13,6 +13,13 @@ def next_migration_number(dirname)
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
......@@ -5,6 +5,8 @@ module Generators # :nodoc:
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!
......
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 %>
......
......@@ -7,14 +7,13 @@ class ModelGenerator < Base # :nodoc:
check_class_collision
class_option :migration, :type => :boolean
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 :migration, type: :boolean
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
......
......@@ -30,11 +30,6 @@ def next_migration_number(dirname)
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
......
......@@ -222,16 +222,12 @@ def test_create_table_migration
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
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.active_record.primary_key = previous_value
end
def test_should_create_empty_migrations_if_name_not_start_with_add_or_remove_or_create
......
......@@ -374,6 +374,15 @@ def test_index_is_skipped_for_references_association
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}"]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册