Module: Shogun::Presenter

Defined in:
lib/shogun/presenter.rb

Instance Method Summary collapse

Instance Method Details

#hrefObject



11
12
13
# File 'lib/shogun/presenter.rb', line 11

def href
  [host, namespace, id].join("/")
end

#initialize(resource:, includes: [], meta: {}, links: {}, linked: {}) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/shogun/presenter.rb', line 3

def initialize(resource:, includes: [], meta: {}, links: {}, linked: {})
  @source = resource
  @includes = includes
  @meta = meta
  @links = links
  @linked = linked
end

#linkedObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/shogun/presenter.rb', line 27

def linked
  @linked.tap do |hash|
    includes.each do |name|
      case association(name).macro
      when :has_many
        hash.store(name, source.public_send(name).pluck(:id))
      when :belongs_to
        hash.store(name, source.public_send(association(name).foreign_key))
      when :has_one
        hash.store(name, source.public_send(name).pluck(:id))
      end
    end
  end
end


19
20
21
22
23
24
25
# File 'lib/shogun/presenter.rb', line 19

def links
  @links.tap do |hash|
    includes.each do |name|
      hash.store(name, [host, name, "?#{name}=#{id}"].join("/"))
    end
  end
end

#metaObject



15
16
17
# File 'lib/shogun/presenter.rb', line 15

def meta
  @meta
end