Class: Statesman::Adapters::ActiveRecord
- Inherits:
-
Object
- Object
- Statesman::Adapters::ActiveRecord
- Defined in:
- lib/statesman/adapters/active_record.rb
Instance Attribute Summary collapse
-
#parent_model ⇒ Object
readonly
Returns the value of attribute parent_model.
-
#transition_class ⇒ Object
readonly
Returns the value of attribute transition_class.
Instance Method Summary collapse
- #create(to, before_cbs, after_cbs, metadata = {}) ⇒ Object
- #history ⇒ Object
-
#initialize(transition_class, parent_model) ⇒ ActiveRecord
constructor
A new instance of ActiveRecord.
- #last ⇒ Object
Constructor Details
#initialize(transition_class, parent_model) ⇒ ActiveRecord
Returns a new instance of ActiveRecord.
9 10 11 12 13 14 15 16 |
# File 'lib/statesman/adapters/active_record.rb', line 9 def initialize(transition_class, parent_model) unless transition_class.serialized_attributes.include?("metadata") raise UnserializedMetadataError, "#{transition_class.name}#metadata is not serialized" end @transition_class = transition_class @parent_model = parent_model end |
Instance Attribute Details
#parent_model ⇒ Object (readonly)
Returns the value of attribute parent_model.
7 8 9 |
# File 'lib/statesman/adapters/active_record.rb', line 7 def parent_model @parent_model end |
#transition_class ⇒ Object (readonly)
Returns the value of attribute transition_class.
6 7 8 |
# File 'lib/statesman/adapters/active_record.rb', line 6 def transition_class @transition_class end |
Instance Method Details
#create(to, before_cbs, after_cbs, metadata = {}) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/statesman/adapters/active_record.rb', line 18 def create(to, before_cbs, after_cbs, = {}) transition = transitions_for_parent.build(to_state: to, sort_key: next_sort_key, metadata: ) ::ActiveRecord::Base.transaction do before_cbs.each { |cb| cb.call(@parent_model, transition) } transition.save! after_cbs.each { |cb| cb.call(@parent_model, transition) } @last_transition = nil end transition end |
#history ⇒ Object
33 34 35 |
# File 'lib/statesman/adapters/active_record.rb', line 33 def history transitions_for_parent.order(:sort_key) end |
#last ⇒ Object
37 38 39 |
# File 'lib/statesman/adapters/active_record.rb', line 37 def last @last_transition ||= transitions_for_parent.order(:sort_key).last end |