提交 4e390720 编写于 作者: M Michał Łomnicki 提交者: Santiago Pastorino

Configurable generation of add_index for references columns

Signed-off-by: NSantiago Pastorino <santiago@wyeworks.com>
上级 5b42e966
......@@ -10,6 +10,7 @@ class ModelGenerator < Base
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"
def create_migration_file
return unless options[:migration] && options[:parent].nil?
......
......@@ -9,8 +9,10 @@ def change
<% end -%>
end
<% if options[:indexes] %>
<% attributes.select {|attr| attr.reference? }.each do |attribute| -%>
add_index :<%= table_name %>, :<%= attribute.name %>_id
<% end -%>
<% end %>
end
end
......@@ -203,4 +203,45 @@ def test_check_class_collision
content = capture(:stderr){ run_generator ["object"] }
assert_match /The name 'Object' is either already used in your application or reserved/, content
end
def test_index_is_added_for_belongs_to_association
run_generator ["account", "supplier:belongs_to"]
assert_migration "db/migrate/create_accounts.rb" do |m|
assert_method :change, m do |up|
assert_match /add_index/, up
end
end
end
def test_index_is_added_for_references_association
run_generator ["account", "supplier:references"]
assert_migration "db/migrate/create_accounts.rb" do |m|
assert_method :change, m do |up|
assert_match /add_index/, up
end
end
end
def test_index_is_skipped_for_belongs_to_association
run_generator ["account", "supplier:belongs_to", "--no-indexes"]
assert_migration "db/migrate/create_accounts.rb" do |m|
assert_method :change, m do |up|
assert_no_match /add_index/, up
end
end
end
def test_index_is_skipped_for_references_association
run_generator ["account", "supplier:references", "--no-indexes"]
assert_migration "db/migrate/create_accounts.rb" do |m|
assert_method :change, m do |up|
assert_no_match /add_index/, up
end
end
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册