Skip to content

Commit

Permalink
Allow selecting cutoff mode for sites
Browse files Browse the repository at this point in the history
REDMINE-20674
  • Loading branch information
tf committed Jun 20, 2024
1 parent 5397680 commit f03712b
Show file tree
Hide file tree
Showing 12 changed files with 150 additions and 11 deletions.
1 change: 1 addition & 0 deletions admins/pageflow/accounts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion admins/pageflow/sites.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
12 changes: 12 additions & 0 deletions app/helpers/pageflow/admin/cutoff_modes_helper.rb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 8 additions & 1 deletion app/models/pageflow/site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
6 changes: 6 additions & 0 deletions app/views/admin/sites/_fields.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 : '<host>') %>
<%= f.input :sitemap_enabled, hint: t('pageflow.admin.sites.sitemap_hint',
Expand Down
8 changes: 8 additions & 0 deletions config/locales/new/cutoff_modes.de.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
de:
pageflow:
cutoff_modes:
none: "(Kein)"
activerecord:
attributes:
pageflow/site:
cutoff_mode_name: "Cutoff-Modus"
8 changes: 8 additions & 0 deletions config/locales/new/cutoff_modes.en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
en:
pageflow:
cutoff_modes:
none: "(None)"
activerecord:
attributes:
pageflow/site:
cutoff_mode_name: "Cutoff mode"
1 change: 1 addition & 0 deletions lib/pageflow/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions lib/pageflow/cutoff_modes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 40 additions & 1 deletion spec/controllers/admin/sites_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'spec_helper'
require 'pageflow/shared_contexts/fake_translations'

module Admin
describe SitesController do
Expand Down Expand Up @@ -161,6 +162,8 @@ def self.name
end

describe '#new' do
include_context 'fake translations'

it 'displays name input' do
account = create(:account)

Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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'
}
})

Expand All @@ -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
Expand Down
47 changes: 47 additions & 0 deletions spec/models/pageflow/site_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
19 changes: 11 additions & 8 deletions spec/pageflow/cutoff_modes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f03712b

Please sign in to comment.