module Shogun::Presenter

Public Class Methods

new(resource:, includes: [], meta: {}, links: {}, linked: {}) click to toggle source
# 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

Public Instance Methods

href() click to toggle source
# File lib/shogun/presenter.rb, line 11
def href
  [host, namespace, id].join("/")
end
linked() click to toggle source
# 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
meta() click to toggle source
# File lib/shogun/presenter.rb, line 15
def meta
  @meta
end

Private Instance Methods

association(name) click to toggle source
# File lib/shogun/presenter.rb, line 50
        def association(name)
  associations[name.to_sym]
end
associations() click to toggle source
# File lib/shogun/presenter.rb, line 54
        def associations
  source.class.reflections
end
host() click to toggle source
# File lib/shogun/presenter.rb, line 42
        def host
  ENV["SERVER_HOST"]
end
includes() click to toggle source
# File lib/shogun/presenter.rb, line 62
        def includes
  @includes
end
namespace() click to toggle source
# File lib/shogun/presenter.rb, line 46
        def namespace
  self.class.const_get("NAMESPACE")
end
source() click to toggle source
# File lib/shogun/presenter.rb, line 58
        def source
  @source
end