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.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/statesman/adapters/active_record.rb', line 20

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
  @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.



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

Class Method Details

.database_supports_partial_indexes?Boolean

Returns:

  • (Boolean)


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

def self.database_supports_partial_indexes?
  # Rails 3 doesn't implement `supports_partial_index?`
  if ::ActiveRecord::Base.connection.respond_to?(:supports_partial_index?)
    ::ActiveRecord::Base.connection.supports_partial_index?
  else
    ::ActiveRecord::Base.connection.adapter_name == "PostgreSQL"
  end
end

Instance Method Details

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



36
37
38
39
40
41
42
43
44
# File 'lib/statesman/adapters/active_record.rb', line 36

def create(from, to,  = {})
  create_transition(from.to_s, to.to_s, )
rescue ::ActiveRecord::RecordNotUnique => e
  raise TransitionConflictError, e.message if transition_conflict_error? e

  raise
ensure
  @last_transition = nil
end

#history(force_reload: false) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/statesman/adapters/active_record.rb', line 46

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



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

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