Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Representable::JSON#collection_wrapper #22

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/representable/json.rb
Expand Up @@ -21,6 +21,14 @@ module ClassMethods
def from_json(*args, &block)
create_represented(*args, &block).from_json(*args)
end

def collection_wrapper opions = {}
representer = self
Module.new do
include Representable::JSON::Collection
items({:extend => representer}.merge(opions))
end
end
end


Expand Down
20 changes: 20 additions & 0 deletions test/json_test.rb
Expand Up @@ -485,6 +485,26 @@ module SongRepresenter
end
end

class CollectionRepresenterTest < MiniTest::Spec
describe "JSON#collection_wrapper" do
describe "with contained objects" do
before do
@song_representer = Module.new do
include Representable::JSON
property :name
end
end

it "renders objects with #to_json" do
assert_json "[{\"name\":\"Days Go By\"},{\"name\":\"Can't Take Them All\"}]", [Song.new("Days Go By"), Song.new("Can't Take Them All")].extend(@song_representer.collection_wrapper).to_json
end

it "returns objects array from #from_json" do
assert_equal [Song.new("Days Go By"), Song.new("Can't Take Them All")], [].extend(@song_representer.collection_wrapper(:class => Song)).from_json("[{\"name\":\"Days Go By\"},{\"name\":\"Can't Take Them All\"}]")
end
end
end
end

require 'representable/json/hash'
class HashRepresenterTest < MiniTest::Spec
Expand Down