Module: OrderStatus

Defined in:
lib/order_status.rb

Overview

off the top of my head possible states: current (i.e. user building order) ordered (i.e. user checkout) queued (first phase of user checkout) payment_pending (system) - to be defined in module payment_received (system) - to be defined in module orderd (system) in_process (being prepared by staff) altered (staff altered, awaiting user approval) alteration_approved (user approves staff alteration) cancelled (user or staff) fulfilled (staff) ready_to_be_shipped shipped (staff) - to be defined in module received (user) - to be defined in module completed (system or staff)

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/order_status.rb', line 18

def self.included(klass)
  klass.extend ClassMethods

  klass.class_eval do
    include Workflow

    workflow do
      state :current do
        # event :checkout, :transitions_to => :ordered
        event :checkout, :transitions_to => :in_process
      end

      # anticipating an interim step between checkout
      # and the system queueing the order for fulfillment
      # in the future, other events, such as "pay", "shipping_info_added"
      # might be added here
      # state :ordered do
      # event :queue, :transitions_to => :in_process
      # end

      state :in_process do
        event :cancel, :transition_to => :cancelled
        event :alter, :transitions_to => :user_review
        event :accept, :transitions_to => :accepted
        event :fulfilled_without_acceptance, :transitions_to => :ready
      end

      state :user_review do
        event :alteration_approve, :transitions_to => :in_process
      end
      
      state :accepted do
        event :fulfilled, :transitions_to => :ready
        event :complete, :transitions_to => :completed
      end

      state :ready do
        event :finish, :transitions_to => :completed
      end

      state :cancelled
      state :completed
    end

    # create a named_scope for each of our declared states
    workflow_spec.state_names.each do |name|
      scope_name = "with_state_#{name}".to_sym
      named_scope scope_name, :conditions => { :workflow_state => name.to_s }, :order => 'updated_at DESC'
    end

    # in(state_name)
    named_scope :in, lambda { |*args|
      options = args.last.is_a?(Hash) ? args.pop : Hash.new
      state = args.is_a?(Array) ? args.first : args

      if state == 'all'
        options
      else
        { :conditions => { :workflow_state => state.to_s }.merge(options) }
      end
    }

    def state_ok_to_delete_line_item?
      return false if %w(user_review cancelled completed ready).include?(workflow_state)
      true
    end

    # write methods named the same as your states
    # to handle things like notifications in your apps
    # when your order moves to that particular state 


    def new_note(note)
      # trigger transition to user_review state if note is added to ready or in_process order
      alter! if in_process? && note.user != user
    end
  end
end