Skip to content

Instantly share code, notes, and snippets.

@peco8
Created August 10, 2016 05:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peco8/43619935a314e6465f12cb2f840168c4 to your computer and use it in GitHub Desktop.
Save peco8/43619935a314e6465f12cb2f840168c4 to your computer and use it in GitHub Desktop.
AASM basic
class Plan < ActiveRecord::Base
include AASM
aasm column: 'state' do
state :pending, initial: true
state :processing
state :finished
state :errored
event :process, after: Proc.new { start } do
transitions from: :pending, to: :processing
end
event :finish, after: Proc.new { finish } do
transitions from: :processing, to: :finished
end
event :fail do
transitions from: :processing, to: :errored
end
end
validates :stripe_id, uniqueness: true
def self.published(stripe_id)
plan = find_by(stripe_id: stripe_id)
plan.update(published: true)
end
# 特定のトランザクションが発生した際に以下のものがコールバックされる。
def start
puts 'started!!!'
end
def finish
self.finish
puts 'finished!!!'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment