Class: Progressive::State
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Progressive::State
- Defined in:
- lib/progressive/state.rb
Instance Method Summary collapse
- #current_state ⇒ Object
-
#default_event_context ⇒ Object
This will be available for all callbacks to get better context around event changes.
-
#loaded? ⇒ Boolean
If we try to access subject_type before it’s present, method_missing goes nuts.
- #method_missing(method_sym, *args, &block) ⇒ Object
- #specification ⇒ Object
- #specification? ⇒ Boolean
-
#to(event, options = {}) ⇒ Object
Public: Transition from the current state to a new state.
- #to_param ⇒ Object
- #to_s ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_sym, *args, &block) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/progressive/state.rb', line 26 def method_missing(method_sym, *args, &block) return super unless loaded? if specification.event?(method_sym) to(method_sym, *args) elsif method_sym.to_s[-1] == '?' && specification.state?(method_sym.to_s[0..-2]) predicate = method_sym.to_s[0..-2] state.to_sym == predicate.to_sym else super end end |
Instance Method Details
#current_state ⇒ Object
83 84 85 |
# File 'lib/progressive/state.rb', line 83 def current_state specification.states[state.to_sym] end |
#default_event_context ⇒ Object
This will be available for all callbacks to get better context around event changes.
Returns Hash.
43 44 45 |
# File 'lib/progressive/state.rb', line 43 def default_event_context {} end |
#loaded? ⇒ Boolean
If we try to access subject_type before it’s present, method_missing goes nuts.
Returns true if loaded, false if not.
22 23 24 |
# File 'lib/progressive/state.rb', line 22 def loaded? !read_attribute(:subject_type).nil? end |
#specification ⇒ Object
9 10 11 |
# File 'lib/progressive/state.rb', line 9 def specification Progressive.specifications[subject_type] end |
#specification? ⇒ Boolean
13 14 15 16 |
# File 'lib/progressive/state.rb', line 13 def specification? return false unless loaded? specification.present? end |
#to(event, options = {}) ⇒ Object
Public: Transition from the current state to a new state.
state - The event
Returns nothing.
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 |
# File 'lib/progressive/state.rb', line 52 def to(event, = {}) return false unless current_state.event?(event) new_record = !!.delete(:new_record) || subject.new_state_record_on_change? current_event = current_state.events[event] previous_event_context = subject.event_context subject.event_context = default_event_context.merge().merge({ from: state.to_sym, to: current_event.to }) subject.run_callbacks(:progress) do subject.run_callbacks(current_event.name) do if new_record record = dup record.state = current_event.to if record.save record else false end else update_attribute(:state, current_event.to) end end end ensure subject.event_context = previous_event_context end |
#to_param ⇒ Object
91 92 93 |
# File 'lib/progressive/state.rb', line 91 def to_param state end |
#to_s ⇒ Object
87 88 89 |
# File 'lib/progressive/state.rb', line 87 def to_s state end |