Skip to content

Commit

Permalink
Added ability to not create scopes if you dont want/need them
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Oberholzer committed Jul 18, 2011
1 parent 1d2d3f3 commit 1bc801e
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/stateflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def stateflow(&block)
define_method "#{state_name}?" do
state_name == current_state.name
end
add_scope state
add_scope state if @machine.create_scopes?
end

@machine.events.keys.each do |key|
Expand Down
10 changes: 9 additions & 1 deletion lib/stateflow/machine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@ class Machine
attr_accessor :states, :initial_state, :events

def initialize(&machine)
@states, @events = Hash.new, Hash.new
@states, @events, @create_scopes = Hash.new, Hash.new, true
instance_eval(&machine)
end

def state_column(name = :state)
@state_column ||= name
end

def create_scopes(bool = false)
@create_scopes = bool
end

def create_scopes?
@create_scopes
end

private
def initial(name)
Expand Down
6 changes: 6 additions & 0 deletions lib/stateflow/persistence/none.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ module Stateflow
module Persistence
module None
extend ActiveSupport::Concern

module ClassMethods
def add_scope(state)
# do nothing
end
end

module InstanceMethods
attr_accessor :state
Expand Down
29 changes: 28 additions & 1 deletion spec/orm/activerecord_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@ def self.up
t.column :state, :string
t.column :name, :string
end


create_table :active_record_no_scope_robots, :force => true do |t|
t.column :state, :string
t.column :name, :string
end
end

def self.down
drop_table :active_record_robots
drop_table :active_record_protected_robots
drop_table :active_record_no_scope_robots
end
end

Expand Down Expand Up @@ -62,6 +67,23 @@ class ActiveRecordProtectedRobot < ActiveRecord::Base
end
end

class ActiveRecordNoScopeRobot < ActiveRecord::Base
include Stateflow

attr_protected :state

stateflow do
create_scopes false
initial :red

state :red, :green

event :change do
transitions :from => :red, :to => :green
end
end
end

describe Stateflow::Persistence::ActiveRecord do
before(:all) { TestMigration.up }
after(:all) { TestMigration.down }
Expand Down Expand Up @@ -184,6 +206,11 @@ class ActiveRecordProtectedRobot < ActiveRecord::Base
ActiveRecordRobot.should respond_to(:red)
ActiveRecordRobot.should respond_to(:green)
end

it "should not be added for each state" do
ActiveRecordNoScopeRobot.should_not respond_to(:red)
ActiveRecordNoScopeRobot.should_not respond_to(:green)
end

it "should behave like Mongoid scopes" do
2.times { ActiveRecordRobot.create(:state => "red") }
Expand Down
29 changes: 28 additions & 1 deletion spec/orm/mongoid_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,30 @@ class MongoRobot
end
end

class MongoNoScopeRobot
include Mongoid::Document
include Stateflow

field :state
field :name

stateflow do
create_scopes false
initial :red

state :red, :green

event :change do
transitions :from => :red, :to => :green
end
end
end

describe Stateflow::Persistence::Mongoid do
after { MongoRobot.collection.drop }
after do
MongoRobot.collection.drop
MongoNoScopeRobot.collection.drop
end

let(:robot) { MongoRobot.new }

Expand Down Expand Up @@ -135,6 +157,11 @@ class MongoRobot
MongoRobot.should respond_to(:red)
MongoRobot.should respond_to(:green)
end

it "should be not added for each state" do
MongoNoScopeRobot.should_not respond_to(:red)
MongoNoScopeRobot.should_not respond_to(:green)
end

it "should behave like Mongoid scopes" do
2.times { MongoRobot.create(:state => "red") }
Expand Down

0 comments on commit 1bc801e

Please sign in to comment.