Class: Statesman::Adapters::ActiveRecord
- Inherits:
-
Object
- Object
- Statesman::Adapters::ActiveRecord
- Defined in:
- lib/statesman/adapters/active_record.rb
Constant Summary collapse
- JSON_COLUMN_TYPES =
%w(json jsonb).freeze
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, options = {}) ⇒ ActiveRecord
constructor
A new instance of ActiveRecord.
- #last ⇒ Object
Constructor Details
#initialize(transition_class, parent_model, observer, options = {}) ⇒ ActiveRecord
Returns a new instance of ActiveRecord.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/statesman/adapters/active_record.rb', line 11 def initialize(transition_class, parent_model, observer, = {}) serialized = serialized?(transition_class) column_type = transition_class.columns_hash['metadata'].sql_type if !serialized && !JSON_COLUMN_TYPES.include?(column_type) raise UnserializedMetadataError, "#{transition_class.name}#metadata is not serialized" elsif serialized && JSON_COLUMN_TYPES.include?(column_type) raise IncompatibleSerializationError, "#{transition_class.name}#metadata column type cannot be json and serialized simultaneously" end @transition_class = transition_class @parent_model = parent_model @observer = observer @association_name = [:association_name] || @transition_class.table_name 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
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/statesman/adapters/active_record.rb', line 29 def create(from, to, = {}) from = from.to_s to = to.to_s create_transition(from, to, ) rescue ::ActiveRecord::RecordNotUnique => e if e..include?(@transition_class.table_name) && e..include?('sort_key') || e..include?('most_recent') raise TransitionConflictError, e. else raise end ensure @last_transition = nil end |
#history ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/statesman/adapters/active_record.rb', line 43 def history if transitions_for_parent.loaded? # Workaround for Rails bug which causes infinite loop when sorting # already loaded result set. Introduced in rails/rails@b097ebe transitions_for_parent.to_a.sort_by(&:sort_key) else transitions_for_parent.order(:sort_key) end end |
#last ⇒ Object
53 54 55 |
# File 'lib/statesman/adapters/active_record.rb', line 53 def last @last_transition ||= history.last end |