Permalink
Browse files

Add more flexibility to Collection

  • Loading branch information...
1 parent f88bcbf commit c22a87e62e9c4d2f97f4793b101ceeef93749bec @jhchabran jhchabran committed Jul 23, 2012
Showing with 12 additions and 4 deletions.
  1. +11 −3 lib/vintner/collection.rb
  2. +1 −1 spec/vintner/representer_spec.rb
@@ -11,11 +11,19 @@ def import
raise "TODO"
end
- def export model
+ # Depending on how you use a collection, it can
+ # be invoked with an array or with a model
+ # if you're defining nested collections
+ # like a has_many relationship
+ def export model_or_collection
if getter_defined?
- result = @getter.call model
+ result = @getter.call model_or_collection
else
- result = model.send(@name)
+ if model_or_collection.is_a? Array
+ result = model_or_collection
+ else
+ result = model_or_collection.send(@name)
+ end
end
wrap_representers result
@@ -203,7 +203,7 @@ class DummyCollection
include Vintner::Representer
collection :dummies, Dummy do
- get { |model| model.collection }
+ get { |model| model.dummies }
end
representation do |json|

0 comments on commit c22a87e

Please sign in to comment.