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

Instance Method Summary collapse

Constructor Details

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


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

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

Instance Method Details

#read(request, path) ⇒ Object


50
51
52
53
54
55
56
57
58
# File 'app/api/core/endpoint/basic_handler/associations/has_many/handler.rb', line 50

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)) do
      super
    end
  end
end

#results_per_pageObject


20
21
22
# File 'app/api/core/endpoint/basic_handler/associations/has_many/handler.rb', line 20

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

#separate(associations, _) ⇒ Object


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

def separate(associations, _)
  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