Class: ApiMaker::IndexCommand

Inherits:
BaseCommand show all
Defined in:
app/services/api_maker/index_command.rb

Instance Attribute Summary collapse

Attributes inherited from BaseCommand

#api_maker_args, #collection, #command_response, #commands, #controller, #current_ability

Instance Method Summary collapse

Methods inherited from BaseCommand

#each_command, execute_in_thread!, goldiloader?, #initialize

Constructor Details

This class inherits a constructor from ApiMaker::BaseCommand

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



2
3
4
# File 'app/services/api_maker/index_command.rb', line 2

def params
  @params
end

Instance Method Details

#collection_from_query(collection) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/services/api_maker/index_command.rb', line 35

def collection_from_query(collection)
  ApiMaker::Configuration.profile("IndexCommand collection_from_query") do
    select = parse_select(params[:select]&.permit!&.to_hash) if params[:select]

    ApiMaker::CollectionSerializer.new(
      ability: current_ability,
      args: api_maker_args,
      collection: collection,
      include_param: params[:include],
      select: select
    ).result
  end
end

#execute!Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/services/api_maker/index_command.rb', line 4

def execute!
  ApiMaker::Configuration.profile("IndexCommand execute") do
    each_command do |command|
      @params = command.args || {}

      set_collection

      if params[:count]
        count = @query.count
        count = count.length if count.is_a?(Hash)

        command.result(count: count)
      else
        collection = collection_from_query(@query.fix)
        response = collection.as_json
        include_pagination_data(response, @query)

        command.result(response)
      end
    end
  end

  ServicePattern::Response.new(success: true)
end

#filter_custom_accessible_by(collection) ⇒ Object



29
30
31
32
33
# File 'app/services/api_maker/index_command.rb', line 29

def filter_custom_accessible_by(collection)
  return collection if params[:accessible_by].blank?

  collection.accessible_by(current_ability, params[:accessible_by].to_sym)
end

#include_pagination_data(response, collection) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'app/services/api_maker/index_command.rb', line 49

def include_pagination_data(response, collection)
  return if params[:page].blank?

  response[:meta] = {
    currentPage: collection.current_page,
    totalCount: collection.try(:total_count) || collection.try(:total_entries),
    totalPages: collection.total_pages
  }
end

#manage_through_relationshipObject



59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/services/api_maker/index_command.rb', line 59

def manage_through_relationship
  return if params[:through].blank?

  model_class = params[:through][:model].safe_constantize
  through_model = model_class.accessible_by(current_ability).find(params[:through][:id])
  association = ActiveRecord::Associations::Association.new(through_model, model_class.reflections.fetch(params[:through][:reflection]))

  query_through = association.scope
  query_through = query_through.accessible_by(current_ability)
  query_through = filter_custom_accessible_by(query_through)
  query_through
end

#parse_select(select) ⇒ Object

This converts the list of attributes to a hash that contains the data needed for the serializer (so the serializer doesn’t have to do it for each model)



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/services/api_maker/index_command.rb', line 73

def parse_select(select)
  new_select = {}

  select.each do |model_collection_name, attributes|
    model_class = model_collection_name.underscore.singularize.camelize
    resource = "Resources::#{model_class}Resource".safe_constantize
    raise "Resource not found for: #{model_collection_name}" unless resource

    new_attributes = resource._attributes.select { |key| attributes.include?(key.to_s) }
    new_select[resource.model_class] = new_attributes
  end

  new_select
end

#set_collectionObject



88
89
90
91
92
93
94
95
# File 'app/services/api_maker/index_command.rb', line 88

def set_collection
  @query = manage_through_relationship || collection
  @query = @query.distinct if params[:distinct]
  @query = @query.ransack(params[:q]).result
  @query = @query.limit(params[:limit]) if params[:limit].present?
  @query = @query.page(params[:page]) if params[:page].present?
  @query = filter_custom_accessible_by(@query)
end