Class: Spree::SetupTasks

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/setup_tasks.rb

Overview

Registry of onboarding tasks for a given subject type. Spree.store_setup_tasks holds the Getting Started checklist evaluated against a store; future registries (e.g. seller onboarding) instantiate the same class against their own subject. Defaults are registered in config/initializers/spree_store_setup_tasks.rb; extensions and host apps can add or remove tasks at boot:

Rails.application.config.after_initialize do
Spree.store_setup_tasks.add :connect_stripe,
  position: 25,
  done: ->(store) { store.payment_methods.active.exists?(type: 'Spree::PaymentMethod::Stripe') }

Spree.store_setup_tasks.remove :setup_taxes_collection
end

The registry holds domain data only — each frontend maps task keys to its own presentation: the React dashboard consumes tasks as SetupTask records via the Admin API.

Defined Under Namespace

Classes: Definition

Instance Method Summary collapse

Constructor Details

#initialize ⇒ SetupTasks

Returns a new instance of SetupTasks.



46
47
48
# File 'app/models/spree/setup_tasks.rb', line 46

def initialize
  @tasks = {}
end

Instance Method Details

#add(key, position:, done:, **options) ⇒ Definition

Registers a task (replacing any existing task with the same key).

Parameters:

  • key (Symbol) —

    identifies the task; frontends derive presentation from it

  • position (Integer) —

    sort order in the checklist

  • done (#call) —

    receives the subject, returns whether the task is complete

  • options (Hash) —

    optional :if (per-subject visibility callable)

Returns:



57
58
59
60
# File 'app/models/spree/setup_tasks.rb', line 57

def add(key, position:, done:, **options)
  key = key.to_sym
  @tasks[key] = Definition.new(key, position: position, done: done, **options)
end

#find(key) ⇒ Object



68
69
70
# File 'app/models/spree/setup_tasks.rb', line 68

def find(key)
  @tasks[key.to_sym]
end

#for(subject) ⇒ Array<Definition>

Returns the tasks applicable to the given subject, sorted by position.

Parameters:

  • subject (Object) —

    the record the checklist belongs to (e.g. a store)

Returns:

  • (Array<Definition>) —

    the tasks applicable to the given subject, sorted by position



79
80
81
# File 'app/models/spree/setup_tasks.rb', line 79

def for(subject)
  tasks.select { |task| task.available?(subject) }
end

#remove(key) ⇒ Definition?

Returns the removed task.

Parameters:

  • key (Symbol)

Returns:



64
65
66
# File 'app/models/spree/setup_tasks.rb', line 64

def remove(key)
  @tasks.delete(key.to_sym)
end

#tasks ⇒ Array<Definition>

Returns all tasks sorted by position.

Returns:

  • (Array<Definition>) —

    all tasks sorted by position



73
74
75
# File 'app/models/spree/setup_tasks.rb', line 73

def tasks
  @tasks.values.sort_by(&:position)
end