From 68e94450a06e043ecf58ede565060f41b61043f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Coutable?= Date: Tue, 24 Jan 2017 17:07:56 +0100 Subject: [PATCH] Raise Gitlab::View::Presenter::CannotOverrideMethodError if presentee already respond to method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rémy Coutable --- lib/gitlab/view/presenter/base.rb | 2 ++ lib/gitlab/view/presenter/delegated.rb | 6 ++++-- spec/lib/gitlab/view/presenter/delegated_spec.rb | 7 +++---- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/gitlab/view/presenter/base.rb b/lib/gitlab/view/presenter/base.rb index 83c8ba5c1cf..dbfe0941e4d 100644 --- a/lib/gitlab/view/presenter/base.rb +++ b/lib/gitlab/view/presenter/base.rb @@ -1,6 +1,8 @@ module Gitlab module View module Presenter + CannotOverrideMethodError = Class.new(StandardError) + module Base extend ActiveSupport::Concern diff --git a/lib/gitlab/view/presenter/delegated.rb b/lib/gitlab/view/presenter/delegated.rb index fd90f7c2bad..387ff0f5d43 100644 --- a/lib/gitlab/view/presenter/delegated.rb +++ b/lib/gitlab/view/presenter/delegated.rb @@ -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) diff --git a/spec/lib/gitlab/view/presenter/delegated_spec.rb b/spec/lib/gitlab/view/presenter/delegated_spec.rb index 8390188bfdc..e9d4af54389 100644 --- a/spec/lib/gitlab/view/presenter/delegated_spec.rb +++ b/spec/lib/gitlab/view/presenter/delegated_spec.rb @@ -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