Skip to content

Commit

Permalink
Fix error when defining states with the same name as the state's mach…
Browse files Browse the repository at this point in the history
…ine in ActiveRecord, MongoMapper, and Mongoid integrations [pluginaweek#89 state:resolved]
  • Loading branch information
obrie committed Aug 5, 2011
1 parent f87e227 commit 8fb2846
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rdoc
@@ -1,5 +1,6 @@
== master

* Fix error when defining states with the same name as the state's machine in ActiveRecord, MongoMapper, and Mongoid integrations
* Fix machine state predicate not calling superclass implementation if defined after machine definition
* Generate warnings when defining a helper method more than once
* Fix multiple machines not being able to target the same attribute if all possible states aren't defined in each
Expand Down
2 changes: 1 addition & 1 deletion lib/state_machine/integrations/active_record.rb
Expand Up @@ -456,7 +456,7 @@ def create_scope(name, scope)
# breaks both ancestor lookups and defined?(super). Need to special-case
# the existence of query attribute methods.
def owner_class_ancestor_has_method?(scope, method)
scope == :instance && method == "#{name}?" || super
scope == :instance && method == "#{name}?" ? owner_class : super
end
end
end
Expand Down
7 changes: 0 additions & 7 deletions lib/state_machine/integrations/mongo_mapper.rb
Expand Up @@ -294,13 +294,6 @@ def create_without_scope(name)
def define_scope(name, scope)
lambda {|model, values| model.query.merge(model.query(scope.call(values)))}
end

# ActiveModel's use of method_missing / respond_to for attribute methods
# breaks both ancestor lookups and defined?(super). Need to special-case
# the existence of query attribute methods.
def owner_class_ancestor_has_method?(scope, method)
scope == :instance && method == "#{name}?" || super
end
end
end
end
7 changes: 0 additions & 7 deletions lib/state_machine/integrations/mongoid.rb
Expand Up @@ -379,13 +379,6 @@ def create_without_scope(name)
def define_scope(name, scope)
lambda {|model, values| model.criteria.where(scope.call(values))}
end

# ActiveModel's use of method_missing / respond_to for attribute methods
# breaks both ancestor lookups and defined?(super). Need to special-case
# the existence of query attribute methods.
def owner_class_ancestor_has_method?(scope, method)
scope == :instance && method == "#{name}?" || super
end
end
end
end
20 changes: 20 additions & 0 deletions test/unit/integrations/active_record_test.rb
Expand Up @@ -359,6 +359,26 @@ def test_should_not_define_attribute_predicate
end
end

class MachineWithConflictingStateNameTest < BaseTestCase
def setup
require 'stringio'
@original_stderr, $stderr = $stderr, StringIO.new

@model = new_model

@machine = StateMachine::Machine.new(@model)
@machine.state :state
end

def test_should_output_warning
assert_equal "Instance method \"state?\" is already defined in ActiveRecordTest::Foo, use generic helper instead.\n", $stderr.string
end

def teardown
$stderr = @original_stderr
end
end

class MachineWithColumnStateAttributeTest < BaseTestCase
def setup
@model = new_model
Expand Down
20 changes: 20 additions & 0 deletions test/unit/integrations/data_mapper_test.rb
Expand Up @@ -327,6 +327,26 @@ def test_should_not_define_attribute_predicate
end
end

class MachineWithConflictingStateNameTest < BaseTestCase
def setup
require 'stringio'
@original_stderr, $stderr = $stderr, StringIO.new

@resource = new_resource

@machine = StateMachine::Machine.new(@resource)
@machine.state :state
end

def test_should_output_warning
assert_equal "Instance method \"state?\" is already defined in DataMapperTest::Foo :state instance helpers, use generic helper instead.\n", $stderr.string
end

def teardown
$stderr = @original_stderr
end
end

class MachineWithColumnStateAttributeTest < BaseTestCase
def setup
@resource = new_resource
Expand Down
20 changes: 20 additions & 0 deletions test/unit/integrations/mongo_mapper_test.rb
Expand Up @@ -297,6 +297,26 @@ def test_should_not_define_attribute_predicate
end
end

class MachineWithConflictingStateNameTest < BaseTestCase
def setup
require 'stringio'
@original_stderr, $stderr = $stderr, StringIO.new

@model = new_model

@machine = StateMachine::Machine.new(@model)
@machine.state :state
end

def test_should_output_warning
assert_match /^Instance method "state\?" is already defined in .*, use generic helper instead\.\n$/, $stderr.string
end

def teardown
$stderr = @original_stderr
end
end

class MachineWithColumnStateAttributeTest < BaseTestCase
def setup
@model = new_model
Expand Down
20 changes: 20 additions & 0 deletions test/unit/integrations/mongoid_test.rb
Expand Up @@ -273,6 +273,26 @@ def test_should_not_define_attribute_predicate
end
end

class MachineWithConflictingStateNameTest < BaseTestCase
def setup
require 'stringio'
@original_stderr, $stderr = $stderr, StringIO.new

@model = new_model

@machine = StateMachine::Machine.new(@model)
@machine.state :state
end

def test_should_output_warning
assert_match /^Instance method "state\?" is already defined in .*, use generic helper instead\.\n$/, $stderr.string
end

def teardown
$stderr = @original_stderr
end
end

class MachineWithColumnStateAttributeTest < BaseTestCase
def setup
@model = new_model
Expand Down

0 comments on commit 8fb2846

Please sign in to comment.