Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add more flexibility to Collection

  • Loading branch information...
commit c22a87e62e9c4d2f97f4793b101ceeef93749bec 1 parent f88bcbf
@jhchabran jhchabran authored
Showing with 12 additions and 4 deletions.
  1. +11 −3 lib/vintner/collection.rb
  2. +1 −1  spec/vintner/representer_spec.rb
View
14 lib/vintner/collection.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
View
2  spec/vintner/representer_spec.rb
@@ -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|
Please sign in to comment.
Something went wrong with that request. Please try again.