Raise Gitlab::View::Presenter::CannotOverrideMethodError if presentee already respond to method

Signed-off-by: Rémy Coutable <remy@rymai.me>
This commit is contained in:
Rémy Coutable 2017-01-24 17:07:56 +01:00
parent 0ac65b6cc3
commit 68e94450a0
No known key found for this signature in database
GPG key ID: 46DF07E5CD9E96AB
3 changed files with 9 additions and 6 deletions

View file

@ -1,6 +1,8 @@
module Gitlab
module View
module Presenter
CannotOverrideMethodError = Class.new(StandardError)
module Base
extend ActiveSupport::Concern

View file

@ -8,9 +8,11 @@ module Gitlab
@subject = subject
attributes.each do |key, value|
unless subject.respond_to?(key)
define_singleton_method(key) { value }
if subject.respond_to?(key)
raise CannotOverrideMethodError.new("#{subject} already respond to #{key}!")
end
define_singleton_method(key) { value }
end
super(subject)

View file

@ -17,10 +17,9 @@ describe Gitlab::View::Presenter::Delegated do
expect(presenter.current_user).to eq('Jane Doe')
end
it 'does not override the presentee attributes' do
presenter = presenter_class.new(project, user: 'Jane Doe')
expect(presenter.user).to eq('John Doe')
it 'raise an error if the presentee already respond to method' do
expect { presenter_class.new(project, user: 'Jane Doe') }.
to raise_error Gitlab::View::Presenter::CannotOverrideMethodError
end
end