Class: AASM::Base
- Inherits:
-
Object
- Object
- AASM::Base
- Defined in:
- lib/aasm/persistence/base.rb,
lib/aasm/base.rb
Overview
Persistence
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#state_machine ⇒ Object
readonly
Returns the value of attribute state_machine.
Instance Method Summary collapse
- #after_all_events(*callbacks, &block) ⇒ Object
- #after_all_transactions(*callbacks, &block) ⇒ Object
- #after_all_transitions(*callbacks, &block) ⇒ Object
-
#attribute_name(column_name = nil) ⇒ Object
This method is both a getter and a setter.
- #before_all_events(*callbacks, &block) ⇒ Object
- #before_all_transactions(*callbacks, &block) ⇒ Object
- #ensure_on_all_events(*callbacks, &block) ⇒ Object
- #error_on_all_events(*callbacks, &block) ⇒ Object
-
#event(name, options = {}, &block) ⇒ Object
define an event.
- #events ⇒ Object
- #from_states_for_state(state, options = {}) ⇒ Object
-
#human_event_name(event) ⇒ Object
aasm.event(:event_name).human?.
- #initial_state(new_initial_state = nil) ⇒ Object
-
#initialize(klass, name, state_machine, options = {}, &block) ⇒ Base
constructor
A new instance of Base.
-
#state_with_scope(*args) ⇒ Object
(also: #state)
make sure to create a (named) scope for each state.
- #states ⇒ Object
- #states_for_select ⇒ Object
Constructor Details
#initialize(klass, name, state_machine, options = {}, &block) ⇒ Base
Returns a new instance of Base.
8 9 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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/aasm/base.rb', line 8 def initialize(klass, name, state_machine, ={}, &block) @klass = klass @name = name # @state_machine = klass.aasm(@name).state_machine @state_machine = state_machine @state_machine.config.column ||= ([:column] || default_column).to_sym # @state_machine.config.column = options[:column].to_sym if options[:column] # master @options = # let's cry if the transition is invalid configure :whiny_transitions, true # create named scopes for each state configure :create_scopes, true # don't store any new state if the model is invalid (in ActiveRecord) configure :skip_validation_on_save, false # raise if the model is invalid (in ActiveRecord) configure :whiny_persistence, false # Use transactions (in ActiveRecord) configure :use_transactions, true # use requires_new for nested transactions (in ActiveRecord) configure :requires_new_transaction, true # use pessimistic locking (in ActiveRecord) # true for FOR UPDATE lock # string for a specific lock type i.e. FOR UPDATE NOWAIT configure :requires_lock, false # automatically set `"#{state_name}_at" = ::Time.now` on state changes configure :timestamps, false # set to true to forbid direct assignment of aasm_state column (in ActiveRecord) configure :no_direct_assignment, false # allow a AASM::Base sub-class to be used for state machine configure :with_klass, AASM::Base configure :enum, nil # Set to true to namespace reader methods and constants configure :namespace, false # Configure a logger, with default being a Logger to STDERR configure :logger, Logger.new(STDERR) # setup timestamp-setting callback if enabled (@name) # make sure to raise an error if no_direct_assignment is enabled # and attribute is directly assigned though setup_no_direct_assignment(@name) end |
Instance Attribute Details
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
6 7 8 |
# File 'lib/aasm/base.rb', line 6 def klass @klass end |
#state_machine ⇒ Object (readonly)
Returns the value of attribute state_machine.
6 7 8 |
# File 'lib/aasm/base.rb', line 6 def state_machine @state_machine end |
Instance Method Details
#after_all_events(*callbacks, &block) ⇒ Object
161 162 163 |
# File 'lib/aasm/base.rb', line 161 def after_all_events(*callbacks, &block) @state_machine.add_global_callbacks(:after_all_events, *callbacks, &block) end |
#after_all_transactions(*callbacks, &block) ⇒ Object
149 150 151 |
# File 'lib/aasm/base.rb', line 149 def after_all_transactions(*callbacks, &block) @state_machine.add_global_callbacks(:after_all_transactions, *callbacks, &block) end |
#after_all_transitions(*callbacks, &block) ⇒ Object
145 146 147 |
# File 'lib/aasm/base.rb', line 145 def after_all_transitions(*callbacks, &block) @state_machine.add_global_callbacks(:after_all_transitions, *callbacks, &block) end |
#attribute_name(column_name = nil) ⇒ Object
This method is both a getter and a setter
66 67 68 69 70 71 72 73 |
# File 'lib/aasm/base.rb', line 66 def attribute_name(column_name=nil) if column_name @state_machine.config.column = column_name.to_sym else @state_machine.config.column ||= :aasm_state end @state_machine.config.column end |
#before_all_events(*callbacks, &block) ⇒ Object
157 158 159 |
# File 'lib/aasm/base.rb', line 157 def before_all_events(*callbacks, &block) @state_machine.add_global_callbacks(:before_all_events, *callbacks, &block) end |
#before_all_transactions(*callbacks, &block) ⇒ Object
153 154 155 |
# File 'lib/aasm/base.rb', line 153 def before_all_transactions(*callbacks, &block) @state_machine.add_global_callbacks(:before_all_transactions, *callbacks, &block) end |
#ensure_on_all_events(*callbacks, &block) ⇒ Object
169 170 171 |
# File 'lib/aasm/base.rb', line 169 def ensure_on_all_events(*callbacks, &block) @state_machine.add_global_callbacks(:ensure_on_all_events, *callbacks, &block) end |
#error_on_all_events(*callbacks, &block) ⇒ Object
165 166 167 |
# File 'lib/aasm/base.rb', line 165 def error_on_all_events(*callbacks, &block) @state_machine.add_global_callbacks(:error_on_all_events, *callbacks, &block) end |
#event(name, options = {}, &block) ⇒ Object
define an event
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/aasm/base.rb', line 111 def event(name, ={}, &block) @state_machine.add_event(name, , &block) aasm_name = @name.to_sym event = name.to_sym # an addition over standard aasm so that, before firing an event, you can ask # may_event? and get back a boolean that tells you whether the guard method # on the transition will let this happen. safely_define_method klass, "may_#{name}?", ->(*args) do aasm(aasm_name).may_fire_event?(event, *args) end safely_define_method klass, "#{name}!", ->(*args, &block) do aasm(aasm_name).current_event = :"#{name}!" aasm_fire_event(aasm_name, event, {:persist => true}, *args, &block) end safely_define_method klass, name, ->(*args, &block) do aasm(aasm_name).current_event = event aasm_fire_event(aasm_name, event, {:persist => false}, *args, &block) end skip_instance_level_validation(event, name, aasm_name, klass) # Create aliases for the event methods. Keep the old names to maintain backwards compatibility. if namespace? klass.send(:alias_method, "may_#{name}_#{namespace}?", "may_#{name}?") klass.send(:alias_method, "#{name}_#{namespace}!", "#{name}!") klass.send(:alias_method, "#{name}_#{namespace}", name) end end |
#events ⇒ Object
177 178 179 |
# File 'lib/aasm/base.rb', line 177 def events @state_machine.events.values end |
#from_states_for_state(state, options = {}) ⇒ Object
190 191 192 193 194 195 196 197 |
# File 'lib/aasm/base.rb', line 190 def from_states_for_state(state, ={}) if [:transition] @state_machine.events[[:transition]].transitions_to_state(state).flatten.map(&:from).flatten else events.map {|e| e.transitions_to_state(state)}.flatten.map(&:from).flatten end end |
#human_event_name(event) ⇒ Object
aasm.event(:event_name).human?
182 183 184 |
# File 'lib/aasm/base.rb', line 182 def human_event_name(event) # event_name? AASM::Localizer.new.human_event_name(klass, event) end |
#initial_state(new_initial_state = nil) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/aasm/base.rb', line 75 def initial_state(new_initial_state=nil) if new_initial_state @state_machine.initial_state = new_initial_state else @state_machine.initial_state end end |
#state_with_scope(*args) ⇒ Object Also known as: state
make sure to create a (named) scope for each state
60 61 62 63 64 65 |
# File 'lib/aasm/persistence/base.rb', line 60 def state_with_scope(*args) names = state_without_scope(*args) names.each do |name| create_scopes(name) end end |
#states ⇒ Object
173 174 175 |
# File 'lib/aasm/base.rb', line 173 def states @state_machine.states end |
#states_for_select ⇒ Object
186 187 188 |
# File 'lib/aasm/base.rb', line 186 def states_for_select states.map { |state| state.for_select } end |