diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2008-08-23 21:41:18 -0700 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2008-08-23 21:41:18 -0700 |
commit | 53e2e8ef2bebb7fb444fe472c657ddb11fae1c89 (patch) | |
tree | 7b90f6e92d318a2112091a2448e2c1d5507a4149 /railties | |
parent | 23fb26a0dcbd46d36bdbc2caa267aa3192f77a24 (diff) | |
parent | c471f13db63844fe290615ed6e1ddca32b26570d (diff) | |
download | rails-53e2e8ef2bebb7fb444fe472c657ddb11fae1c89.tar.gz rails-53e2e8ef2bebb7fb444fe472c657ddb11fae1c89.tar.bz2 rails-53e2e8ef2bebb7fb444fe472c657ddb11fae1c89.zip |
Merge branch 'master' into i18n
Diffstat (limited to 'railties')
3 files changed, 23 insertions, 0 deletions
diff --git a/railties/lib/rails_generator/generated_attribute.rb b/railties/lib/rails_generator/generated_attribute.rb index 25af3931de..a3d4a01142 100644 --- a/railties/lib/rails_generator/generated_attribute.rb +++ b/railties/lib/rails_generator/generated_attribute.rb @@ -37,6 +37,10 @@ module Rails "" end end + + def reference? + [ :references, :belongs_to ].include?(self.type) + end end end end diff --git a/railties/lib/rails_generator/generators/components/model/templates/model.rb b/railties/lib/rails_generator/generators/components/model/templates/model.rb index 8d4c89e912..6fcf393bdf 100644 --- a/railties/lib/rails_generator/generators/components/model/templates/model.rb +++ b/railties/lib/rails_generator/generators/components/model/templates/model.rb @@ -1,2 +1,5 @@ class <%= class_name %> < ActiveRecord::Base +<% attributes.select(&:reference?).each do |attribute| -%> + belongs_to :<%= attribute.name %> +<% end -%> end diff --git a/railties/test/generators/rails_model_generator_test.rb b/railties/test/generators/rails_model_generator_test.rb index 0bfc338566..aea2abafba 100644 --- a/railties/test/generators/rails_model_generator_test.rb +++ b/railties/test/generators/rails_model_generator_test.rb @@ -29,4 +29,20 @@ class RailsModelGeneratorTest < GeneratorTestCase assert_generated_column t, :created_at, :timestamp end end + + def test_model_with_reference_attributes_generates_belongs_to_associations + run_generator('model', %w(Product name:string supplier:references)) + + assert_generated_model_for :product do |body| + assert body =~ /^\s+belongs_to :supplier/, "#{body.inspect} should contain 'belongs_to :supplier'" + end + end + + def test_model_with_belongs_to_attributes_generates_belongs_to_associations + run_generator('model', %w(Product name:string supplier:belongs_to)) + + assert_generated_model_for :product do |body| + assert body =~ /^\s+belongs_to :supplier/, "#{body.inspect} should contain 'belongs_to :supplier'" + end + end end |