class Statesman::Adapters::Sequel

Attributes

observer[R]
parent_model[R]
transition_class[R]

Public Class Methods

new(transition_class, parent_model, observer, options={}) click to toggle source
# File lib/statesman/adapters/sequel.rb, line 6
def initialize(transition_class, parent_model, observer, options={})
  @transition_class = transition_class
  @parent_model = parent_model
  @observer = observer
end

Public Instance Methods

create(from, to, metadata = {}) click to toggle source
# File lib/statesman/adapters/sequel.rb, line 12
def create(from, to, metadata = {})
  create_transition(from.to_s, to.to_s, metadata)
ensure
  @last_transition = nil
end
history() click to toggle source
# File lib/statesman/adapters/sequel.rb, line 18
def history
  history_dataset.all
end
last(force_reload: false) click to toggle source
# File lib/statesman/adapters/sequel.rb, line 22
def last(force_reload: false)
  @last_transition = nil if force_reload
  @last_transition ||= history_dataset.last
end

Private Instance Methods

create_transition(from, to, metadata) click to toggle source
# File lib/statesman/adapters/sequel.rb, line 41
def create_transition(from, to, metadata)
  transition = transition_class.new(
    to_state: to,
    sort_key: next_sort_key,
    metadata: metadata,
    most_recent: true,
    parent_model_foreign_key => @parent_model.pk
  )

  parent_model_class.db.transaction do
    @observer.execute(:before, from, to, transition)
    unset_old_most_recent
    transition.save
    @last_transition = transition
    @observer.execute(:after, from, to, transition)
  end

  @observer.execute(:after_commit, from, to, transition)

  transition
end
history_dataset() click to toggle source
# File lib/statesman/adapters/sequel.rb, line 29
def history_dataset
  transitions_for_parent.order(:sort_key)
end
next_sort_key() click to toggle source
# File lib/statesman/adapters/sequel.rb, line 37
def next_sort_key
  (last && last.sort_key + 10) || 10
end
parent_association_name() click to toggle source
# File lib/statesman/adapters/sequel.rb, line 71
def parent_association_name
  parent_model_class.table_name.to_s.singularize.to_sym
end
parent_model_class() click to toggle source
# File lib/statesman/adapters/sequel.rb, line 67
def parent_model_class
  @parent_model_class ||= @parent_model.class
end
parent_model_foreign_key() click to toggle source
# File lib/statesman/adapters/sequel.rb, line 75
def parent_model_foreign_key
  @transition_class.association_reflection(parent_association_name)[:key]
end
transition_table_name() click to toggle source
# File lib/statesman/adapters/sequel.rb, line 63
def transition_table_name
  @transition_table_name ||= @transition_class.table_name
end
transitions_for_parent() click to toggle source
# File lib/statesman/adapters/sequel.rb, line 33
def transitions_for_parent
  @parent_model.send("#{transition_table_name}_dataset")
end
unset_old_most_recent() click to toggle source
# File lib/statesman/adapters/sequel.rb, line 79
def unset_old_most_recent
  transitions_for_parent.update(most_recent: false)
end