Class: Grape::Entity

Inherits:
Object
  • Object
show all
Defined in:
lib/hypertext_application_language/grape/entity.rb

Instance Method Summary collapse

Instance Method Details

#to_hal(env: {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/hypertext_application_language/grape/entity.rb', line 5

def to_hal(env: {})
  representation = HypertextApplicationLanguage::Representation.new

  rel = object.class.table_name
  representation.with_link(HypertextApplicationLanguage::Link::SELF_REL, "#{rel}/#{object.id}")

  object.class.reflections.each do |name, reflection|
    href = unless i(has_one belongs_to).include?(reflection.macro) && (association = object.send(name))
             "#{representation.link.href}/#{name}"
           else
             "#{reflection.plural_name}/#{association.id}"
           end
    representation.with_link(name, href)
  end

  representation.properties = serializable_hash

  representation
end