Guest User

Untitled

a guest
Sep 19th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. require 'delegate'
  2.  
  3. class DropCollectionExhibit < SimpleDelegator
  4. def initialize(drops, options)
  5. @links = options.fetch :links, {}
  6. @context = options.fetch :context
  7. super Array(drops)
  8. end
  9.  
  10. def render
  11. @context.render json: { collection: collection },
  12. status: :ok,
  13. content_type: 'application/vnd.collection+json'
  14. end
  15.  
  16. protected
  17.  
  18. def collection
  19. { version: '1.0',
  20. href: '/drops',
  21. items: exhibited_drops,
  22. templates: templates
  23. }.merge(links)
  24. end
  25.  
  26. def exhibited_drops
  27. map {|drop| DropExhibit.new(drop).as_json }
  28. end
  29.  
  30. def links
  31. return {} unless previous_link or next_link
  32.  
  33. { links: [].tap do |links|
  34. links << { rel: 'next', href: next_link } if next_link
  35. links << { rel: 'previous', href: previous_link } if previous_link
  36. end
  37. }
  38. end
  39.  
  40. def templates
  41. [{
  42. rel: '/rels/recover',
  43. href: '/drops?filter=trash',
  44. data: [{ name: 'drop_ids', value: [] }]
  45. }, {
  46. rel: '/rels/remove',
  47. href: '/drops',
  48. data: [{ name: 'drop_ids', value: [] }]
  49. }]
  50. end
  51.  
  52. def previous_link
  53. @links.fetch :previous, nil
  54. end
  55.  
  56. def next_link
  57. @links.fetch :next, nil
  58. end
  59. end
Add Comment
Please, Sign In to add comment