Skip to content

Instantly share code, notes, and snippets.

@gudata
Last active May 4, 2017 10:04
Show Gist options
  • Save gudata/4eb9ad5890cf04b3551f0fa3358b8327 to your computer and use it in GitHub Desktop.
Save gudata/4eb9ad5890cf04b3551f0fa3358b8327 to your computer and use it in GitHub Desktop.
state tracking
state_machine :state, initial: :init do
before_transition any => any do |transition|
@status_before_transition = transition.state_name
end
after_transition any => any do |transition|
log = "#{@status_before_transition} -> #{transition.state_name}"
SystemEvent.create(trackable: transition, description: log, tag: "state_change")
end
CREATE TABLE system_events (
id integer NOT NULL,
tag character varying NOT NULL,
description text,
trackable_id integer,
trackable_type character varying,
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL
);
add in manage event tracking
class SystemEvent < ApplicationRecord
belongs_to :trackable, optional: true, polymorphic: true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment