Module: Gris::PresenterLinkHelpers

Defined in:
lib/gris/output_formatters/presenter_link_helpers.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/gris/output_formatters/presenter_link_helpers.rb', line 3

def self.included(base)
  base.class_eval do
    def self.resource_links(name, args = [], resource_uri_template = '/{id}')
      args += %w(page size)
      endpoint_link(
        name.to_s.pluralize,
        template_options: args, templated: true
      )
      endpoint_link(
        name,
        namespace: name.to_s.pluralize,
        uri_template: resource_uri_template,
        templated: true
      )
    end

    def self.endpoint_link(name, options = {})
      namespace = options[:namespace] || name
      template_options = options[:template_options] || []
      uri_template = options[:uri_template] || format_template_options(template_options)
      link name do
        link = {
          href: "#{Gris::Identity.base_url}/#{namespace}#{uri_template}"
        }
        link[:templated] = true if !!options[:templated] || !template_options.blank?
        link
      end
    end

    private

    def self.format_template_options(template_options = [])
      return unless template_options.any?
      "{?#{template_options.join(',')}}"
    end
  end
end