mirror of
https://github.com/aasm/aasm
synced 2023-03-27 23:22:41 -04:00
Silence method override warning when it's an enum
This commit is contained in:
parent
92a707e89d
commit
243a7f0888
2 changed files with 33 additions and 1 deletions
|
@ -193,7 +193,13 @@ module AASM
|
|||
end
|
||||
|
||||
def safely_define_method(klass, method_name, method_definition)
|
||||
if !@options[:enum] && klass.instance_methods.include?(method_name.to_sym)
|
||||
# Warn if method already exists and it did not originate from an enum
|
||||
if klass.instance_methods.include?(method_name.to_sym) &&
|
||||
! ( @state_machine.config.enum &&
|
||||
klass.respond_to?(:defined_enums) &&
|
||||
klass.defined_enums.values.any?{ |methods|
|
||||
methods.keys{| enum | enum + '?' == method_name }
|
||||
})
|
||||
warn "#{klass.name}: overriding method '#{method_name}'!"
|
||||
end
|
||||
|
||||
|
|
|
@ -12,6 +12,21 @@ describe 'warns when overrides a method' do
|
|||
end
|
||||
end
|
||||
|
||||
module WithEnumBase
|
||||
def self.included base
|
||||
base.send :include, AASM
|
||||
base.instance_eval do
|
||||
def defined_enums
|
||||
{ 'state' => { 'valid' => 0, 'invalid' => 1 } }
|
||||
end
|
||||
end
|
||||
base.aasm enum: true do
|
||||
state :valid
|
||||
event(:save) { }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'state' do
|
||||
class Base
|
||||
def valid?; end
|
||||
|
@ -22,6 +37,16 @@ describe 'warns when overrides a method' do
|
|||
end
|
||||
end
|
||||
|
||||
describe 'enum' do
|
||||
class EnumBase
|
||||
def valid?; end
|
||||
end
|
||||
it "dosn't warn when overriding an enum" do
|
||||
expect { EnumBase.send :include, WithEnumBase }.
|
||||
not_to output(/EnumBase: overriding method 'valid\?'!/).to_stderr
|
||||
end
|
||||
end
|
||||
|
||||
describe 'event' do
|
||||
context 'may?' do
|
||||
class Base
|
||||
|
@ -40,4 +65,5 @@ describe 'warns when overrides a method' do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue