From 2df388ffd7279ec7738691bf133ef2a63834610e Mon Sep 17 00:00:00 2001 From: Roman Rizzi Date: Thu, 2 Jul 2020 11:47:43 -0300 Subject: [PATCH] DEV: Plugins can extend ReviewableScore types. (#10156) --- app/models/reviewable_score.rb | 8 ++++++ lib/plugin/instance.rb | 3 ++- spec/components/plugin/instance_spec.rb | 35 +++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/app/models/reviewable_score.rb b/app/models/reviewable_score.rb index 13265826c85..208ea5b8fc5 100644 --- a/app/models/reviewable_score.rb +++ b/app/models/reviewable_score.rb @@ -21,6 +21,14 @@ class ReviewableScore < ActiveRecord::Base types end + def self.add_new_types(type_names) + next_id = types.values.max + 1 + + type_names.each_with_index do |name, idx| + @types[name] = next_id + idx + end + end + def self.statuses @statuses ||= Enum.new( pending: 0, diff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rb index 20419fe3ca3..711dd56c0c9 100644 --- a/lib/plugin/instance.rb +++ b/lib/plugin/instance.rb @@ -148,7 +148,7 @@ class Plugin::Instance end # Applies to all sites in a multisite environment. Ignores plugin.enabled? - def replace_flags(settings: ::FlagSettings.new) + def replace_flags(settings: ::FlagSettings.new, score_type_names: []) next_flag_id = ReviewableScore.types.values.max + 1 yield(settings, next_flag_id) @@ -156,6 +156,7 @@ class Plugin::Instance reloadable_patch do |plugin| ::PostActionType.replace_flag_settings(settings) ::ReviewableScore.reload_types + ::ReviewableScore.add_new_types(score_type_names) end end diff --git a/spec/components/plugin/instance_spec.rb b/spec/components/plugin/instance_spec.rb index 1c5bee70669..34e39055f42 100644 --- a/spec/components/plugin/instance_spec.rb +++ b/spec/components/plugin/instance_spec.rb @@ -553,4 +553,39 @@ describe Plugin::Instance do expect(custom_emoji.group).to eq("baz") end end + + describe '#replace_flags' do + let(:original_flags) { PostActionType.flag_settings } + + it 'adds a new flag' do + highest_flag_id = ReviewableScore.types.values.max + flag_name = :new_flag + + subject.replace_flags(settings: original_flags) do |settings, next_flag_id| + settings.add( + next_flag_id, + flag_name + ) + end + + expect(PostActionType.flag_settings.flag_types.keys).to include(flag_name) + expect(PostActionType.flag_settings.flag_types.values.max).to eq(highest_flag_id + 1) + end + + it 'adds a new score type after adding a new flag' do + highest_flag_id = ReviewableScore.types.values.max + new_score_type = :new_score_type + + subject.replace_flags(settings: original_flags, score_type_names: [new_score_type]) do |settings, next_flag_id| + settings.add( + next_flag_id, + :new_flag + ) + end + + expect(PostActionType.flag_settings.flag_types.values.max).to eq(highest_flag_id + 1) + expect(ReviewableScore.types.keys).to include(new_score_type) + expect(ReviewableScore.types.values.max).to eq(highest_flag_id + 2) + end + end end