Skip to content

Commit

Permalink
Add the ability to ignore method conflicts for helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
obrie committed May 30, 2011
1 parent a58f5ee commit d3c130c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.rdoc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
== master

* Add the ability to ignore method conflicts for helpers
* Generate warnings for any helper, not just state helpers, that has a conflicting method defined in the class
* Fix scopes in Sequel not working if the table name contains double underscores or is not a string/symbol
* Add full support for chaining state scopes within Sequel integrations
Expand Down
7 changes: 6 additions & 1 deletion lib/state_machine/machine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,11 @@ class << self; attr_accessor :default_messages; end
:invalid_transition => 'cannot transition via "%s"'
}

# Whether to ignore any conflicts that are detected for helper methods that
# get generated for a machine's owner class. Default is false.
class << self; attr_accessor :ignore_method_conflicts; end
@ignore_method_conflicts = false

# The class that the machine is defined in
attr_accessor :owner_class

Expand Down Expand Up @@ -619,7 +624,7 @@ def define_helper(scope, method, *args, &block)
helper_module = @helper_modules.fetch(scope)

if block_given?
if conflicting_ancestor = owner_class_ancestor_has_method?(scope, method)
if !self.class.ignore_method_conflicts && conflicting_ancestor = owner_class_ancestor_has_method?(scope, method)
ancestor_name = conflicting_ancestor.name && !conflicting_ancestor.name.empty? ? conflicting_ancestor.name : conflicting_ancestor.to_s
warn "#{scope == :class ? 'Class' : 'Instance'} method \"#{method}\" is already defined in #{ancestor_name}, use generic helper instead."
else
Expand Down
40 changes: 40 additions & 0 deletions test/unit/machine_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1192,6 +1192,26 @@ def park
$stderr = @original_stderr
end

def test_should_define_if_ignoring_method_conflicts_and_defined_in_superclass
require 'stringio'
@original_stderr, $stderr = $stderr, StringIO.new
StateMachine::Machine.ignore_method_conflicts = true

superclass = Class.new do
def park
end
end
klass = Class.new(superclass)
machine = StateMachine::Machine.new(klass)

machine.define_helper(:instance, :park) {true}
assert_equal '', $stderr.string
assert_equal true, klass.new.park
ensure
StateMachine::Machine.ignore_method_conflicts = false
$stderr = @original_stderr
end

def test_should_define_nonexistent_methods
@machine.define_helper(:instance, :state) {'parked'}
assert_equal 'parked', @object.state
Expand Down Expand Up @@ -1342,6 +1362,26 @@ def park
$stderr = @original_stderr
end

def test_should_define_if_ignoring_method_conflicts_and_defined_in_superclass
require 'stringio'
@original_stderr, $stderr = $stderr, StringIO.new
StateMachine::Machine.ignore_method_conflicts = true

superclass = Class.new do
def self.park
end
end
klass = Class.new(superclass)
machine = StateMachine::Machine.new(klass)

machine.define_helper(:class, :park) {true}
assert_equal '', $stderr.string
assert_equal true, klass.park
ensure
StateMachine::Machine.ignore_method_conflicts = false
$stderr = @original_stderr
end

def test_should_define_nonexistent_methods
@machine.define_helper(:class, :states) {[]}
assert_equal [], @klass.states
Expand Down

0 comments on commit d3c130c

Please sign in to comment.