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. vendor 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 legacy admin renders the spree/admin/dashboard/setup_tasks/_<key> partial titled by the admin.store_setup_tasks.<key> translation (both resolved across engine paths, so extensions ship theirs by convention), and the React dashboard consumes tasks as SetupTask records via the Admin API.

Defined Under Namespace

Classes: Definition

Instance Method Summary collapse

Constructor Details

#initializeSetupTasks

Returns a new instance of SetupTasks.



49
50
51
# File 'app/models/spree/setup_tasks.rb', line 49

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:



60
61
62
63
# File 'app/models/spree/setup_tasks.rb', line 60

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

#find(key) ⇒ Object



71
72
73
# File 'app/models/spree/setup_tasks.rb', line 71

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



82
83
84
# File 'app/models/spree/setup_tasks.rb', line 82

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

#remove(key) ⇒ Definition?

Returns the removed task.

Parameters:

  • key (Symbol)

Returns:



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

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

#tasksArray<Definition>

Returns all tasks sorted by position.

Returns:

  • (Array<Definition>)

    all tasks sorted by position



76
77
78
# File 'app/models/spree/setup_tasks.rb', line 76

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