Class: Statesman::Adapters::ActiveRecord

Inherits:
Object
  • Object
show all
Defined in:
lib/statesman/adapters/active_record.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transition_class, parent_model, observer) ⇒ ActiveRecord

Returns a new instance of ActiveRecord.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/statesman/adapters/active_record.rb', line 9

def initialize(transition_class, parent_model, observer)
  serialized = serialized?(transition_class)
  column_type = transition_class.columns_hash['metadata'].sql_type
  if !serialized && column_type != 'json'
    raise UnserializedMetadataError,
          "#{transition_class.name}#metadata is not serialized"
  elsif serialized && column_type == 'json'
    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
end

Instance Attribute Details

#parent_modelObject (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_classObject (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



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/statesman/adapters/active_record.rb', line 25

def create(from, to,  = {})
  from = from.to_s
  to = to.to_s
  create_transition(from, to, )
rescue ::ActiveRecord::RecordNotUnique => e
  if e.message.include?('sort_key') &&
     e.message.include?(@transition_class.table_name)
    raise TransitionConflictError, e.message
  else raise
  end
ensure
  @last_transition = nil
end

#historyObject



39
40
41
42
43
44
45
46
47
# File 'lib/statesman/adapters/active_record.rb', line 39

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

#lastObject



49
50
51
# File 'lib/statesman/adapters/active_record.rb', line 49

def last
  @last_transition ||= history.last
end