Commit 68e94450 authored by Rémy Coutable's avatar Rémy Coutable

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

Signed-off-by: default avatarRémy Coutable <remy@rymai.me>
parent 0ac65b6c
module Gitlab module Gitlab
module View module View
module Presenter module Presenter
CannotOverrideMethodError = Class.new(StandardError)
module Base module Base
extend ActiveSupport::Concern extend ActiveSupport::Concern
......
...@@ -8,9 +8,11 @@ module Gitlab ...@@ -8,9 +8,11 @@ module Gitlab
@subject = subject @subject = subject
attributes.each do |key, value| attributes.each do |key, value|
unless subject.respond_to?(key) if subject.respond_to?(key)
define_singleton_method(key) { value } raise CannotOverrideMethodError.new("#{subject} already respond to #{key}!")
end end
define_singleton_method(key) { value }
end end
super(subject) super(subject)
......
...@@ -17,10 +17,9 @@ describe Gitlab::View::Presenter::Delegated do ...@@ -17,10 +17,9 @@ describe Gitlab::View::Presenter::Delegated do
expect(presenter.current_user).to eq('Jane Doe') expect(presenter.current_user).to eq('Jane Doe')
end end
it 'does not override the presentee attributes' do it 'raise an error if the presentee already respond to method' do
presenter = presenter_class.new(project, user: 'Jane Doe') expect { presenter_class.new(project, user: 'Jane Doe') }.
to raise_error Gitlab::View::Presenter::CannotOverrideMethodError
expect(presenter.user).to eq('John Doe')
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment