Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'delegate'
- class DropCollectionExhibit < SimpleDelegator
- def initialize(drops, options)
- @links = options.fetch :links, {}
- @context = options.fetch :context
- super Array(drops)
- end
- def render
- @context.render json: { collection: collection },
- status: :ok,
- content_type: 'application/vnd.collection+json'
- end
- protected
- def collection
- { version: '1.0',
- href: '/drops',
- items: exhibited_drops,
- templates: templates
- }.merge(links)
- end
- def exhibited_drops
- map {|drop| DropExhibit.new(drop).as_json }
- end
- def links
- return {} unless previous_link or next_link
- { links: [].tap do |links|
- links << { rel: 'next', href: next_link } if next_link
- links << { rel: 'previous', href: previous_link } if previous_link
- end
- }
- end
- def templates
- [{
- rel: '/rels/recover',
- href: '/drops?filter=trash',
- data: [{ name: 'drop_ids', value: [] }]
- }, {
- rel: '/rels/remove',
- href: '/drops',
- data: [{ name: 'drop_ids', value: [] }]
- }]
- end
- def previous_link
- @links.fetch :previous, nil
- end
- def next_link
- @links.fetch :next, nil
- end
- end
Add Comment
Please, Sign In to add comment