Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supporting inherited model #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions lib/stateful_enum/machine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module StatefulEnum
class Machine
def initialize(model, column, states, prefix, suffix, &block)
@model, @column, @states, @event_names = model, column, states, []
@model, @column, @states, @event_names, @original_methods = model, column, states, [], {}
@prefix = if prefix == true
"#{column}_"
elsif prefix
Expand All @@ -17,6 +17,7 @@ def initialize(model, column, states, prefix, suffix, &block)

# undef non-verb methods e.g. Model#active!
states.each do |state|
@original_methods[state] = @model.instance_method "#{@prefix}#{state}#{@suffix}!"
@model.send :undef_method, "#{@prefix}#{state}#{@suffix}!"
end

Expand All @@ -25,12 +26,12 @@ def initialize(model, column, states, prefix, suffix, &block)

def event(name, &block)
raise ArgumentError, "event: :#{name} has already been defined." if @event_names.include? name
Event.new @model, @column, @states, @prefix, @suffix, name, &block
Event.new @model, @column, @states, @original_methods, @prefix, @suffix, name, &block
@event_names << name
end

class Event
def initialize(model, column, states, prefix, suffix, name, &block)
def initialize(model, column, states, original_methods, prefix, suffix, name, &block)
@states, @name, @transitions, @before, @after = states, name, {}, nil, nil

instance_eval(&block) if block
Expand All @@ -48,7 +49,7 @@ def initialize(model, column, states, prefix, suffix, name, &block)
if to && (!condition || instance_exec(&condition))
#TODO transaction?
instance_eval(&before) if before
original_method = self.class.send(:_enum_methods_module).instance_method "#{prefix}#{to}#{suffix}!"
original_method = original_methods[to]
ret = original_method.bind(self).call
instance_eval(&after) if after
ret
Expand Down
13 changes: 13 additions & 0 deletions test/dummy/app/models/post.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

class Post < ActiveRecord::Base
enum status: {drafted: 0, published: 1} do
event :draft do
transition :published => :drafted
end

event :publish do
transition :drafted => :published
end
end
end
4 changes: 4 additions & 0 deletions test/dummy/app/models/quote_post.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

class QuotePost < Post
end
14 changes: 14 additions & 0 deletions test/dummy/db/migrate/20160524075858_create_posts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

class CreatePosts < (Rails::VERSION::STRING >= '5' ? ActiveRecord::Migration[5.0] : ActiveRecord::Migration)
def change
create_table :posts do |t|
t.string :title
t.text :body
t.string :type
t.integer :status, default: 0

t.timestamps null: false
end
end
end
10 changes: 10 additions & 0 deletions test/mechanic_machine_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ def test_unless_condition
end
end

def test_inherited_model
post = Post.new
post.publish!
assert_equal 'published', post.status

quote_post = QuotePost.new
quote_post.publish!
assert_equal 'published', quote_post.status
end

def test_enum_definition_with_array
ActiveRecord::Migration.create_table(:array_enum_test) {|t| t.integer :col }
tes = Class.new(ActiveRecord::Base) do
Expand Down