Class: Spree::AgentTools::SearchResources

Inherits:
Spree::AgentTool show all
Defined in:
app/tools/spree/agent_tools/search_resources.rb

Overview

Searches store records, through the Admin API's own list operation.

Filters, sorting, pagination, store scoping and record-level permissions are the endpoint's, not this tool's. That is the point: the tool used to call .ransack itself and skipped the step that decodes prefixed ids, so filtering by an id it had just handed the model matched nothing and reported it as a fact.

Constant Summary collapse

DEFAULT_LIMIT =
10
MAX_LIMIT =

The Admin API's own page ceiling; asking for more returns this many.

100

Constants inherited from Spree::AgentTool

Spree::AgentTool::PARENT_ID_DESCRIPTION

Instance Attribute Summary

Attributes inherited from Spree::AgentTool

#context

Instance Method Summary collapse

Methods inherited from Spree::AgentTool

additive!, audience, description, destructive?, #initialize, input_schema, mutating!, mutating?, param, params, permission, #permitted?, tool_name, #unknown_resource

Constructor Details

This class inherits a constructor from Spree::AgentTool

Instance Method Details

#call(resource:, filters: nil, sort: nil, limit: nil, page: nil, parent_id: nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/tools/spree/agent_tools/search_resources.rb', line 35

def call(resource:, filters: nil, sort: nil, limit: nil, page: nil, parent_id: nil)
  entry = ResourceMap.find(resource)
  return unknown_resource(resource) if entry.nil?
  return not_searchable(entry) if entry.api_path(:index).blank?
  return forbidden(entry) unless entry.readable_by?(context)

  applied = sanitize_filters(filters)
  # Ransack drops a filter it does not recognise and returns the whole
  # collection, which a model reports as the answer ("you have 38
  # products with status notreal"). The Admin API will refuse those
  # itself once strict filters land; until then this is the only thing
  # between a typo and a confident wrong answer, so it stays.
  unrecognised = unrecognised_filters(entry, applied)
  return invalid_filters(entry, unrecognised) if unrecognised.any?

  path = entry.path_for(:index, parent_id: parent_id)
  return wrong_parent(entry, parent_id) if path.nil? && parent_id.present? && !entry.nested?(:index)
  return not_searchable(entry) if path.nil?

  response = dispatch.call(method: :get, path: path, params: query_for(applied, sort, limit, page))
  return { error: response.error_message } unless response.success?

  rows = Array(response.data)
  meta = response.meta || {}

  {
    resource: entry.key,
    # What the endpoint counted, not what this page holds — the model is
    # asked how many there are, and would otherwise say "10 products"
    # for a store holding hundreds.
    total: meta['count'] || rows.size,
    count: rows.size,
    page: meta['page'],
    pages: meta['pages'],
    # Named outright, because a model told only "25 of 88" reports the
    # 25 as the answer.
    next_page: meta['next'],
    records: rows.map { |row| RecordSummary.from_payload(entry: entry, payload: row) }
  }.compact
end

#summary(arguments) ⇒ Object



76
77
78
79
80
81
82
83
# File 'app/tools/spree/agent_tools/search_resources.rb', line 76

def summary(arguments)
  label = arguments[:resource].to_s.tr('_', ' ')
  filters = arguments[:filters]
  return "Search #{label}" if filters.blank?

  described = filters.keys.map { |key| key.to_s.tr('_', ' ') }.join(', ')
  "Search #{label} by #{described}"
end