Class: Spree::AgentTools::ResourceMap

Inherits:
Object
  • Object
show all
Defined in:
lib/spree/agent_tools/resource_map.rb

Overview

What the generic tools are allowed to look at.

Not a hand-written list: the Admin API's controllers already declare their model, serializer and permission scope, so the map is derived from them — spree_api registers one entry per admin resource controller after the classes load. Core owns the structure and the refusal below; a resource no controller serves is not reachable by any agent.

Permissions therefore mirror the controllers by construction rather than by convention: the catalog has no read_price_lists, and the price-lists controller rides read_products, so the derived entry does too.

Defined Under Namespace

Classes: Entry

Constant Summary collapse

UnscopedResourceError =

A registered resource whose model has no store scoping. Raised rather than quietly leaking another store's records.

Class.new(StandardError)
PARENT_SEGMENT =

The parent segment of a nested path: /promotions/:promotion_id/promotion_rules names promotions.

%r{/(?<parent>[a-z_]+)/:[a-z_]+_id(?:/|\z)}

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.deriver ⇒ #call?

How the map fills itself the first time it is read. spree_api sets this to its controller walk; core owns only the structure, so an installation without the API engine simply has an empty map.

Returns:

  • (#call, nil)


422
423
424
# File 'lib/spree/agent_tools/resource_map.rb', line 422

def deriver
  @deriver
end

Class Method Details

.all ⇒ Array<Entry>

Returns every registered resource, in registration order.

Returns:

  • (Array<Entry>) —

    every registered resource, in registration order



425
426
427
428
# File 'lib/spree/agent_tools/resource_map.rb', line 425

def all
  derive!
  entries.values
end

.available_for(context) ⇒ Array<Entry>

Resources this caller may read, so a tool description lists only what they can actually ask about.

Parameters:

Returns:



534
535
536
# File 'lib/spree/agent_tools/resource_map.rb', line 534

def available_for(context)
  all.select { |entry| entry.readable_by?(context) }
end

.derive! ⇒ void

This method returns an undefined value.

Fills the map once, from whatever deriver was registered.

Under a mutex, because the deriver rebuilds in place — it clears the map and registers each entry — so a second thread reading between those two moments would see an empty map and report every resource as unknown.

The flag is cleared again if the deriver raises: the map is empty by then, and leaving it marked derived would answer "unknown resource" for the rest of the process, since stale! only runs at boot and on reload. A failed derivation is retried by the next caller instead.



511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/spree/agent_tools/resource_map.rb', line 511

def derive!
  return if @derived || deriver.nil?

  derive_mutex.synchronize do
    return if @derived

    # Set before calling, so a deriver that reads the map while filling
    # it does not recurse.
    @derived = true
    begin
      deriver.call
    rescue StandardError
      @derived = false
      raise
    end
  end
end

.find(key) ⇒ Entry?

Parameters:

  • key (String)

Returns:



484
485
486
487
# File 'lib/spree/agent_tools/resource_map.rb', line 484

def find(key)
  derive!
  entries[key.to_s]
end

.register(key:, model_name:, permission:, serializer_name:, dashboard_path: nil, scope_name: nil, write_permission: nil, writable_attributes: [], create_workflow_key: nil, update_workflow_key: nil, distinct: nil, api_paths: nil, index_paths: nil, access_permissions: nil) ⇒ Entry

Registers one resource. Called by spree_api for each admin resource controller; an extension with its own admin controller is picked up by the same walk, so nothing registers by hand.

Re-registering a key replaces it, because the walk runs again on every code reload in development.

Parameters:

  • key (String, Symbol) —

    the resource key agents address it by

  • model_name (String)
  • permission (String) —

    the read_* key gating it

  • serializer_name (String)
  • dashboard_path (String, nil) (defaults to: nil) —

    a %<id>s format string

  • scope_name (Symbol, nil) (defaults to: nil) —

    narrows the model to a slice of itself

  • write_permission (String, nil) (defaults to: nil) —

    the write_* key, absent when the controller offers no writes

  • writable_attributes (Array<Symbol>) (defaults to: []) —

    what a write may set

  • create_workflow_key (String, nil) (defaults to: nil) —

    set when the controller declares a create workflow, which the generic write defers to

  • update_workflow_key (String, nil) (defaults to: nil) —

    likewise for updates

  • distinct (Boolean, nil) (defaults to: nil) —

    false where the controller disables DISTINCT on its own collection, which Postgres requires when the sort column is not in the select list

  • index_paths (Array<String>, nil) (defaults to: nil) —

    every collection path the resource is listed at, the nested ones under a parent included

  • access_permissions (Array<String>, nil) (defaults to: nil) —

    keys any one of which opens the resource, in place of permission

Returns:



457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# File 'lib/spree/agent_tools/resource_map.rb', line 457

def register(key:, model_name:, permission:, serializer_name:, dashboard_path: nil, scope_name: nil,
             write_permission: nil, writable_attributes: [], create_workflow_key: nil,
             update_workflow_key: nil, distinct: nil, api_paths: nil, index_paths: nil,
             access_permissions: nil)
  entry = Entry.new(key: key.to_s, model_name: model_name, permission: permission,
                    serializer_name: serializer_name, dashboard_path: dashboard_path,
                    scope_name: scope_name, write_permission: write_permission,
                    writable_attributes: writable_attributes,
                    create_workflow_key: create_workflow_key,
                    update_workflow_key: update_workflow_key,
                    distinct: distinct,
                    api_paths: api_paths&.to_a,
                    index_paths: index_paths,
                    access_permissions: access_permissions)
  entries[entry.key] = entry
end

.reset! ⇒ void

This method returns an undefined value.

Forgets every registration, so the derivation can run again from scratch on a code reload.



478
479
480
# File 'lib/spree/agent_tools/resource_map.rb', line 478

def reset!
  @entries = {}
end

.stale! ⇒ void

This method returns an undefined value.

Marks the map for rebuilding on its next read. Called on every code reload, because a controller edited in development changes what the map should say — but the rebuild itself waits for a reader.



494
495
496
# File 'lib/spree/agent_tools/resource_map.rb', line 494

def stale!
  @derived = false
end