From f03712be232c4f990d5db6e0076e95792b9ae09e Mon Sep 17 00:00:00 2001 From: Tim Fischbach Date: Wed, 19 Jun 2024 09:21:48 +0200 Subject: [PATCH] Allow selecting cutoff mode for sites REDMINE-20674 --- admins/pageflow/accounts.rb | 1 + admins/pageflow/sites.rb | 4 +- .../pageflow/admin/cutoff_modes_helper.rb | 12 +++++ app/models/pageflow/site.rb | 9 +++- app/views/admin/sites/_fields.html.erb | 6 +++ config/locales/new/cutoff_modes.de.yml | 8 ++++ config/locales/new/cutoff_modes.en.yml | 8 ++++ lib/pageflow/configuration.rb | 1 + lib/pageflow/cutoff_modes.rb | 5 ++ .../admin/sites_controller_spec.rb | 41 +++++++++++++++- spec/models/pageflow/site_spec.rb | 47 +++++++++++++++++++ spec/pageflow/cutoff_modes_spec.rb | 19 ++++---- 12 files changed, 150 insertions(+), 11 deletions(-) create mode 100644 app/helpers/pageflow/admin/cutoff_modes_helper.rb create mode 100644 config/locales/new/cutoff_modes.de.yml create mode 100644 config/locales/new/cutoff_modes.en.yml diff --git a/admins/pageflow/accounts.rb b/admins/pageflow/accounts.rb index 01261c6599..50c0ea4d64 100644 --- a/admins/pageflow/accounts.rb +++ b/admins/pageflow/accounts.rb @@ -67,6 +67,7 @@ module Pageflow end controller do + helper Pageflow::Admin::CutoffModesHelper helper Pageflow::Admin::FeaturesHelper helper Pageflow::Admin::FormHelper helper Pageflow::Admin::LocalesHelper diff --git a/admins/pageflow/sites.rb b/admins/pageflow/sites.rb index 84c355f1f1..6589974b93 100644 --- a/admins/pageflow/sites.rb +++ b/admins/pageflow/sites.rb @@ -29,11 +29,13 @@ module Pageflow :copyright_link_url, :copyright_link_label, :privacy_link_url, - :home_url + :home_url, + :cutoff_mode_name ] + permitted_admin_form_input_params end controller do + helper Pageflow::Admin::CutoffModesHelper helper Pageflow::Admin::FormHelper before_create do |site| diff --git a/app/helpers/pageflow/admin/cutoff_modes_helper.rb b/app/helpers/pageflow/admin/cutoff_modes_helper.rb new file mode 100644 index 0000000000..e24ede2d82 --- /dev/null +++ b/app/helpers/pageflow/admin/cutoff_modes_helper.rb @@ -0,0 +1,12 @@ +module Pageflow + module Admin + # @api private + module CutoffModesHelper + def cutoff_modes_collection(config) + config.cutoff_modes.names.map do |name| + [t(name, scope: 'pageflow.cutoff_modes'), name] + end + end + end + end +end diff --git a/app/models/pageflow/site.rb b/app/models/pageflow/site.rb index bb8b3d0465..a07a407c26 100644 --- a/app/models/pageflow/site.rb +++ b/app/models/pageflow/site.rb @@ -9,7 +9,8 @@ class Site < ApplicationRecord scope :with_home_url, -> { where.not(home_url: '') } scope :for_request, ->(request) { Pageflow.config.site_request_scope.call(all, request) } - validates :account, :presence => true + validates :account, presence: true + validates_inclusion_of :cutoff_mode_name, in: :available_cutoff_mode_names, allow_blank: true delegate :enabled_feature_names, to: :account @@ -67,5 +68,11 @@ def self.ransackable_attributes(_auth_object = nil) def self.ransackable_associations(_auth_object = nil) %w[account] end + + private + + def available_cutoff_mode_names + Pageflow.config_for(account).cutoff_modes.names + end end end diff --git a/app/views/admin/sites/_fields.html.erb b/app/views/admin/sites/_fields.html.erb index 4883ec8fc8..a84d18cb4c 100644 --- a/app/views/admin/sites/_fields.html.erb +++ b/app/views/admin/sites/_fields.html.erb @@ -12,6 +12,12 @@ <%= f.input :copyright_link_url %> <%= f.input :privacy_link_url %> + <% if cutoff_modes_collection(account_config).present? %> + <%= f.input(:cutoff_mode_name, + collection: cutoff_modes_collection(account_config), + include_blank: t('pageflow.cutoff_modes.none')) %> + <% end %> + <%= f.input :feeds_enabled, hint: t('pageflow.admin.sites.feeds_hint', site_host: @site&.persisted? ? @site.host : '') %> <%= f.input :sitemap_enabled, hint: t('pageflow.admin.sites.sitemap_hint', diff --git a/config/locales/new/cutoff_modes.de.yml b/config/locales/new/cutoff_modes.de.yml new file mode 100644 index 0000000000..9f7fbbd768 --- /dev/null +++ b/config/locales/new/cutoff_modes.de.yml @@ -0,0 +1,8 @@ +de: + pageflow: + cutoff_modes: + none: "(Kein)" + activerecord: + attributes: + pageflow/site: + cutoff_mode_name: "Cutoff-Modus" diff --git a/config/locales/new/cutoff_modes.en.yml b/config/locales/new/cutoff_modes.en.yml new file mode 100644 index 0000000000..a850f2bdc8 --- /dev/null +++ b/config/locales/new/cutoff_modes.en.yml @@ -0,0 +1,8 @@ +en: + pageflow: + cutoff_modes: + none: "(None)" + activerecord: + attributes: + pageflow/site: + cutoff_mode_name: "Cutoff mode" diff --git a/lib/pageflow/configuration.rb b/lib/pageflow/configuration.rb index a95d3d6940..851730bfb6 100644 --- a/lib/pageflow/configuration.rb +++ b/lib/pageflow/configuration.rb @@ -617,6 +617,7 @@ def enable_all_features delegate :widget_types, to: :config delegate :public_entry_cache_control_header=, to: :config delegate :additional_public_entry_headers, to: :config + delegate :cutoff_modes, to: :config delegate :for_entry_type, to: :config end diff --git a/lib/pageflow/cutoff_modes.rb b/lib/pageflow/cutoff_modes.rb index 399baef869..be5c7e326d 100644 --- a/lib/pageflow/cutoff_modes.rb +++ b/lib/pageflow/cutoff_modes.rb @@ -28,6 +28,11 @@ def enabled_for?(entry, request) !!@modes[entry.site.cutoff_mode_name]&.enabled&.call(entry, request) end + # @api private + def names + @modes.keys + end + # @api private Mode = Struct.new(:name, :enabled) end diff --git a/spec/controllers/admin/sites_controller_spec.rb b/spec/controllers/admin/sites_controller_spec.rb index ae2481052c..8edfea671f 100644 --- a/spec/controllers/admin/sites_controller_spec.rb +++ b/spec/controllers/admin/sites_controller_spec.rb @@ -1,4 +1,5 @@ require 'spec_helper' +require 'pageflow/shared_contexts/fake_translations' module Admin describe SitesController do @@ -161,6 +162,8 @@ def self.name end describe '#new' do + include_context 'fake translations' + it 'displays name input' do account = create(:account) @@ -182,6 +185,37 @@ def self.name expect(response.body).to have_selector('[name="site[custom_field]"]') end + + it 'does not display cutoff mode select by default' do + pageflow_configure do |config| + config.features.register('test_cutoff_mode') do |entry_type_config| + entry_type_config.cutoff_modes.register(:some_cutoff, proc { true }) + end + end + account = create(:account) + + sign_in(create(:user, :admin), scope: :user) + get(:new, params: {account_id: account}) + + expect(response.body).not_to have_select('Cutoff mode') + end + + it 'displays cutoff modes enabled for account' do + translation(I18n.locale, + 'pageflow.cutoff_modes.some_cutoff', + 'Some Cutoff Mode') + pageflow_configure do |config| + config.features.register('test_cutoff_mode') do |entry_type_config| + entry_type_config.cutoff_modes.register(:some_cutoff, proc { true }) + end + end + account = create(:account, with_feature: 'test_cutoff_mode') + + sign_in(create(:user, :admin), scope: :user) + get(:new, params: {account_id: account}) + + expect(response.body).to have_select('Cutoff mode', options: ['(None)', 'Some Cutoff Mode']) + end end describe '#edit' do @@ -210,6 +244,9 @@ def self.name describe '#create' do it 'sets attributes' do + pageflow_configure do |config| + config.cutoff_modes.register(:some_cutoff, proc { true }) + end account = create(:account) sign_in(create(:user, :admin), scope: :user) @@ -221,7 +258,8 @@ def self.name title: 'Second Site', sitemap_enabled: true, feeds_enabled: true, - imprint_link_url: 'http://example.com/new' + imprint_link_url: 'http://example.com/new', + cutoff_mode_name: 'some_cutoff' } }) @@ -231,6 +269,7 @@ def self.name expect(site.title).to eq('Second Site') expect(site.sitemap_enabled?).to eq(true) expect(site.feeds_enabled?).to eq(true) + expect(site.cutoff_mode_name).to eq('some_cutoff') end it 'creates root permalink directory' do diff --git a/spec/models/pageflow/site_spec.rb b/spec/models/pageflow/site_spec.rb index c952d6f38a..ef0b91fb46 100644 --- a/spec/models/pageflow/site_spec.rb +++ b/spec/models/pageflow/site_spec.rb @@ -76,6 +76,53 @@ module Pageflow end end + describe '#cutoff_mode_name' do + it 'is invalid if cutoff mode not registered' do + site = build(:site, cutoff_mode_name: 'unknown') + + site.valid? + + expect(site.errors).to include(:cutoff_mode_name) + end + + it 'is invalid if cutoff mode disabled for account' do + pageflow_configure do |config| + config.features.register('some_cutoff_mode') do |feature_config| + feature_config.cutoff_modes.register(:some, proc { true }) + end + end + + account = create(:account) + site = build(:site, account:, cutoff_mode_name: 'some') + + site.valid? + expect(site.errors).to include(:cutoff_mode_name) + end + + it 'is valid if cutoff mode registered' do + pageflow_configure do |config| + config.cutoff_modes.register(:some, proc { true }) + end + + site = build(:site, cutoff_mode_name: 'some') + + expect(site).to be_valid + end + + it 'is valid if cutoff mode enabled for account' do + pageflow_configure do |config| + config.features.register('some_cutoff_mode') do |feature_config| + feature_config.cutoff_modes.register(:some, proc { true }) + end + end + + account = create(:account, with_feature: 'some_cutoff_mode') + site = build(:site, account:, cutoff_mode_name: 'some') + + expect(site).to be_valid + end + end + describe '.with_home_url' do it 'includes site with home_url' do site = create(:site, home_url: 'http://home.example.com') diff --git a/spec/pageflow/cutoff_modes_spec.rb b/spec/pageflow/cutoff_modes_spec.rb index 4805213bd2..3247c3aebd 100644 --- a/spec/pageflow/cutoff_modes_spec.rb +++ b/spec/pageflow/cutoff_modes_spec.rb @@ -7,32 +7,35 @@ module Pageflow end it 'returns false by default' do - cutoff_modes = CutoffModes.new site = create(:site) entry = create(:published_entry, site:) - result = cutoff_modes.enabled_for?(entry, request) + result = Pageflow.config.cutoff_modes.enabled_for?(entry, request) expect(result).to eq(false) end it 'returns result from cutoff mode configured in site' do - cutoff_modes = CutoffModes.new - cutoff_modes.register(:test, proc { true }) - site = create(:site, cutoff_mode: 'test') + pageflow_configure do |config| + config.cutoff_modes.register(:test, proc { true }) + end + site = create(:site, cutoff_mode_name: 'test') entry = create(:published_entry, site:) - result = cutoff_modes.enabled_for?(entry, request) + result = Pageflow.config.cutoff_modes.enabled_for?(entry, request) expect(result).to eq(true) end it 'passes entry and request to registered proc' do - cutoff_modes = CutoffModes.new - site = create(:site, cutoff_mode: 'test') + pageflow_configure do |config| + config.cutoff_modes.register(:test, proc { true }) + end + site = create(:site, cutoff_mode_name: 'test') entry = create(:published_entry, site:) expect { |probe| + cutoff_modes = Pageflow.config.cutoff_modes cutoff_modes.register(:test, probe.to_proc) cutoff_modes.enabled_for?(entry, request) }.to yield_with_args(entry, request)