Sha256: d81c4fd53648d23c729d54d607d1101f445ff576bfda93ec2d9c3d1bd5e3c435

Contents?: true

Size: 1.28 KB

Versions: 2

Compression:

Stored size: 1.28 KB

Contents

require "support/active_record"
require "json"

DB = Pathname.new("test.sqlite3")

class MyStateMachine
  include Statesman::Machine

  state :initial, initial: true
  state :succeeded
  state :failed

  transition from: :initial, to: [:succeeded, :failed]
  transition from: :failed,  to: :initial
end

class MyActiveRecordModel < ActiveRecord::Base
  has_many :my_active_record_model_transitions

  def state_machine
    @state_machine ||= MyStateMachine
      .new(self, transition_class: MyActiveRecordModelTransition)
  end
end

class MyActiveRecordModelTransition < ActiveRecord::Base
  belongs_to :my_active_record_model
  serialize :metadata, JSON
end

class CreateMyActiveRecordModelMigration < ActiveRecord::Migration
  def change
    create_table :my_active_record_models do |t|
      t.string :current_state
      t.timestamps
    end
  end
end

# TODO: make this a module we can extend from the app? Or a generator?
class CreateMyActiveRecordModelTransitionMigration < ActiveRecord::Migration
  def change
    create_table :my_active_record_model_transitions do |t|
      t.string  :to_state
      t.integer :my_active_record_model_id
      t.integer :sort_key
      t.text    :metadata
      t.timestamps
    end

    add_index :my_active_record_model_transitions, :sort_key, unique: true
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
statesman-1.0.0.beta2 spec/support/active_record.rb
statesman-1.0.0.beta1 spec/support/active_record.rb