Module: HalApi::Representer::Embeds::ClassMethods

Defined in:
lib/hal_api/representer/embeds.rb

Overview

Possible values for zoom option in the embed representer definition

  • false - will be zoomed only if in the root doc and in the zoom param

  • true - zoomed in root doc if no zoom_param, or if included in zoom_param

  • always - zoomed no matter what is in zoom param, and even if in embed

Instance Method Summary collapse

Instance Method Details

#embed(name, options = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/hal_api/representer/embeds.rb', line 64

def embed(name, options={})
  options[:embedded] = true
  options[:writeable] = false
  options[:if] ||= ->(_a) { id } unless options[:zoom] == :always

  if options[:paged]
    opts = {
      no_curies: true,
      item_class: options.delete(:item_class),
      url: options.delete(:url),
      item_decorator: options.delete(:item_decorator)
    }
    getter_per = options.delete(:per) || Kaminari.config.default_per_page
    options[:getter] ||= ->(*) do
      per = getter_per == :all ? send(name).count : getter_per
      HalApi::PagedCollection.new(send(name).page(1).per(per), nil, opts.merge(parent: self))
    end
    options[:decorator] = HalApi::PagedCollection.representer
  end

  property(name, options)
end

#embeds(name, options = {}) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/hal_api/representer/embeds.rb', line 87

def embeds(name, options={})
  options[:embedded] = true
  options[:writeable] = false
  options[:if] ||= ->(_a) { id } unless options[:zoom] == :always

  collection(name, options)
end