Module: SolidState::ClassMethods

Defined in:
lib/solidstate.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.state_transitionsObject



21
22
23
# File 'lib/solidstate.rb', line 21

def self.state_transitions
  @@state_transitions
end

.statesObject



17
18
19
# File 'lib/solidstate.rb', line 17

def self.states
  @@states
end

Instance Method Details

#states(*list, &block) ⇒ Object



10
11
12
13
14
15
16
17
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
# File 'lib/solidstate.rb', line 10

def states(*list, &block)
  raise "This is not a list of names" unless list.first.is_a?(String)
  list = list.collect(&:to_s)

  @@states = list
  @@state_transitions = {}

  def self.states
    @@states
  end

  def self.state_transitions
    @@state_transitions
  end

  if respond_to?(:validates_inclusion_of)
    validates_inclusion_of STATE_ATTRIBUTE, in: list
  end

  scope :with_state, lambda { |state|
    return query if state.blank?
    where(STATE_ATTRIBUTE => state)
  } if respond_to?(:scope)

  list.each do |s|
    scope(s, lambda { where(STATE_ATTRIBUTE => s) }) if respond_to?(:scope)

    define_method "#{s}?" do
      state.to_sym == s.to_sym
    end
  end

  yield if block_given?
end

#transitions(opts) ⇒ Object



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
# File 'lib/solidstate.rb', line 45

def transitions(opts)
  validate :ensure_valid_transition if respond_to?(:validate)

  from = opts.delete(:from) or raise ":from required"
  to   = opts.delete(:to)   or raise ":to required"
  to   = [to] unless to.is_a?(Array)

  # puts "From #{from} to #{to.join(' or ')}"
  to.each do |dest|

    state_transitions[from.to_sym] ||= []
    state_transitions[from.to_sym].push(dest.to_sym)

    define_method("#{dest}!") do
      unless set_state(dest.to_sym)
        raise InvalidTransition.new("Cannot transition from #{state} to #{dest}")
      end

      if !respond_to?(:valid?) or (valid? && save)
        send("once_#{dest}", from) if respond_to?("once_#{dest}")
        true
      else
        false
      end
    end
  end
end