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(from, to, metadata = {}) ⇒ Object
- #history ⇒ Object
-
#initialize(transition_class, parent_model, observer) ⇒ ActiveRecord
constructor
A new instance of ActiveRecord.
- #last ⇒ Object
Constructor Details
#initialize(transition_class, parent_model, observer) ⇒ ActiveRecord
Returns a new instance of ActiveRecord.
9 10 11 12 13 14 15 16 17 |
# File 'lib/statesman/adapters/active_record.rb', line 9 def initialize(transition_class, parent_model, observer) 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 @observer = observer 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(from, to, metadata = {}) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/statesman/adapters/active_record.rb', line 19 def create(from, to, = {}) transition = transitions_for_parent.build(to_state: to, sort_key: next_sort_key, metadata: ) ::ActiveRecord::Base.transaction do @observer.execute(:before, from, to, transition) transition.save! @last_transition = transition @observer.execute(:after, from, to, transition) @last_transition = nil end @observer.execute(:after_commit, from, to, transition) transition end |
#history ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/statesman/adapters/active_record.rb', line 36 def history if transitions_for_parent.loaded? transitions_for_parent.sort_by(&:sort_key) else transitions_for_parent.order(:sort_key) end end |
#last ⇒ Object
44 45 46 |
# File 'lib/statesman/adapters/active_record.rb', line 44 def last @last_transition ||= history.last end |