Class: Statesman::Adapters::ActiveRecord

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

Constant Summary collapse

JSON_COLUMN_TYPES =
%w[json jsonb].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transition_class, parent_model, observer, options = {}) ⇒ ActiveRecord

Returns a new instance of ActiveRecord.



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 initialize(transition_class, parent_model, observer, options = {})
  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
  elsif serialized && JSON_COLUMN_TYPES.include?(column_type)
    raise IncompatibleSerializationError, transition_class.name
  end

  @transition_class = transition_class
  @transition_table = transition_class.arel_table
  @parent_model = parent_model
  @observer = observer
  @association_name =
    options[:association_name] || @transition_class.table_name
end

Instance Attribute Details

#parent_modelObject (readonly)

Returns the value of attribute parent_model.



36
37
38
# File 'lib/statesman/adapters/active_record.rb', line 36

def parent_model
  @parent_model
end

#transition_classObject (readonly)

Returns the value of attribute transition_class.



36
37
38
# File 'lib/statesman/adapters/active_record.rb', line 36

def transition_class
  @transition_class
end

#transition_tableObject (readonly)

Returns the value of attribute transition_table.



36
37
38
# File 'lib/statesman/adapters/active_record.rb', line 36

def transition_table
  @transition_table
end

Class Method Details

.database_supports_partial_indexes?(model) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
# File 'lib/statesman/adapters/active_record.rb', line 10

def self.database_supports_partial_indexes?(model)
  # Rails 3 doesn't implement `supports_partial_index?`
  if model.connection.respond_to?(:supports_partial_index?)
    model.connection.supports_partial_index?
  else
    model.connection.adapter_name.casecmp("postgresql").zero?
  end
end

Instance Method Details

#create(from, to, metadata = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/statesman/adapters/active_record.rb', line 38

def create(from, to,  = {})
  create_transition(from.to_s, to.to_s, )
rescue ::ActiveRecord::RecordNotUnique => e
  if transition_conflict_error? e
    # The history has the invalid transition on the end of it, which means
    # `current_state` would then be incorrect. We force a reload of the history to
    # avoid this.
    transitions_for_parent.reload
    raise TransitionConflictError, e.message
  end

  raise
ensure
  reset
end

#history(force_reload: false) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/statesman/adapters/active_record.rb', line 54

def history(force_reload: false)
  if transitions_for_parent.loaded? && !force_reload
    # 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(force_reload: false) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/statesman/adapters/active_record.rb', line 64

def last(force_reload: false)
  if force_reload
    @last_transition = history(force_reload: true).last
  elsif instance_variable_defined?(:@last_transition)
    @last_transition
  else
    @last_transition = history.last
  end
end

#resetObject



74
75
76
77
78
# File 'lib/statesman/adapters/active_record.rb', line 74

def reset
  if instance_variable_defined?(:@last_transition)
    remove_instance_variable(:@last_transition)
  end
end