diff options
author | Tarmo Tänav <tarmo@itech.ee> | 2008-08-23 21:54:43 +0300 |
---|---|---|
committer | Tarmo Tänav <tarmo@itech.ee> | 2008-08-23 21:54:43 +0300 |
commit | b1f3c6e6ec08a40fc7153ee3ba78d51b466a58e6 (patch) | |
tree | a1ec39f011ff12c0f052c81c6927c8d30cf8e8eb /railties | |
parent | 9223a919111867e6b47b2627c30d8eb21e8ac7d9 (diff) | |
download | rails-b1f3c6e6ec08a40fc7153ee3ba78d51b466a58e6.tar.gz rails-b1f3c6e6ec08a40fc7153ee3ba78d51b466a58e6.tar.bz2 rails-b1f3c6e6ec08a40fc7153ee3ba78d51b466a58e6.zip |
Generate belongs_to association when generating a model
Diffstat (limited to 'railties')
3 files changed, 13 insertions, 1 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 8bf93b9e8b..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,5 +1,5 @@ class <%= class_name %> < ActiveRecord::Base -<% attributes.select { |a| a.type.to_s == 'references' }.each do |attribute| -%> +<% 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 67cd3aa26f..aea2abafba 100644 --- a/railties/test/generators/rails_model_generator_test.rb +++ b/railties/test/generators/rails_model_generator_test.rb @@ -37,4 +37,12 @@ class RailsModelGeneratorTest < GeneratorTestCase 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 |