From aec457e09a019edab7d28237df6100236457755a Mon Sep 17 00:00:00 2001 From: Roman Rizzi Date: Mon, 8 Apr 2019 14:42:36 -0300 Subject: [PATCH] DEV: Expose a way to extend a method that returns a list --- lib/plugin/instance.rb | 10 +++++++--- spec/components/plugin/instance_spec.rb | 17 ++++++++++++++--- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rb index 4a4d81ac9b3..2611f55688e 100644 --- a/lib/plugin/instance.rb +++ b/lib/plugin/instance.rb @@ -605,11 +605,15 @@ class Plugin::Instance end def register_reviewable_type(reviewable_type_class) - types = Reviewable.types - types << reviewable_type_class.name + extend_list_method Reviewable, :types, [reviewable_type_class.name] + end + + def extend_list_method(klass, method, new_attributes) + current_list = klass.send(method) + current_list.concat(new_attributes) reloadable_patch do - Reviewable.send(:define_singleton_method, :types) { types } + klass.send(:define_singleton_method, method) { current_list } end end diff --git a/spec/components/plugin/instance_spec.rb b/spec/components/plugin/instance_spec.rb index 02576337f4b..52cb47e180c 100644 --- a/spec/components/plugin/instance_spec.rb +++ b/spec/components/plugin/instance_spec.rb @@ -465,11 +465,22 @@ describe Plugin::Instance do describe '#register_reviewable_types' do it 'Overrides the existing Reviewable types adding new ones' do current_types = Reviewable.types - new_type_class = Class + new_type_class = Class - Plugin::Instance.new.register_reviewable_type new_type_class + Plugin::Instance.new.register_reviewable_type new_type_class - expect(Reviewable.types).to match_array(current_types << new_type_class.name) + expect(Reviewable.types).to match_array(current_types << new_type_class.name) + end + end + + describe '#extend_list_method' do + it 'Overrides the existing list appending new elements' do + current_list = Reviewable.types + new_element = Class.name + + Plugin::Instance.new.extend_list_method Reviewable, :types, [new_element] + + expect(Reviewable.types).to match_array(current_list << new_element) end end end