Permalink
Browse files

Generate belongs_to association when generating a model

  • Loading branch information...
1 parent 9223a91 commit b1f3c6e6ec08a40fc7153ee3ba78d51b466a58e6 @tarmo tarmo committed Aug 23, 2008
@@ -37,6 +37,10 @@ def default
""
end
end
+
+ def reference?
+ [ :references, :belongs_to ].include?(self.type)
+ end
end
end
end
@@ -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
@@ -37,4 +37,12 @@ def test_model_with_reference_attributes_generates_belongs_to_associations
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

0 comments on commit b1f3c6e

Please sign in to comment.