Skip to content
Browse files

Extend Collection Presenter to receive some parameters

  • Loading branch information...
1 parent 60da254 commit 93021f4972468d7dff021acce81f55592168485a @rnaveiras rnaveiras committed Mar 21, 2012
Showing with 97 additions and 4 deletions.
  1. +21 −3 lib/almodovar/resource_presenter/collection.rb
  2. +76 −1 spec/acceptance/present_resource_spec.rb
View
24 lib/almodovar/resource_presenter/collection.rb
@@ -2,17 +2,35 @@ module Almodovar
class ResourcePresenter::Collection
- def initialize(resource_class, resources_args = [])
+ def initialize(resource_class, resources_args = [], options = {})
@resource_class = resource_class
@resources = resources_args.map { |arg| @resource_class.new(arg) }
+
+ @total = options[:total_entries]
+ @next = options[:next_link]
+ @prev = options[:prev_link]
end
def to_xml(options = {})
- @resources.to_xml(options.merge({:root => resource_type.pluralize}))
+ @resources.to_xml(options.merge({:root => resource_type.pluralize})) do |xml|
+ xml.tag!('total-entries', @total) if @total
+ xml.link :rel => 'next', :href => @next if @next
+ xml.link :rel => 'prev', :href => @prev if @prev
+ end
end
def as_json(options = {})
- { 'entries' => @resources.map { |resource| resource.as_json(options) }}
+ ActiveSupport::OrderedHash.new.tap do |message|
+ message[:total_entries] = @total if @total
+ message[:next_link] = @next if @next
+ message[:prev_link] = @prev if @prev
+ message[:entries] = @resources.map { |resource| resource.as_json(options) }
+ end
+ end
+
+ def to_json(options = {})
+ require 'yajl'
+ Yajl::Encoder.encode(as_json(options), :pretty => true) + "\n"
end
def resource_type
View
77 spec/acceptance/present_resource_spec.rb
@@ -73,6 +73,42 @@ def initialize(model)
</series>
XML
end
+
+ example 'Presenting a resource collection in xml format with options' do
+ collection = []
+ 1.upto(3) { |i| collection << Series.new(i, 'Mad Men S1')}
+
+ resources = Almodovar::ResourcePresenter::Collection.new(SeriesResource, collection, { :total_entries => 3, :next_link => 'http://wadus.com/series?after=3' })
+
+ resources.to_xml.should equal_xml <<-XML
+<?xml version="1.0" encoding="UTF-8"?>
+<series type="array">
+ <total-entries>3</total-entries>
+ <link rel="next" href="http://wadus.com/series?after=3"/>
+ <series>
+ <id type="integer">1</id>
+ <name>Mad Men S1</name>
+ <link rel="self" href="http://wadus.com/series/1"/>
+ <link rel="show" href="http://wadus.com/show/20"/>
+ <link rel="episodes" href="http://wadus.com/series/1/episodes"/>
+ </series>
+ <series>
+ <id type="integer">2</id>
+ <name>Mad Men S1</name>
+ <link rel="self" href="http://wadus.com/series/2"/>
+ <link rel="show" href="http://wadus.com/show/20"/>
+ <link rel="episodes" href="http://wadus.com/series/2/episodes"/>
+ </series>
+ <series>
+ <id type="integer">3</id>
+ <name>Mad Men S1</name>
+ <link rel="self" href="http://wadus.com/series/3"/>
+ <link rel="show" href="http://wadus.com/show/20"/>
+ <link rel="episodes" href="http://wadus.com/series/3/episodes"/>
+ </series>
+</series>
+XML
+ end
scenario 'Presenting a resource in json format' do
resource = SeriesResource.new(Series.new(5, 'Mad Men S1'))
@@ -121,4 +157,43 @@ def initialize(model)
}
JSON
end
-end
+
+ example 'Presenting a resource in json format with options' do
+ collection = []
+ 1.upto(3) { |i| collection << Series.new(i, 'Two and a Half Men ')}
+
+ resources = Almodovar::ResourcePresenter::Collection.new(SeriesResource, collection, { :total_entries => 3, :prev_link => 'http://wadus.com/series?before=1' })
+ resources.to_json.should == <<-JSON
+{
+ "total_entries": 3,
+ "prev_link": "http://wadus.com/series?before=1",
+ "entries": [
+ {
+ "resource_type": "series",
+ "id": 1,
+ "name": "Two and a Half Men ",
+ "self_link": "http://wadus.com/series/1",
+ "show_link": "http://wadus.com/show/20",
+ "episodes_link": "http://wadus.com/series/1/episodes"
+ },
+ {
+ "resource_type": "series",
+ "id": 2,
+ "name": "Two and a Half Men ",
+ "self_link": "http://wadus.com/series/2",
+ "show_link": "http://wadus.com/show/20",
+ "episodes_link": "http://wadus.com/series/2/episodes"
+ },
+ {
+ "resource_type": "series",
+ "id": 3,
+ "name": "Two and a Half Men ",
+ "self_link": "http://wadus.com/series/3",
+ "show_link": "http://wadus.com/show/20",
+ "episodes_link": "http://wadus.com/series/3/episodes"
+ }
+ ]
+}
+JSON
+ end
+end

0 comments on commit 93021f4

Please sign in to comment.
Something went wrong with that request. Please try again.