Class: Core::Endpoint::BasicHandler::Associations::HasMany::Handler

Inherits:
Core::Endpoint::BasicHandler
  • Object
show all
Includes:
Paged
Defined in:
app/api/core/endpoint/basic_handler/associations/has_many/handler.rb

Overview

rubocop:todo Style/Documentation

Instance Method Summary collapse

Constructor Details

#initialize(association, options, &block) ⇒ Handler

Returns a new instance of Handler.


5
6
7
8
# File 'app/api/core/endpoint/basic_handler/associations/has_many/handler.rb', line 5

def initialize(association, options, &block)
  super(&block)
  @association, @options = association, options
end

Instance Method Details

#read(request, path) ⇒ Object


55
56
57
58
59
60
61
# File 'app/api/core/endpoint/basic_handler/associations/has_many/handler.rb', line 55

def read(request, path)
  association_details_for(request) do |association_io|
    association = association_from(request)
    eager_loaded = association_io.eager_loading_for(association).include_uuid
    nested_action(request, path, page_of_results(eager_loaded, path.first.try(:to_i) || 1, association)) { super }
  end
end

#results_per_pageObject


25
26
27
# File 'app/api/core/endpoint/basic_handler/associations/has_many/handler.rb', line 25

def results_per_page
  @options[:per_page] || super
end

#separate(associations, _) ⇒ Object

rubocop:todo Metrics/AbcSize


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/api/core/endpoint/basic_handler/associations/has_many/handler.rb', line 69

def separate(associations, _) # rubocop:todo Metrics/AbcSize
  associations[@options[:json].to_s] =
    lambda do |object, options, stream|
      stream.block(@options[:json].to_s) do |nested_stream|
        association = object.send(@association)
        nested_stream.attribute('size', association.count)

        nested_stream.block('actions') do |action_stream|
          actions(count_of_pages(association), options.merge(target: object)).map do |action, url|
            action_stream.attribute(action, url)
          end
        end
      end
    end
end