Class: Spree::AgentTools::ResourceMap::Entry
- Inherits:
-
Struct
- Object
- Struct
- Spree::AgentTools::ResourceMap::Entry
- Defined in:
- lib/spree/agent_tools/resource_map.rb
Instance Attribute Summary collapse
-
#access_permissions ⇒ Object
Returns the value of attribute access_permissions.
-
#api_paths ⇒ Object
Returns the value of attribute api_paths.
-
#create_workflow_key ⇒ Object
Returns the value of attribute create_workflow_key.
-
#dashboard_path ⇒ Object
Returns the value of attribute dashboard_path.
-
#distinct ⇒ Object
Returns the value of attribute distinct.
-
#index_paths ⇒ Object
Returns the value of attribute index_paths.
-
#key ⇒ Object
Returns the value of attribute key.
-
#model_name ⇒ Object
Returns the value of attribute model_name.
-
#permission ⇒ Object
Returns the value of attribute permission.
-
#scope_name ⇒ Object
Returns the value of attribute scope_name.
-
#serializer_name ⇒ Object
Returns the value of attribute serializer_name.
-
#update_workflow_key ⇒ Object
Returns the value of attribute update_workflow_key.
-
#writable_attributes ⇒ Object
Returns the value of attribute writable_attributes.
-
#write_permission ⇒ Object
Returns the value of attribute write_permission.
Instance Method Summary collapse
-
#api_path(action) ⇒ String?
The Admin API path for one of this resource's operations, which is how a tool reaches it.
-
#dashboard_path_for(record) ⇒ String?
Where this record lives in the dashboard, so an answer links to it rather than describing where to look.
-
#dispatchable? ⇒ Boolean
Whether a tool can dispatch to this resource's collection on its own.
-
#distinct? ⇒ Boolean
Whether a search may add DISTINCT.
- #expand_names_in(klass) ⇒ Object
-
#expandable_associations ⇒ Array<String>
The associations the show operation will include when asked, read from the serializer that renders it.
-
#filterable_fields ⇒ Array<String>
Fields the model actually allows filtering and sorting on.
-
#filterable_scopes ⇒ Array<String>
Named queries the model exposes —
in_stock,out_of_stock,price_lteand so on. -
#generic_writes? ⇒ Boolean
Whether the generic write tools may touch this resource.
-
#id_prefix ⇒ String?
The prefix of this resource's ids (
orfor orders), or nil. -
#index_parents ⇒ Hash{String => String}
The records this resource can be listed under, each with the collection path that lists it there — claims under an order, products under a category or collection, variants under a product.
- #model_class ⇒ Class
-
#nested?(action = :index) ⇒ Boolean
Whether the path a tool would dispatch
actionto runs under a parent. -
#nested_collection_shapes ⇒ Hash{String => Array<String>}
The shape of each attribute that carries a list of records — a product's
variants, an order'sitems. -
#nested_typed_families ⇒ Hash{String => Class}
The STI family behind each list-valued attribute whose rows carry a
type— a promotion'srulesandactions, a price list'srules. -
#parent_key(action = :index) ⇒ String?
The resource key of the parent
action's path runs under, read from the route rather than guessed from the model, so a renamed nested route (/orders/:order_id/items) still namesorders. -
#parent_param(action) ⇒ String?
The record a nested resource is reached through —
order_idfor an order's items, discounts and fees — or nil for a top-level one. -
#path_for(action, id: nil, parent_id: nil) ⇒ String?
The operation's path with its segments filled, or nil when a nested resource was not given the record it lives under.
-
#permission_keys ⇒ Array<String>
Every key that can open this resource.
-
#readable_by?(context) ⇒ Boolean
Whether this caller may read the resource.
-
#scope_for(context, action = :show) ⇒ ActiveRecord::Relation
Records of this kind the asking admin may see.
-
#serializer_class ⇒ Class
The admin serializer for this resource.
-
#serializer_file(klass) ⇒ Object
The file the autoloader maps the class to, under whichever engine holds it.
-
#store_scoped? ⇒ Boolean
Whether this resource's scoping is deliberate — either a real store relation, or a model that owns its
for_storeand has decided it is global. - #typed_association_for(name) ⇒ Object
-
#typed_family ⇒ Class?
The STI family this resource is, when its rows are selected by a
typethe model's own registry resolves — payment methods, seller requirements, promotion rules. - #typed_family_class(klass) ⇒ Object
-
#writable_attribute_names ⇒ Array<String>
The attributes the Admin API would accept on a write, so the generic tools offer exactly the same surface as the endpoint.
Instance Attribute Details
#access_permissions ⇒ Object
Returns the value of attribute access_permissions
23 24 25 |
# File 'lib/spree/agent_tools/resource_map.rb', line 23 def @access_permissions end |
#api_paths ⇒ Object
Returns the value of attribute api_paths
23 24 25 |
# File 'lib/spree/agent_tools/resource_map.rb', line 23 def api_paths @api_paths end |
#create_workflow_key ⇒ Object
Returns the value of attribute create_workflow_key
23 24 25 |
# File 'lib/spree/agent_tools/resource_map.rb', line 23 def create_workflow_key @create_workflow_key end |
#dashboard_path ⇒ Object
Returns the value of attribute dashboard_path
23 24 25 |
# File 'lib/spree/agent_tools/resource_map.rb', line 23 def dashboard_path @dashboard_path end |
#distinct ⇒ Object
Returns the value of attribute distinct
23 24 25 |
# File 'lib/spree/agent_tools/resource_map.rb', line 23 def distinct @distinct end |
#index_paths ⇒ Object
Returns the value of attribute index_paths
23 24 25 |
# File 'lib/spree/agent_tools/resource_map.rb', line 23 def index_paths @index_paths end |
#key ⇒ Object
Returns the value of attribute key
23 24 25 |
# File 'lib/spree/agent_tools/resource_map.rb', line 23 def key @key end |
#model_name ⇒ Object
Returns the value of attribute model_name
23 24 25 |
# File 'lib/spree/agent_tools/resource_map.rb', line 23 def model_name @model_name end |
#permission ⇒ Object
Returns the value of attribute permission
23 24 25 |
# File 'lib/spree/agent_tools/resource_map.rb', line 23 def @permission end |
#scope_name ⇒ Object
Returns the value of attribute scope_name
23 24 25 |
# File 'lib/spree/agent_tools/resource_map.rb', line 23 def scope_name @scope_name end |
#serializer_name ⇒ Object
Returns the value of attribute serializer_name
23 24 25 |
# File 'lib/spree/agent_tools/resource_map.rb', line 23 def serializer_name @serializer_name end |
#update_workflow_key ⇒ Object
Returns the value of attribute update_workflow_key
23 24 25 |
# File 'lib/spree/agent_tools/resource_map.rb', line 23 def update_workflow_key @update_workflow_key end |
#writable_attributes ⇒ Object
Returns the value of attribute writable_attributes
23 24 25 |
# File 'lib/spree/agent_tools/resource_map.rb', line 23 def writable_attributes @writable_attributes end |
#write_permission ⇒ Object
Returns the value of attribute write_permission
23 24 25 |
# File 'lib/spree/agent_tools/resource_map.rb', line 23 def @write_permission end |
Instance Method Details
#api_path(action) ⇒ String?
The Admin API path for one of this resource's operations, which is how a tool reaches it.
54 55 56 |
# File 'lib/spree/agent_tools/resource_map.rb', line 54 def api_path(action) Array(api_paths).to_h[action.to_sym] end |
#dashboard_path_for(record) ⇒ String?
Where this record lives in the dashboard, so an answer links to it rather than describing where to look. Store-relative — the panel prefixes the current store segment.
309 310 311 312 313 |
# File 'lib/spree/agent_tools/resource_map.rb', line 309 def dashboard_path_for(record) return if dashboard_path.blank? format(dashboard_path, id: record.prefixed_id) end |
#dispatchable? ⇒ Boolean
Whether a tool can dispatch to this resource's collection on its own.
A resource reached only under a parent is not: the tool has no parent id to put in the path, and dispatching the template as it stands answered "Promotion not found" for a search of promotion rules, which a model repeats as a fact about the store.
66 67 68 |
# File 'lib/spree/agent_tools/resource_map.rb', line 66 def dispatchable? api_path(:index).present? && !nested?(:index) end |
#distinct? ⇒ Boolean
Whether a search may add DISTINCT.
Postgres rejects SELECT DISTINCT beside an ORDER BY the select list does not carry, which is why some controllers turn it off. Carried from the controller rather than decided here, so a resource that changes its mind is followed without a second list to maintain.
100 101 102 |
# File 'lib/spree/agent_tools/resource_map.rb', line 100 def distinct? distinct.nil? ? true : distinct end |
#expand_names_in(klass) ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/spree/agent_tools/resource_map.rb', line 205 def (klass) file = serializer_file(klass) return [] if file.nil? # Comment lines are skipped: the base serializer explains `expand?` # with an example, which put `variants` on every resource's list. code = File.readlines(file).reject { |line| line.lstrip.start_with?('#') }.join code.scan(/expand\?\('([a-z_]+)'\)/).flatten rescue StandardError [] end |
#expandable_associations ⇒ Array<String>
The associations the show operation will include when asked, read from the serializer that renders it.
A record's associations are left out by default because a full admin payload is more than a model's context should hold. Without knowing the names, a model cannot ask for the one it needs — it fetched a product and saw no variants at all.
190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/spree/agent_tools/resource_map.rb', line 190 def @expandable_associations ||= begin # Walked up the chain rather than read off one file: a serializer # inherits most of its conditional associations from the Store # API one it extends. classes = Array(serializer_class&.ancestors).select do |ancestor| ancestor.is_a?(Class) && ancestor.name.to_s.include?('Serializer') end classes.flat_map { |klass| (klass) }.uniq.sort rescue StandardError [] end end |
#filterable_fields ⇒ Array<String>
Fields the model actually allows filtering and sorting on. Reused from the model's own Ransack allowlist so the assistant can never query a field the API would refuse.
382 383 384 |
# File 'lib/spree/agent_tools/resource_map.rb', line 382 def filterable_fields model_class.ransackable_attributes.sort end |
#filterable_scopes ⇒ Array<String>
Named queries the model exposes — in_stock, out_of_stock,
price_lte and so on. These answer the questions merchants actually
ask ("what's out of stock?") and cannot be expressed as an attribute
filter, because the answer lives across stock levels rather than in a
column. Passed as bare keys rather than field_predicate pairs.
393 394 395 |
# File 'lib/spree/agent_tools/resource_map.rb', line 393 def filterable_scopes model_class.ransackable_scopes.map(&:to_s).sort end |
#generic_writes? ⇒ Boolean
Whether the generic write tools may touch this resource.
A declared workflow no longer excludes it: the generic writes dispatch to the controller, which runs its own workflow, so there is still exactly one way to write each thing and this is it. What is left to answer is whether the endpoint accepts a body at all.
113 114 115 |
# File 'lib/spree/agent_tools/resource_map.rb', line 113 def generic_writes? .present? end |
#id_prefix ⇒ String?
The prefix of this resource's ids (or for orders), or nil.
323 324 325 326 327 |
# File 'lib/spree/agent_tools/resource_map.rb', line 323 def id_prefix model_class.try(:_prefix_id_prefix).presence rescue NameError nil end |
#index_parents ⇒ Hash{String => String}
The records this resource can be listed under, each with the collection path that lists it there — claims under an order, products under a category or collection, variants under a product. A path nested twice is left out: one parent id cannot fill it.
140 141 142 143 144 145 146 147 |
# File 'lib/spree/agent_tools/resource_map.rb', line 140 def index_parents Array(index_paths.presence || api_path(:index)).each_with_object({}) do |path, parents| next unless path.scan(/:[a-z_]+_id\b/).one? parent = path.scan(PARENT_SEGMENT).flatten.first parents[parent] ||= path if parent end end |
#model_class ⇒ Class
316 317 318 |
# File 'lib/spree/agent_tools/resource_map.rb', line 316 def model_class model_name.constantize end |
#nested?(action = :index) ⇒ Boolean
Whether the path a tool would dispatch action to runs under a
parent. Asked per action, because a resource can be listed at the
top level and written under its parent (/variants,
/products/:product_id/variants).
77 78 79 |
# File 'lib/spree/agent_tools/resource_map.rb', line 77 def nested?(action = :index) parent_key(action).present? end |
#nested_collection_shapes ⇒ Hash{String => Array<String>}
The shape of each attribute that carries a list of records — a
product's variants, an order's items.
A model that is only told the key exists sends the records one at a time to their own endpoints instead, which costs several round trips and, where the parent reconciles its list, leaves behind whatever the first write created.
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/spree/agent_tools/resource_map.rb', line 277 def nested_collection_shapes return {} unless defined?(Spree::Api::AgentWriteSchemas) Spree::Api::AgentWriteSchemas.for(key).each_with_object({}) do |(name, schema), shapes| next unless schema.is_a?(Hash) && schema['type'] == 'array' # A row's shape is published as a component too, so the array # points at one rather than describing its items inline. row = Spree::Api::AgentWriteSchemas.resolve_schema(schema['items']) properties = row&.dig('properties') next unless properties.is_a?(Hash) && properties.any? # Names alone left an agent to guess: it sent a number where the # endpoint wanted a decimal string, and omitted a field the model # requires, learning both from a 422. The type and the # requirement are already in the published schema. required = Array(row['required']) shapes[name.to_s] = properties.keys.sort.map do |field| { 'name' => field, 'type' => properties.dig(field, 'type'), 'required' => required.include?(field) } end end rescue StandardError {} end |
#nested_typed_families ⇒ Hash{String => Class}
The STI family behind each list-valued attribute whose rows carry a
type — a promotion's rules and actions, a price list's
rules. The attribute is the short name the write accepts; the
association behind it carries the model's prefix
(promotion_rules), which is how the two are matched.
243 244 245 246 247 248 249 250 251 252 |
# File 'lib/spree/agent_tools/resource_map.rb', line 243 def nested_typed_families nested_collection_shapes.each_with_object({}) do |(name, fields), families| next unless fields.any? { |field| field['name'] == 'type' } family = typed_family_class(typed_association_for(name)&.klass) families[name] = family if family end rescue StandardError {} end |
#parent_key(action = :index) ⇒ String?
The resource key of the parent action's path runs under, read
from the route rather than guessed from the model, so a renamed
nested route (/orders/:order_id/items) still names orders. The
innermost parent, for a path nested twice.
88 89 90 |
# File 'lib/spree/agent_tools/resource_map.rb', line 88 def parent_key(action = :index) api_path(action).to_s.scan(PARENT_SEGMENT).flatten.last end |
#parent_param(action) ⇒ String?
The record a nested resource is reached through — order_id for an
order's items, discounts and fees — or nil for a top-level one.
130 131 132 |
# File 'lib/spree/agent_tools/resource_map.rb', line 130 def parent_param(action) param_in(api_path(action)) end |
#path_for(action, id: nil, parent_id: nil) ⇒ String?
The operation's path with its segments filled, or nil when a nested resource was not given the record it lives under.
A search given a parent runs on the collection under that parent, chosen by the parent id's prefix, and is nil when the resource is not listed under that kind of record. Listing the whole store instead answered "the claims on order B" with every claim.
Each value is percent-encoded so it stays one segment: a ? or /
in an id would otherwise send the call to a different route than
the one the tool checked and the approver was shown. A path with a
segment left unfilled — a resource nested two levels deep — is nil
too, rather than dispatched with a placeholder in it.
167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/spree/agent_tools/resource_map.rb', line 167 def path_for(action, id: nil, parent_id: nil) path = action.to_sym == :index && parent_id.present? ? index_path_under(parent_id) : api_path(action) return if path.blank? path = path.sub(':id', ERB::Util.url_encode(id.to_s)) if id.present? parent = param_in(path) if parent return if parent_id.blank? path = path.sub(":#{parent}", ERB::Util.url_encode(parent_id.to_s)) end path unless path.match?(/:[a-z_]+/) end |
#permission_keys ⇒ Array<String>
Every key that can open this resource.
45 46 47 |
# File 'lib/spree/agent_tools/resource_map.rb', line 45 def || [] end |
#readable_by?(context) ⇒ Boolean
Whether this caller may read the resource.
Imports and exports have no key of their own: each record is gated
by the scope of the resource it carries. They list those keys in
access_permissions, and holding any one of them is enough to
reach the endpoint, which filters to the records that key covers.
36 37 38 39 40 |
# File 'lib/spree/agent_tools/resource_map.rb', line 36 def readable_by?(context) return context.permitted?() if .nil? .any? { |key| context.holds?(key) } end |
#scope_for(context, action = :show) ⇒ ActiveRecord::Relation
Records of this kind the asking admin may see.
Takes the context rather than the store so the ability filter cannot
be skipped by forgetting to apply it: store scoping answers tenancy,
accessible answers which records within that tenant — the same two
steps an Admin API controller takes.
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
# File 'lib/spree/agent_tools/resource_map.rb', line 339 def scope_for(context, action = :show) relation = model_class.for_store(context.store) # `Spree::Base.for_store` falls back to the bare class when a model # has no matching Store association, returning EVERY store's rows. # A model that defines its own `for_store` has made a deliberate # choice (customers are global by design, and Spree::Customer says # so); inheriting the fallback is an accident. Only the accident is # refused, because serving another store's records to an assistant # that reads them aloud is worse than a missing resource. if model_class.method(:for_store).owner == Spree::Base.singleton_class && relation.equal?(model_class) raise UnscopedResourceError, key end # A resource that is a slice of a model rather than the whole of it # (draft orders among orders) narrows further. relation = relation.public_send(scope_name) if scope_name.present? context.accessible(relation, action) end |
#serializer_class ⇒ Class
Returns the admin serializer for this resource.
373 374 375 |
# File 'lib/spree/agent_tools/resource_map.rb', line 373 def serializer_class serializer_name.constantize end |
#serializer_file(klass) ⇒ Object
The file the autoloader maps the class to, under whichever engine
holds it. const_source_location is the fallback: it names the file
that first touched the constant, which for a serializer another one
inherits from was the subclass's file.
221 222 223 224 225 |
# File 'lib/spree/agent_tools/resource_map.rb', line 221 def serializer_file(klass) relative = "#{klass.name.underscore}.rb" found = Rails.autoloaders.main.dirs.lazy.map { |dir| File.join(dir, relative) }.find { |path| File.exist?(path) } found || Object.const_source_location(klass.name)&.first.then { |path| path if path && File.exist?(path) } end |
#store_scoped? ⇒ Boolean
Whether this resource's scoping is deliberate — either a real
store relation, or a model that owns its for_store and has decided
it is global.
365 366 367 368 369 370 |
# File 'lib/spree/agent_tools/resource_map.rb', line 365 def store_scoped? model_class.method(:for_store).owner != Spree::Base.singleton_class || !model_class.for_store(Spree::Store.new).equal?(model_class) rescue StandardError false end |
#typed_association_for(name) ⇒ Object
254 255 256 257 258 |
# File 'lib/spree/agent_tools/resource_map.rb', line 254 def typed_association_for(name) reflections = model_class.reflect_on_all_associations(:has_many) reflections.find { |reflection| reflection.name.to_s == name } || reflections.find { |reflection| reflection.name.to_s.end_with?("_#{name}") } end |
#typed_family ⇒ Class?
The STI family this resource is, when its rows are selected by a
type the model's own registry resolves — payment methods, seller
requirements, promotion rules. Nil for an ordinary resource.
232 233 234 |
# File 'lib/spree/agent_tools/resource_map.rb', line 232 def typed_family typed_family_class(model_class) end |
#typed_family_class(klass) ⇒ Object
260 261 262 263 264 265 266 |
# File 'lib/spree/agent_tools/resource_map.rb', line 260 def typed_family_class(klass) return unless klass.respond_to?(:subclass_registry) && klass.subclass_registry klass rescue StandardError nil end |
#writable_attribute_names ⇒ Array<String>
The attributes the Admin API would accept on a write, so the generic tools offer exactly the same surface as the endpoint.
121 122 123 |
# File 'lib/spree/agent_tools/resource_map.rb', line 121 def writable_attribute_names Array(writable_attributes).map(&:to_s).sort end |