Permalink
Browse files

-- migration.rb: added add_index for reference columns

-- test: added a test to ensure creation of add_index
-- test_helper: added assert_generated_index
  • Loading branch information...
eladmeidar
eladmeidar committed Apr 2, 2009
1 parent 6dcf653 commit 86ebea74fdf5717d96919ca32416a6c796ae65e4
Showing with 20 additions and 0 deletions.
  1. +8 −0 generators/model_plus/templates/migration.rb
  2. +5 −0 test/generator_test_helper.rb
  3. +7 −0 test/model_plus_test.rb
@@ -1,3 +1,7 @@
+<%
+ indexed_foreign_keys = attributes.select {|attr| attr.is_reference? }
+%>
+
class <%= migration_name %> < ActiveRecord::Migration
def self.up
create_table :<%= table_name %> do |t|
@@ -10,6 +14,10 @@ def self.up
t.timestamps
<% end -%>
end
+
+ <% for attribute in indexed_foreign_keys %>
+ add_index :<%= table_name %>, :<%= attribute.name + "_id" %>
+ <% end %>
end
def self.down
@@ -301,4 +301,9 @@ def assert_has_method(body, *methods)
def assert_generated_column(body, name, type)
assert_match /t\.#{type.to_s} :#{name.to_s}/, body, "should have column #{name.to_s} defined"
end
+
+ # Asserts that the given column's index has been defined in the migration
+ def assert_generated_index(body, table_name, column_name)
+ assert_match /add_index \:#{table_name.to_s}, :#{column_name.to_s}_id/, body, "should have index on column #{column_name} defined"
+ end
end
View
@@ -48,6 +48,13 @@ def test_model_with_belongs_to_attributes_generates_belongs_to_associations
end
end
+ def test_model_with_belongs_to_attributes_generates_add_index_statement
+ run_generator('model_plus', %w(Product name:string supplier:belongs_to))
+
+ assert_generated_migration :create_products do |t|
+ assert_generated_index t, :products, :supplier
+ end
+ end
def test_model_with_has_many_attributes_generates_has_many_associations
run_generator('model_plus', %w(Supplier name:string products:has_many))

0 comments on commit 86ebea7

Please sign in to comment.