From 10fb8542b2b3d1294608b127e2a60b6716b7eec2 Mon Sep 17 00:00:00 2001 From: Tim Fischbach Date: Thu, 15 Oct 2026 12:54:56 +0200 Subject: [PATCH 1/3] Let users configure entry copies before duplication Clicking "Copy Story" used to create the copy right away and only then redirect to its edit form. Until it was renamed and moved, the copy was already visible to other users in the original account, site and folder. The button now leads to a form where title, permalink, account, site and folder can be chosen before anything is created. Users can also decide whether members of the original story should be copied or whether the copy starts without members. Members who do not belong to the target account are not copied. --- admins/pageflow/entry.rb | 61 +++-- .../stylesheets/pageflow/admin/forms.scss | 9 + app/helpers/pageflow/admin/entries_helper.rb | 5 + app/helpers/pageflow/admin/form_helper.rb | 4 + app/models/pageflow/entry_duplicate.rb | 94 ++++++-- app/views/admin/entries/_base_inputs.html.erb | 24 ++ .../admin/entries/_folder_input.html.erb | 6 + app/views/admin/entries/_form.html.erb | 34 +-- app/views/admin/entries/duplicate.html.erb | 46 ++++ config/locales/de.yml | 10 +- config/locales/en.yml | 10 +- .../admin/entries_controller_spec.rb | 214 +++++++++++++++++- .../duplicating_an_entry_spec.rb | 24 +- .../pageflow/admin/form_helper_spec.rb | 22 ++ spec/models/pageflow/entry_duplicate_spec.rb | 198 +++++++++++++++- spec/support/dominos/admin/entry_form.rb | 1 + 16 files changed, 695 insertions(+), 67 deletions(-) create mode 100644 app/views/admin/entries/_base_inputs.html.erb create mode 100644 app/views/admin/entries/_folder_input.html.erb create mode 100644 app/views/admin/entries/duplicate.html.erb create mode 100644 spec/helpers/pageflow/admin/form_helper_spec.rb diff --git a/admins/pageflow/entry.rb b/admins/pageflow/entry.rb index 2a359d929c..160a8459a6 100644 --- a/admins/pageflow/entry.rb +++ b/admins/pageflow/entry.rb @@ -147,14 +147,10 @@ module Pageflow action_item(:duplicate, only: :show, priority: 5) do if authorized?(:duplicate, entry) - button_to(I18n.t('pageflow.admin.entries.duplicate'), - duplicate_admin_entry_path(entry), - method: :post, - form_class: 'action_item duplicate', - data: { - rel: 'duplicate', - confirm: I18n.t('pageflow.admin.entries.confirm_duplicate') - }) + link_to(I18n.t('pageflow.admin.entries.duplicate'), + duplicate_admin_entry_path(entry), + class: 'duplicate', + data: {rel: 'duplicate'}) end end @@ -184,11 +180,23 @@ module Pageflow end end - member_action :duplicate, method: :post do - entry = Entry.find(params[:id]) - authorize!(:duplicate, entry) - new_entry = entry.duplicate - redirect_to(edit_admin_entry_path(new_entry)) + member_action :duplicate, method: [:get, :post] do + original_entry = Entry.find(params[:id]) + authorize!(:duplicate, original_entry) + + duplicate = EntryDuplicate.of( + original_entry, + attributes: permitted_duplicate_attributes(original_entry), + copy_memberships: params.dig(:duplicate, :copy_memberships) != '0' + ) + + if request.post? && duplicate.save + redirect_to(admin_entry_path(duplicate.new_entry)) + else + @page_title = I18n.t('pageflow.admin.entries.duplicate_form.title', + title: original_entry.title) + render(:duplicate, locals: {original_entry:, entry: duplicate.new_entry, duplicate:}) + end end member_action :snapshot, method: :post do @@ -364,6 +372,33 @@ def permitted_attributes # rubocop:todo Metrics/AbcSize, Metrics/CyclomaticCompl end # rubocop:enable Metrics/PerceivedComplexity + def permitted_duplicate_attributes(original_entry) + attributes = [:title, {permalink_attributes: [:slug, :directory_id]}] + attributes << :account_id if moving_duplicate_to_other_account_allowed?(original_entry) + attributes << :folder_id if authorized?(:configure_folder_for, original_entry) + + if authorized?(:update_site_on, original_entry) && site_params_present? && + site_in_allowed_sites_for?(duplicate_target_account(original_entry)) + attributes << :site_id + end + + params.fetch(:entry, {}).permit(attributes) + end + + def moving_duplicate_to_other_account_allowed?(original_entry) + account_params_present? && + authorized?(:update_account_on, original_entry) && + account_policy_scope.entry_movable.include?(Account.find(params[:entry][:account_id])) + end + + def duplicate_target_account(original_entry) + if moving_duplicate_to_other_account_allowed?(original_entry) + Account.find(params[:entry][:account_id]) + else + original_entry.account + end + end + def permit_feature_states(attributes) return unless params[:id] && authorized?(:update_feature_configuration_on, resource) diff --git a/app/assets/stylesheets/pageflow/admin/forms.scss b/app/assets/stylesheets/pageflow/admin/forms.scss index acd05cb2d3..4931a5ba75 100644 --- a/app/assets/stylesheets/pageflow/admin/forms.scss +++ b/app/assets/stylesheets/pageflow/admin/forms.scss @@ -9,6 +9,15 @@ padding: 5px 10px; } + .choice_title { + font-weight: bold; + + &::after { + content: " –"; + font-weight: normal; + } + } + fieldset.actions .link_action { padding: 5px; diff --git a/app/helpers/pageflow/admin/entries_helper.rb b/app/helpers/pageflow/admin/entries_helper.rb index 8dc2f88abb..57662d4c47 100644 --- a/app/helpers/pageflow/admin/entries_helper.rb +++ b/app/helpers/pageflow/admin/entries_helper.rb @@ -78,6 +78,11 @@ def entry_comments_tooltip(summary) t("#{scope}.tooltip", summary: parts.join(', ')) end + def account_input_shown_for?(entry) + authorized?(:update_account_on, entry) && + account_policy_scope.entry_creatable.many? + end + def entry_type_collection(entry_types = Pageflow.config.entry_types) entry_types.map(&:name).index_by do |type| I18n.t(type, scope: 'activerecord.values.pageflow/entry.type_names') diff --git a/app/helpers/pageflow/admin/form_helper.rb b/app/helpers/pageflow/admin/form_helper.rb index e44a27b9a8..8a2e4b48ec 100644 --- a/app/helpers/pageflow/admin/form_helper.rb +++ b/app/helpers/pageflow/admin/form_helper.rb @@ -9,6 +9,10 @@ module FormHelper # rubocop:todo Style/Documentation def admin_form_for(resource, options = {}, &) semantic_form_for(resource, options.merge(builder: ActiveAdmin::FormBuilder), &) end + + def choice_label_with_description(title, description) + safe_join([content_tag(:span, title, class: 'choice_title'), ' ', description]) + end end end end diff --git a/app/models/pageflow/entry_duplicate.rb b/app/models/pageflow/entry_duplicate.rb index 12dcc4b2ca..97e8fcb939 100644 --- a/app/models/pageflow/entry_duplicate.rb +++ b/app/models/pageflow/entry_duplicate.rb @@ -1,33 +1,80 @@ module Pageflow - EntryDuplicate = Struct.new(:original_entry) do - def create! - ActiveRecord::Base.transaction do - create_entry + # @api private + class EntryDuplicate + attr_reader :original_entry, :new_entry + + def self.of(entry, attributes: {}, copy_memberships: true) + new(entry, attributes:, copy_memberships:) + end + + def initialize(original_entry, attributes: {}, copy_memberships: true) + @original_entry = original_entry + @copy_memberships = copy_memberships + @new_entry = build_entry(attributes) + end + + def copy_memberships? + @copy_memberships + end + + def save + passed_slug = new_entry.permalink&.slug + + saved = ActiveRecord::Base.transaction do + assign_default_permalink_slug + next false unless new_entry.save - copy_permalink copy_draft - copy_memberships + copy_memberships if copy_memberships? + + true end - new_entry + new_entry.permalink.slug = passed_slug if !saved && new_entry.permalink + saved end - def self.of(entry) - new(entry) + def create! + raise ActiveRecord::RecordInvalid, new_entry unless save + + new_entry end private - attr_reader :new_entry + def build_entry(attributes) + Entry.new(default_attributes).tap do |entry| + entry.assign_attributes(attributes) + + entry.site = entry.account.default_site if entry.site.account_id != entry.account_id + entry.folder = nil if entry.folder && entry.folder.account_id != entry.account_id + + build_permalink_in_original_directory(entry) unless entry.permalink + end + end + + def build_permalink_in_original_directory(entry) + directory = original_entry.permalink&.directory + return unless directory && directory.site_id == entry.site_id - def create_entry - @new_entry = Entry.create!(new_attributes) + entry.build_permalink(directory:) end - def copy_permalink - return if original_entry.permalink.blank? + def assign_default_permalink_slug + permalink = new_entry.permalink + return if !permalink&.directory || permalink.slug.present? + + base_slug = new_entry.default_permalink_slug + return if base_slug.blank? + + permalink.slug = unique_slug(permalink.directory, base_slug) + end - new_entry.create_permalink!(directory: original_entry.permalink.directory) + def unique_slug(directory, base_slug) + (1..) + .lazy + .map { |index| index == 1 ? base_slug : "#{base_slug}-#{index}" } + .find { |slug| !directory.permalinks.exists?(slug:) } end def copy_draft @@ -37,17 +84,24 @@ def copy_draft end def copy_memberships - original_entry.memberships.each do |membership| - Membership.create(user: membership.user, role: membership.role.to_sym, entity: new_entry) + memberships_of_target_account_members.each do |membership| + Membership.create!(user: membership.user, role: membership.role.to_sym, entity: new_entry) end end - def new_attributes + def memberships_of_target_account_members + original_entry + .memberships + .where(user_id: new_entry.account.memberships.select(:user_id)) + end + + def default_attributes { type_name: original_entry.type_name, - title: new_title, + title: default_title, account: original_entry.account, site: original_entry.site, + folder: original_entry.folder, features_configuration: original_entry.features_configuration, perma_id_counter: original_entry.perma_id_counter, @@ -55,7 +109,7 @@ def new_attributes } end - def new_title + def default_title I18n.t('pageflow.entry.duplicated_title', title: original_entry.title) end end diff --git a/app/views/admin/entries/_base_inputs.html.erb b/app/views/admin/entries/_base_inputs.html.erb new file mode 100644 index 0000000000..c76043c844 --- /dev/null +++ b/app/views/admin/entries/_base_inputs.html.erb @@ -0,0 +1,24 @@ +<%= form.input(:title, + hint: entry.site.permalink_directories.blank? && + I18n.t('pageflow.admin.entries.title_hint')) %> + +<% if account_input_shown_for?(entry) %> + <%= form.input(:account, + as: :searchable_select, + include_blank: false, + ajax: { + resource: Pageflow::Entry, + collection_name: :eligible_accounts + }, + input_html: {class: 'entry_account_input'}) %> +<%end %> + +<%= render('admin/entries/site_input', + entry:, + form:) %> + +<%= form.fields_for(:permalink, permalink) do |permalink_form| %> + <%= render('admin/entries/permalink_inputs', + entry:, + form: permalink_form) %> +<% end %> diff --git a/app/views/admin/entries/_folder_input.html.erb b/app/views/admin/entries/_folder_input.html.erb new file mode 100644 index 0000000000..70c1e33ecd --- /dev/null +++ b/app/views/admin/entries/_folder_input.html.erb @@ -0,0 +1,6 @@ +<% if authorized?(:configure_folder_for, entry) %> + <% folder_collection = collection_for_folders(entry.account, entry.folder) %> + <%= form.input(:folder, + collection: folder_collection, + include_blank: true) unless folder_collection.empty? %> +<% end %> diff --git a/app/views/admin/entries/_form.html.erb b/app/views/admin/entries/_form.html.erb index e998b71a07..096010e895 100644 --- a/app/views/admin/entries/_form.html.erb +++ b/app/views/admin/entries/_form.html.erb @@ -1,44 +1,20 @@ <%= admin_form_for([:admin, resource], data: {root_permalink: @entry.persisted? && @entry&.permalink&.slug == ''}) do |f| %> <%= f.inputs do %> - <%= f.input(:title, - hint: @entry.site.permalink_directories.blank? && - I18n.t('pageflow.admin.entries.title_hint')) %> - - <% if authorized?(:update_account_on, resource) && - account_policy_scope.entry_creatable.many? %> - <%= f.input(:account, - as: :searchable_select, - include_blank: false, - ajax: { - resource: Pageflow::Entry, - collection_name: :eligible_accounts - }, - input_html: {class: 'entry_account_input'}) %> - <%end %> - - <%= render('admin/entries/site_input', + <%= render('admin/entries/base_inputs', entry: resource, + permalink: @entry.permalink || Pageflow::Permalink.new, form: f) %> - <%= f.fields_for(:permalink, @entry.permalink || Pageflow::Permalink.new) do |permalink| %> - <%= render('admin/entries/permalink_inputs', - entry: resource, - form: permalink) %> - <% end %> - <% if resource.new_record? %> <%= render('pageflow/admin/entries/entry_type_name_input', form: f, entry_types: Pageflow.config_for(resource.account).entry_types) %> <% end %> - <% if authorized?(:configure_folder_for, resource) %> - <% folder_collection = collection_for_folders(resource.account, resource.folder) %> - <%= f.input(:folder, - collection: folder_collection, - include_blank: true) unless folder_collection.empty? %> - <% end %> + <%= render('admin/entries/folder_input', + entry: resource, + form: f) %> <% entry_config = Pageflow.config_for(resource) %> <% entry_config.admin_form_inputs.find_all_for(:entry).each do |form_input| %> diff --git a/app/views/admin/entries/duplicate.html.erb b/app/views/admin/entries/duplicate.html.erb new file mode 100644 index 0000000000..252e6e802b --- /dev/null +++ b/app/views/admin/entries/duplicate.html.erb @@ -0,0 +1,46 @@ +<%= admin_form_for(entry, + as: :entry, + url: duplicate_admin_entry_path(original_entry), + method: :post, + html: {class: 'pageflow_entry'}) do |f| %> + <%= f.semantic_errors(:type_name) %> + + <%= f.inputs do %> + <%= render('admin/entries/base_inputs', + entry:, + permalink: entry.permalink || Pageflow::Permalink.new, + form: f) %> + + <%= render('admin/entries/folder_input', + entry:, + form: f) %> + + <% if original_entry.memberships.any? %> + <%= semantic_fields_for(:duplicate, + OpenStruct.new(copy_memberships: duplicate.copy_memberships? ? '1' : '0'), + builder: ActiveAdmin::FormBuilder) do |duplicate_form| %> + <%= duplicate_form.input(:copy_memberships, + as: :radio, + required: false, + label: t('pageflow.admin.entries.duplicate_form.memberships'), + hint: account_input_shown_for?(entry) && + t('pageflow.admin.entries.duplicate_form.memberships_hint'), + collection: [ + [choice_label_with_description( + t('pageflow.admin.entries.duplicate_form.copy_memberships'), + t('pageflow.admin.entries.duplicate_form.copy_memberships_description') + ), '1'], + [choice_label_with_description( + t('pageflow.admin.entries.duplicate_form.skip_memberships'), + t('pageflow.admin.entries.duplicate_form.skip_memberships_description') + ), '0'] + ]) %> + <% end %> + <% end %> + <% end %> + + <%= f.actions do %> + <%= f.action(:submit, label: t('pageflow.admin.entries.duplicate_form.submit')) %> + <%= f.action(:cancel, as: :link, url: admin_entry_path(original_entry)) %> + <% end %> +<% end %> diff --git a/config/locales/de.yml b/config/locales/de.yml index 5c2dd84364..47c43e9026 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -901,10 +901,18 @@ de: one: 1 ungelesenes Thema other: "%{count} ungelesene Themen" confirm_depublish: Soll der Beitrag wirklich depubliziert werden? - confirm_duplicate: Beitrag wirklich duplizieren? confirm_restore: Soll der Beitrag wirklich auf den Stand dieser Revision zurückgesetzt werden? Vor dem Zurücksetzen wird eine automatische Sicherung des aktuellen Standes erstellt. depublish: Beitrag depublizieren duplicate: Beitrag kopieren + duplicate_form: + copy_memberships: Mitglieder übernehmen + copy_memberships_description: Alle, die Zugriff auf den ursprünglichen Beitrag haben, erhalten mit derselben Rolle auch Zugriff auf die Kopie. + memberships: Mitglieder + memberships_hint: Mitglieder ohne Mitgliedschaft im ausgewählten Account werden nicht übernommen. + skip_memberships: Ohne Mitglieder beginnen + skip_memberships_description: Nur Benutzer mit einer Rolle im Account können auf die Kopie zugreifen. + submit: Beitrag kopieren + title: „%{title}“ kopieren edit: Bearbeiten edit_config: Konfiguration ändern edit_revision: Ändern diff --git a/config/locales/en.yml b/config/locales/en.yml index 8a62318cac..67eb575e6c 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -897,10 +897,18 @@ en: one: 1 unread topic other: "%{count} unread topics" confirm_depublish: Depublish this story? - confirm_duplicate: Duplicate this story? confirm_restore: Restore story to the selected version? A snapshot will be created, so that you can roll back later. depublish: Depublish story duplicate: Copy Story + duplicate_form: + copy_memberships: Copy members + copy_memberships_description: Everyone with access to the original story also gets access to the copy, with the same role. + memberships: Members + memberships_hint: Members without a membership in the selected account are not copied. + skip_memberships: Start without members + skip_memberships_description: Only users with a role on the account can access the copy. + submit: Copy Story + title: Copy "%{title}" edit: Edit edit_config: Edit configuration edit_revision: Edit diff --git a/spec/controllers/admin/entries_controller_spec.rb b/spec/controllers/admin/entries_controller_spec.rb index eb72fc2335..a5d610731c 100644 --- a/spec/controllers/admin/entries_controller_spec.rb +++ b/spec/controllers/admin/entries_controller_spec.rb @@ -1718,7 +1718,73 @@ def self.name end end - describe '#duplicate' do + describe 'get #duplicate' do + it 'renders form with default title and does not create entry' do + user = create(:user) + entry = create(:entry, title: 'Some Title', with_publisher: user) + + sign_in(user, scope: :user) + + expect { + get(:duplicate, params: {id: entry.id}) + }.not_to(change { Pageflow::Entry.count }) + expect(response.body).to have_field('entry_title', with: 'Copy of Some Title') + end + + it 'does not allow entry editor to see form' do + user = create(:user) + entry = create(:entry, with_editor: user) + + sign_in(user, scope: :user) + get(:duplicate, params: {id: entry.id}) + + expect(response).to redirect_to(admin_root_path) + end + + it 'offers choice whether to copy memberships' do + entry = create(:entry, with_editor: create(:user)) + + sign_in(create(:user, :admin), scope: :user) + get(:duplicate, params: {id: entry.id}) + + expect(response.body).to have_field('duplicate_copy_memberships_0') + end + + it 'hints that members are only copied to accounts they belong to if account can change' do + create(:account) + entry = create(:entry, with_editor: create(:user)) + + sign_in(create(:user, :admin), scope: :user) + get(:duplicate, params: {id: entry.id}) + + expect(response.body).to have_text( + I18n.t('pageflow.admin.entries.duplicate_form.memberships_hint') + ) + end + + it 'does not show memberships hint if account cannot change' do + account = create(:account) + entry = create(:entry, account:, with_editor: create(:user)) + + sign_in(create(:user, :publisher, on: account), scope: :user) + get(:duplicate, params: {id: entry.id}) + + expect(response.body).not_to have_text( + I18n.t('pageflow.admin.entries.duplicate_form.memberships_hint') + ) + end + + it 'does not offer choice whether to copy memberships if entry has no members' do + entry = create(:entry) + + sign_in(create(:user, :admin), scope: :user) + get(:duplicate, params: {id: entry.id}) + + expect(response.body).not_to have_field('duplicate_copy_memberships_0') + end + end + + describe 'post #duplicate' do it 'does not allow account editor to duplicate own entries of own account' do user = create(:user) account = create(:account, with_editor: user) @@ -1752,6 +1818,152 @@ def self.name post(:duplicate, params: {id: entry.id}) }.to(change { Pageflow::Entry.count }) end + + it 'redirects to admin page of new entry' do + user = create(:user) + entry = create(:entry, with_publisher: user) + + sign_in(user, scope: :user) + post(:duplicate, params: {id: entry.id}) + + expect(response).to redirect_to(admin_entry_path(Pageflow::Entry.last)) + end + + it 'uses passed title and permalink slug' do + user = create(:user) + entry = create(:entry, + with_publisher: user, + permalink_attributes: {slug: 'some-slug', directory_path: 'en/'}) + + sign_in(user, scope: :user) + post(:duplicate, + params: { + id: entry.id, + entry: { + title: 'Custom Title', + permalink_attributes: { + slug: 'custom-slug', + directory_id: entry.permalink.directory_id + } + } + }) + + expect(Pageflow::Entry.last).to have_attributes(title: 'Custom Title') + expect(Pageflow::Entry.last.permalink).to have_attributes(slug: 'custom-slug') + end + + it 'copies memberships by default' do + user = create(:user) + entry = create(:entry, with_publisher: user) + + sign_in(user, scope: :user) + post(:duplicate, params: {id: entry.id}) + + expect(Pageflow::Entry.last.users).to include(user) + end + + it 'supports skipping memberships' do + user = create(:user) + entry = create(:entry, with_publisher: user) + + sign_in(user, scope: :user) + post(:duplicate, params: {id: entry.id, duplicate: {copy_memberships: '0'}}) + + expect(Pageflow::Entry.last.users).to be_empty + end + + it 'allows account publisher to place duplicate in other account with publisher role' do + user = create(:user) + account = create(:account, with_publisher: user) + other_account = create(:account, with_publisher: user) + entry = create(:entry, account:) + + sign_in(user, scope: :user) + post(:duplicate, params: {id: entry.id, entry: {account_id: other_account.id}}) + + expect(Pageflow::Entry.last.account).to eq(other_account) + end + + it 'does not allow placing duplicate in account without publisher role' do + user = create(:user) + account = create(:account, with_publisher: user) + other_account = create(:account, with_editor: user) + entry = create(:entry, account:) + + sign_in(user, scope: :user) + post(:duplicate, params: {id: entry.id, entry: {account_id: other_account.id}}) + + expect(Pageflow::Entry.last.account).to eq(account) + end + + it 'does not allow entry publisher to place duplicate in other account' do + user = create(:user) + entry = create(:entry, with_publisher: user) + other_account = create(:account, with_publisher: user) + + sign_in(user, scope: :user) + post(:duplicate, params: {id: entry.id, entry: {account_id: other_account.id}}) + + expect(Pageflow::Entry.last.account).to eq(entry.account) + end + + it 'allows account publisher to place duplicate in other site of account' do + user = create(:user) + account = create(:account, with_publisher: user) + other_site = create(:site, account:) + entry = create(:entry, account:) + + sign_in(user, scope: :user) + post(:duplicate, params: {id: entry.id, entry: {site_id: other_site.id}}) + + expect(Pageflow::Entry.last.site).to eq(other_site) + end + + it 'does not allow placing duplicate in site of other account' do + user = create(:user) + account = create(:account, with_publisher: user) + other_site = create(:site) + entry = create(:entry, account:) + + sign_in(user, scope: :user) + post(:duplicate, params: {id: entry.id, entry: {site_id: other_site.id}}) + + expect(Pageflow::Entry.last.site).to eq(account.default_site) + end + + it 'displays error if entry type is not available in selected account' do + pageflow_configure do |config| + config.features.register('test_entry_type') do |feature_config| + Pageflow::TestEntryType.register(feature_config, name: 'test') + end + config.features.enable_by_default('test_entry_type') + end + user = create(:user) + account = create(:account, with_publisher: user) + other_account = create(:account, + with_publisher: user, + features_configuration: {'test_entry_type' => false}) + entry = create(:entry, account:, type_name: 'test') + + sign_in(user, scope: :user) + + expect { + post(:duplicate, params: {id: entry.id, entry: {account_id: other_account.id}}) + }.not_to(change { Pageflow::Entry.count }) + expect(response.body).to have_selector('.errors') + end + + it 're-renders form and does not create entry if title is blank' do + user = create(:user) + entry = create(:entry, with_publisher: user) + + sign_in(user, scope: :user) + + expect { + post(:duplicate, params: {id: entry.id, entry: {title: ''}}) + }.not_to(change { Pageflow::Entry.count }) + expect(response).to have_http_status(:ok) + end end describe '#destroy' do diff --git a/spec/features/entry_publisher/duplicating_an_entry_spec.rb b/spec/features/entry_publisher/duplicating_an_entry_spec.rb index 7ed210ead6..914706540c 100644 --- a/spec/features/entry_publisher/duplicating_an_entry_spec.rb +++ b/spec/features/entry_publisher/duplicating_an_entry_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' feature 'as entry publisher, duplicating an entry' do - scenario 'entry is duplicated' do + scenario 'entry is duplicated with selected title' do entry = create(:entry, title: 'Test Entry') Dom::Admin::Page.sign_in_as(:publisher, on: entry) @@ -11,4 +11,26 @@ expect(Dom::Admin::EntryPage.first.title).to eq('My Copy of Test Title') end + + scenario 'leaving duplicate form without submitting' do + entry = create(:entry, title: 'Test Entry') + Dom::Admin::Page.sign_in_as(:publisher, on: entry) + + visit(admin_entry_path(entry)) + + expect { + Dom::Admin::EntryPage.first.duplicate_button.click + }.not_to(change { Pageflow::Entry.count }) + end + + scenario 'entry is duplicated without members' do + entry = create(:entry, title: 'Test Entry', with_previewer: create(:user)) + Dom::Admin::Page.sign_in_as(:admin) + + visit(admin_entry_path(entry)) + Dom::Admin::EntryPage.first.duplicate_button.click + Dom::Admin::EntryForm.first.submit_with(title: 'Copy', copy_memberships: false) + + expect(Dom::Admin::EntryPage.first).not_to have_role_flag(:previewer) + end end diff --git a/spec/helpers/pageflow/admin/form_helper_spec.rb b/spec/helpers/pageflow/admin/form_helper_spec.rb new file mode 100644 index 0000000000..2e1d020423 --- /dev/null +++ b/spec/helpers/pageflow/admin/form_helper_spec.rb @@ -0,0 +1,22 @@ +require 'spec_helper' + +module Pageflow + module Admin + describe FormHelper do + describe '#choice_label_with_description' do + it 'wraps title in span followed by description' do + html = helper.choice_label_with_description('Title', 'Some description.') + + expect(html).to have_selector('span.choice_title', text: 'Title') + expect(html).to have_text('Title Some description.') + end + + it 'escapes title and description' do + html = helper.choice_label_with_description('Title', 'Text') + + expect(html).not_to have_selector('b, i') + end + end + end + end +end diff --git a/spec/models/pageflow/entry_duplicate_spec.rb b/spec/models/pageflow/entry_duplicate_spec.rb index fa63ef2d4b..f892e1ee72 100644 --- a/spec/models/pageflow/entry_duplicate_spec.rb +++ b/spec/models/pageflow/entry_duplicate_spec.rb @@ -43,6 +43,74 @@ module Pageflow expect(duplicate.users.first).to eq(user) end + it 'only copies memberships of users with membership on target account' do + other_account = create(:account) + user_on_both_accounts = create(:user, :member, on: other_account) + user_on_original_account = create(:user) + entry = create(:entry, + with_previewer: user_on_both_accounts, + with_editor: user_on_original_account) + + duplicate = EntryDuplicate.of(entry, attributes: {account_id: other_account.id}).create! + + expect(duplicate.users).to eq([user_on_both_accounts]) + end + + it 'does not copy memberships if copy_memberships is false' do + entry = create(:entry, with_previewer: create(:user)) + + duplicate = EntryDuplicate.of(entry, copy_memberships: false).create! + + expect(duplicate.users).to be_empty + end + + it 'uses passed title' do + entry = create(:entry, title: 'Some Title') + + duplicate = EntryDuplicate.of(entry, attributes: {title: 'Other Title'}).create! + + expect(duplicate.title).to eq('Other Title') + end + + it 'copies folder' do + account = create(:account) + folder = create(:folder, account:) + entry = create(:entry, account:, folder:) + + duplicate = EntryDuplicate.of(entry).create! + + expect(duplicate.folder).to eq(folder) + end + + it 'supports placing duplicate in other account' do + account = create(:account) + entry = create(:entry, account:, folder: create(:folder, account:)) + other_account = create(:account) + + duplicate = EntryDuplicate.of(entry, attributes: {account_id: other_account.id}).create! + + expect(duplicate).to have_attributes(account: other_account, + site: other_account.default_site, + folder: nil) + end + + it 'supports placing duplicate in other site of same account' do + entry = create(:entry) + other_site = create(:site, account: entry.account) + + duplicate = EntryDuplicate.of(entry, attributes: {site_id: other_site.id}).create! + + expect(duplicate.site).to eq(other_site) + end + + it 'raises if attributes are invalid' do + entry = create(:entry) + + expect { + EntryDuplicate.of(entry, attributes: {title: ''}).create! + }.to raise_error(ActiveRecord::RecordInvalid) + end + it 'copies enabled features' do entry = create(:entry, features_configuration: {fancy_page_type: true}) @@ -103,10 +171,83 @@ module Pageflow duplicate = EntryDuplicate.of(entry).create! expect(duplicate.permalink) - .to have_attributes(slug: "copy-of-my-entry-#{duplicate.id}", + .to have_attributes(slug: 'copy-of-my-entry-2', directory: permalink_directory) end + it 'makes default slug unique when entry is duplicated repeatedly' do + entry = create(:entry, + title: 'My entry', + permalink_attributes: {slug: 'slug', directory_path: 'en/'}) + + EntryDuplicate.of(entry).create! + EntryDuplicate.of(entry).create! + duplicate = EntryDuplicate.of(entry).create! + + expect(duplicate.permalink).to have_attributes(slug: 'copy-of-my-entry-3') + end + + it 'makes default slug for blank passed slug unique in directory' do + entry = create(:entry, + title: 'My entry', + permalink_attributes: {slug: 'slug', directory_path: 'en/'}) + EntryDuplicate.of(entry).create! + + duplicate = EntryDuplicate.of( + entry, + attributes: { + permalink_attributes: {slug: '', directory_id: entry.permalink.directory_id} + } + ).create! + + expect(duplicate.permalink).to have_attributes(slug: 'copy-of-my-entry-2') + end + + it 'does not create permalink in directory of original site if placed in other account' do + entry = create(:entry, permalink_attributes: {slug: 'slug', directory_path: 'en/'}) + other_account = create(:account) + + duplicate = EntryDuplicate.of(entry, attributes: {account_id: other_account.id}).create! + + expect(duplicate.permalink).to be_nil + end + + it 'does not create permalink in directory of original site if placed in other site' do + entry = create(:entry, permalink_attributes: {slug: 'slug', directory_path: 'en/'}) + other_site = create(:site, account: entry.account) + + duplicate = EntryDuplicate.of(entry, attributes: {site_id: other_site.id}).create! + + expect(duplicate.permalink).to be_nil + end + + it 'uses passed permalink attributes' do + entry = create(:entry, permalink_attributes: {slug: 'slug', directory_path: 'en/'}) + other_directory = create(:permalink_directory, site: entry.site, path: 'de/') + + duplicate = EntryDuplicate.of( + entry, + attributes: {permalink_attributes: {slug: 'custom', directory_id: other_directory.id}} + ).create! + + expect(duplicate.permalink) + .to have_attributes(slug: 'custom', directory: other_directory) + end + + it 'uses default slug based on passed title if passed slug is blank' do + entry = create(:entry, permalink_attributes: {slug: 'slug', directory_path: 'en/'}) + + duplicate = EntryDuplicate.of( + entry, + attributes: { + title: 'Other Title', + permalink_attributes: {slug: '', directory_id: entry.permalink.directory_id} + } + ).create! + + expect(duplicate.permalink).to have_attributes(slug: 'other-title') + end + it 'only keeps permalink unique in directory' do create( :entry, @@ -130,5 +271,60 @@ module Pageflow directory: entry.permalink.directory) end end + + describe '#save' do + it 'returns false and does not create entry if attributes are invalid' do + entry = create(:entry) + duplicate = EntryDuplicate.of(entry, attributes: {title: ''}) + + expect { + expect(duplicate.save).to eq(false) + }.not_to(change { Entry.count }) + expect(duplicate.new_entry.errors).to include(:title) + end + + it 'keeps passed slug if permalink slug is taken' do + entry = create(:entry, permalink_attributes: {slug: 'taken', directory_path: 'en/'}) + duplicate = EntryDuplicate.of( + entry, + attributes: { + permalink_attributes: {slug: 'taken', directory_id: entry.permalink.directory_id} + } + ) + + duplicate.save + + expect(duplicate.new_entry.permalink.slug).to eq('taken') + end + + it 'keeps blank passed slug if save fails' do + entry = create(:entry, permalink_attributes: {slug: 'slug', directory_path: 'en/'}) + directory_of_other_site = create(:permalink_directory) + duplicate = EntryDuplicate.of( + entry, + attributes: { + permalink_attributes: {slug: '', directory_id: directory_of_other_site.id} + } + ) + + duplicate.save + + expect(duplicate.new_entry.permalink.slug).to eq('') + end + + it 'does not create entry if permalink slug is taken' do + entry = create(:entry, permalink_attributes: {slug: 'taken', directory_path: 'en/'}) + duplicate = EntryDuplicate.of( + entry, + attributes: { + permalink_attributes: {slug: 'taken', directory_id: entry.permalink.directory_id} + } + ) + + expect { + expect(duplicate.save).to eq(false) + }.not_to(change { Entry.count }) + end + end end end diff --git a/spec/support/dominos/admin/entry_form.rb b/spec/support/dominos/admin/entry_form.rb index ba4e062354..6801e1f8e0 100644 --- a/spec/support/dominos/admin/entry_form.rb +++ b/spec/support/dominos/admin/entry_form.rb @@ -14,6 +14,7 @@ def submit_with(options) select(Pageflow::Folder.find(options[:folder_id]).name, from: 'entry_folder_id') end + choose('duplicate_copy_memberships_0') if options[:copy_memberships] == false find('[name="commit"]').click end end From 08b40233b81e7b916cca05da8eff3d7803310e7e Mon Sep 17 00:00:00 2001 From: Tim Fischbach Date: Thu, 15 Oct 2026 17:39:34 +0200 Subject: [PATCH 2/3] Make default permalink slugs unique in directory When the permalink slug is left empty and the default slug derived from the entry title is already taken in the selected directory, append the first free number (e.g. "my-title-2") instead of failing validation. The slug placeholder in the entry and duplicate forms shows the resulting slug for the selected directory, so the suffix never comes as a surprise. Validation errors were previously preferred over Friendly ID's UUID suffixes, which could not be predicted in the placeholder. Duplicated entries now use the same scheme instead of appending the entry id. --- .../javascripts/pageflow/admin/entries.js | 17 ++-- app/helpers/pageflow/admin/entries_helper.rb | 10 +++ app/models/pageflow/entry_duplicate.rb | 20 +---- app/models/pageflow/permalink.rb | 9 +- app/models/pageflow/permalink_directory.rb | 14 +++ .../admin/entries/_permalink_inputs.html.erb | 6 +- .../admin/entries/permalink_inputs.html.erb | 1 + .../admin/entries_controller_spec.rb | 88 +++++++++++++++++++ .../account_publisher/adding_an_entry_spec.rb | 17 ++++ .../editing_entry_permalink_spec.rb | 27 ++++++ .../concerns/pageflow/permalinkable_spec.rb | 49 +++++++++-- .../pageflow/permalink_directory_spec.rb | 32 +++++++ 12 files changed, 259 insertions(+), 31 deletions(-) create mode 100644 spec/features/account_publisher/editing_entry_permalink_spec.rb diff --git a/app/assets/javascripts/pageflow/admin/entries.js b/app/assets/javascripts/pageflow/admin/entries.js index ac09a14a71..9379f23ada 100644 --- a/app/assets/javascripts/pageflow/admin/entries.js +++ b/app/assets/javascripts/pageflow/admin/entries.js @@ -91,6 +91,7 @@ jQuery(function($) { var siteSelect = $('#entry_site_id', this); var titleInput = $('#entry_title', this); var permalinkDirectorySelect = $('#entry_permalink_attributes_directory_id', this); + var permalinkIdInput = $('#entry_permalink_attributes_id', this); var isRootPermalink = $(this).attr('data-root-permalink') === 'true'; @@ -115,16 +116,20 @@ jQuery(function($) { } function updatePermalinkInput() { - fetchPermalinkInput(function(response) { + fetchPermalinkInput('', function(response) { $('#entry_permalink_attributes_permalink_input').replaceWith(response); permalinkDirectorySelect = $('#entry_permalink_attributes_directory_id'); - permalinkDirectorySelect.on('change', updateSlugPlaceholder); + permalinkDirectorySelect.on('change', updateSlugPlaceholderData); }); } function updateSlugPlaceholderData() { - fetchPermalinkInput(function(response) { + var directoryParam = permalinkDirectorySelect.val() ? + '&entry[permalink_attributes][directory_id]=' + permalinkDirectorySelect.val() : + ''; + + fetchPermalinkInput(directoryParam, function(response) { $('#entry_permalink_attributes_slug') .attr('data-placeholder', $(response).find('input[data-placeholder]').attr('data-placeholder')); @@ -147,10 +152,12 @@ jQuery(function($) { ); } - function fetchPermalinkInput(callback) { + function fetchPermalinkInput(extraParams, callback) { $.get('/admin/entries/permalink_inputs' + '?entry[account_id]=' + accountSelect.val() + (siteSelect.val() ? '&entry[site_id]=' + siteSelect.val() : '') + + extraParams + + (permalinkIdInput.val() ? '&permalink_id=' + permalinkIdInput.val() : '') + '&entry[title]=' + encodeURIComponent(titleInput.val())) .done(callback); } @@ -161,7 +168,7 @@ jQuery(function($) { siteSelect.on('change', updatePermalinkInput); titleInput.on('change keyup', debounce(updateSlugPlaceholderData, 300)); - permalinkDirectorySelect.on('change', updateSlugPlaceholder); + permalinkDirectorySelect.on('change', updateSlugPlaceholderData); updateSlugPlaceholder(); }); diff --git a/app/helpers/pageflow/admin/entries_helper.rb b/app/helpers/pageflow/admin/entries_helper.rb index 57662d4c47..3bfce9515f 100644 --- a/app/helpers/pageflow/admin/entries_helper.rb +++ b/app/helpers/pageflow/admin/entries_helper.rb @@ -88,6 +88,16 @@ def entry_type_collection(entry_types = Pageflow.config.entry_types) I18n.t(type, scope: 'activerecord.values.pageflow/entry.type_names') end end + + def permalink_slug_placeholder(entry, permalink, except:) + directory = permalink.directory + + unless directory&.site_id == entry.site_id + directory = entry.site.permalink_directories.first + end + + Permalink.default_slug_for(entry, directory:, except:) + end end end end diff --git a/app/models/pageflow/entry_duplicate.rb b/app/models/pageflow/entry_duplicate.rb index 97e8fcb939..bcd74e5ec6 100644 --- a/app/models/pageflow/entry_duplicate.rb +++ b/app/models/pageflow/entry_duplicate.rb @@ -21,7 +21,6 @@ def save passed_slug = new_entry.permalink&.slug saved = ActiveRecord::Base.transaction do - assign_default_permalink_slug next false unless new_entry.save copy_draft @@ -57,24 +56,7 @@ def build_permalink_in_original_directory(entry) directory = original_entry.permalink&.directory return unless directory && directory.site_id == entry.site_id - entry.build_permalink(directory:) - end - - def assign_default_permalink_slug - permalink = new_entry.permalink - return if !permalink&.directory || permalink.slug.present? - - base_slug = new_entry.default_permalink_slug - return if base_slug.blank? - - permalink.slug = unique_slug(permalink.directory, base_slug) - end - - def unique_slug(directory, base_slug) - (1..) - .lazy - .map { |index| index == 1 ? base_slug : "#{base_slug}-#{index}" } - .find { |slug| !directory.permalinks.exists?(slug:) } + entry.build_permalink(directory:, slug: '') end def copy_draft diff --git a/app/models/pageflow/permalink.rb b/app/models/pageflow/permalink.rb index 00e42d70d1..e6ddd9d9bd 100644 --- a/app/models/pageflow/permalink.rb +++ b/app/models/pageflow/permalink.rb @@ -24,13 +24,20 @@ class Permalink < ApplicationRecord attr_accessor :allow_root_path + def self.default_slug_for(entry, directory:, except: nil) + base_slug = entry.default_permalink_slug + return base_slug if base_slug.blank? || !directory + + directory.first_available_slug(base_slug, except:) + end + private def set_default_slug return unless slug == '' return if allow_emtpy_slug? - self.slug = entry.default_permalink_slug + self.slug = Permalink.default_slug_for(entry, directory:, except: id) end def allow_emtpy_slug? diff --git a/app/models/pageflow/permalink_directory.rb b/app/models/pageflow/permalink_directory.rb index b4e8d7915c..32e1d2fc98 100644 --- a/app/models/pageflow/permalink_directory.rb +++ b/app/models/pageflow/permalink_directory.rb @@ -13,5 +13,19 @@ class PermalinkDirectory < ApplicationRecord has_many(:redirects, class_name: 'PermalinkRedirect', foreign_key: :directory_id) + + def first_available_slug(base_slug, except: nil) + taken_slugs = permalinks + .where.not(id: except) + .where('slug = :base_slug OR slug LIKE :suffixed', + base_slug:, + suffixed: "#{self.class.sanitize_sql_like(base_slug)}-%") + .pluck(:slug) + + (1..) + .lazy + .map { |index| index == 1 ? base_slug : "#{base_slug}-#{index}" } + .find { |slug| !taken_slugs.include?(slug) } + end end end diff --git a/app/views/admin/entries/_permalink_inputs.html.erb b/app/views/admin/entries/_permalink_inputs.html.erb index ca7a4196a4..12c2847ca1 100644 --- a/app/views/admin/entries/_permalink_inputs.html.erb +++ b/app/views/admin/entries/_permalink_inputs.html.erb @@ -4,7 +4,11 @@ pretty_site_url(entry.site), site: entry.site, only_root: entry.permalink&.allow_root_path, - slug_placeholder: entry.default_permalink_slug, + slug_placeholder: permalink_slug_placeholder( + entry, + form.object, + except: local_assigns.fetch(:except_permalink_id, form.object.id) + ), hint: entry.new_record? ? nil : t('pageflow.admin.entries.permalink_hint'))%> <% if entry.permalink&.allow_root_path %> diff --git a/app/views/admin/entries/permalink_inputs.html.erb b/app/views/admin/entries/permalink_inputs.html.erb index 0057765f23..51c0995afe 100644 --- a/app/views/admin/entries/permalink_inputs.html.erb +++ b/app/views/admin/entries/permalink_inputs.html.erb @@ -2,6 +2,7 @@ <%= form.fields_for(:permalink, @entry.permalink || Pageflow::Permalink.new) do |permalink| %> <%= render('admin/entries/permalink_inputs', entry: @entry, + except_permalink_id: params[:permalink_id], form: permalink) %> <% end %> <% end %> diff --git a/spec/controllers/admin/entries_controller_spec.rb b/spec/controllers/admin/entries_controller_spec.rb index a5d610731c..a7324bf63c 100644 --- a/spec/controllers/admin/entries_controller_spec.rb +++ b/spec/controllers/admin/entries_controller_spec.rb @@ -2162,6 +2162,94 @@ def self.name '[data-placeholder="some-title"]') end + it 'appends suffix to placeholder if default slug is taken in passed directory' do + account = create(:account) + create(:permalink_directory, site: account.default_site, path: 'en/') + directory = create(:permalink_directory, site: account.default_site, path: 'de/') + create(:entry, account:, permalink_attributes: {slug: 'some-title', directory:}) + + sign_in(create(:user, :publisher, on: account)) + get(:permalink_inputs, + params: { + entry: { + account_id: account, + title: 'Some Title', + permalink_attributes: {directory_id: directory.id} + } + }) + + expect(response.body) + .to have_selector('input[name="entry[permalink_attributes][slug]"]' \ + '[data-placeholder="some-title-2"]') + end + + it 'checks placeholder against first directory of site if none is passed' do + account = create(:account) + create(:permalink_directory, site: account.default_site, path: 'en/') + directory = create(:permalink_directory, site: account.default_site, path: 'de/') + create(:entry, account:, permalink_attributes: {slug: 'some-title', directory:}) + + sign_in(create(:user, :publisher, on: account)) + get(:permalink_inputs, + params: { + entry: { + account_id: account, + title: 'Some Title' + } + }) + + expect(response.body) + .to have_selector('input[name="entry[permalink_attributes][slug]"]' \ + '[data-placeholder="some-title-2"]') + end + + it 'ignores passed directory of other site for placeholder' do + account = create(:account) + create(:permalink_directory, site: account.default_site, path: 'en/') + directory_of_other_site = create(:permalink_directory) + create(:entry, + site: directory_of_other_site.site, + account: directory_of_other_site.site.account, + permalink_attributes: {slug: 'some-title', directory: directory_of_other_site}) + + sign_in(create(:user, :publisher, on: account)) + get(:permalink_inputs, + params: { + entry: { + account_id: account, + title: 'Some Title', + permalink_attributes: {directory_id: directory_of_other_site.id} + } + }) + + expect(response.body) + .to have_selector('input[name="entry[permalink_attributes][slug]"]' \ + '[data-placeholder="some-title"]') + end + + it 'does not treat slug of passed permalink as taken for placeholder' do + account = create(:account) + directory = create(:permalink_directory, site: account.default_site, path: 'en/') + entry = create(:entry, + account:, + permalink_attributes: {slug: 'some-title', directory:}) + + sign_in(create(:user, :publisher, on: account)) + get(:permalink_inputs, + params: { + permalink_id: entry.permalink.id, + entry: { + account_id: account, + title: 'Some Title', + permalink_attributes: {directory_id: directory.id} + } + }) + + expect(response.body) + .to have_selector('input[name="entry[permalink_attributes][slug]"]' \ + '[data-placeholder="some-title"]') + end + it 'renders hidden permalink field without inputs if no permalink directories' do account = create(:account) diff --git a/spec/features/account_publisher/adding_an_entry_spec.rb b/spec/features/account_publisher/adding_an_entry_spec.rb index ba95e7c9f0..8041888913 100644 --- a/spec/features/account_publisher/adding_an_entry_spec.rb +++ b/spec/features/account_publisher/adding_an_entry_spec.rb @@ -67,4 +67,21 @@ expect(Dom::Admin::EntryForm.first .has_entry_type_option?(nyan_entry_type_name)).to eq(false) end + + scenario 'slug placeholder takes taken slugs of selected directory into account', js: true do + account = create(:account) + create(:permalink_directory, site: account.default_site, path: 'de/') + en_directory = create(:permalink_directory, site: account.default_site, path: 'en/') + create(:entry, account:, permalink_attributes: {slug: 'some-title', directory: en_directory}) + Dom::Admin::Page.sign_in_as(:publisher, on: account) + + visit(new_admin_entry_path) + fill_in('entry_title', with: 'Some Title') + + expect(page).to have_selector('#entry_permalink_attributes_slug[placeholder="some-title"]') + + select('en/', from: 'entry_permalink_attributes_directory_id') + + expect(page).to have_selector('#entry_permalink_attributes_slug[placeholder="some-title-2"]') + end end diff --git a/spec/features/account_publisher/editing_entry_permalink_spec.rb b/spec/features/account_publisher/editing_entry_permalink_spec.rb new file mode 100644 index 0000000000..b1d2bee8e0 --- /dev/null +++ b/spec/features/account_publisher/editing_entry_permalink_spec.rb @@ -0,0 +1,27 @@ +require 'spec_helper' + +feature 'as account publisher, editing entry permalink' do + scenario 'slug placeholder does not treat own slug as taken', js: true do + account = create(:account) + de_directory = create(:permalink_directory, site: account.default_site, path: 'de/') + en_directory = create(:permalink_directory, site: account.default_site, path: 'en/') + entry = create(:entry, + account:, + title: 'Some Title', + permalink_attributes: {slug: 'some-title', directory: en_directory}) + create(:entry, + account:, + permalink_attributes: {slug: 'some-title', directory: de_directory}) + Dom::Admin::Page.sign_in_as(:publisher, on: account) + + visit(edit_admin_entry_path(entry)) + fill_in('entry_permalink_attributes_slug', with: '') + select('de/', from: 'entry_permalink_attributes_directory_id') + + expect(page).to have_selector('#entry_permalink_attributes_slug[placeholder="some-title-2"]') + + select('en/', from: 'entry_permalink_attributes_directory_id') + + expect(page).to have_selector('#entry_permalink_attributes_slug[placeholder="some-title"]') + end +end diff --git a/spec/models/concerns/pageflow/permalinkable_spec.rb b/spec/models/concerns/pageflow/permalinkable_spec.rb index b3d7dbeaf7..d1260961d7 100644 --- a/spec/models/concerns/pageflow/permalinkable_spec.rb +++ b/spec/models/concerns/pageflow/permalinkable_spec.rb @@ -173,31 +173,70 @@ module Pageflow expect(entry.permalink).to have_attributes(slug: 'my-example') end - it 'does not enforce uniqueness when generating default slug' do + it 'appends suffix to default slug if taken in directory' do account = create(:account) permalink_directory = create( :permalink_directory, site: account.default_site ) + ['my-example', 'my-example-2'].each do |slug| + create( + :entry, + account:, + permalink_attributes: { + slug:, + directory: permalink_directory + } + ) + end + entry = create( + :entry, + account:, + title: 'My Example', + permalink_attributes: { + slug: '', + directory: permalink_directory + } + ) + + expect(entry.permalink).to have_attributes(slug: 'my-example-3') + end + + it 'ignores slugs in other directories when generating default slug' do + account = create(:account) create( :entry, account:, permalink_attributes: { slug: 'my-example', - directory: permalink_directory + directory_path: 'de/' } ) - entry = build( + entry = create( :entry, account:, title: 'My Example', permalink_attributes: { slug: '', - directory: permalink_directory + directory_path: 'en/' } ) - expect(entry).to have(1).error_on('permalink.slug') + expect(entry.permalink).to have_attributes(slug: 'my-example') + end + + it 'keeps own slug when generating default slug on update' do + entry = create( + :entry, + title: 'My Example', + permalink_attributes: { + slug: 'my-example' + } + ) + + entry.update(permalink_attributes: {slug: ''}) + + expect(entry.permalink).to have_attributes(slug: 'my-example') end it 'creates permalink redirect when slug changes' do diff --git a/spec/models/pageflow/permalink_directory_spec.rb b/spec/models/pageflow/permalink_directory_spec.rb index f66005e80f..f137d6230c 100644 --- a/spec/models/pageflow/permalink_directory_spec.rb +++ b/spec/models/pageflow/permalink_directory_spec.rb @@ -54,5 +54,37 @@ module Pageflow expect(permalink_directory).to be_valid end end + + describe '#first_available_slug' do + it 'returns base slug if not taken' do + directory = create(:permalink_directory) + + expect(directory.first_available_slug('some-title')).to eq('some-title') + end + + it 'appends first free suffix' do + directory = create(:permalink_directory) + ['some-title', 'some-title-3'].each do |slug| + create(:permalink, directory:, slug:) + end + + expect(directory.first_available_slug('some-title')).to eq('some-title-2') + end + + it 'ignores slugs that only share prefix' do + directory = create(:permalink_directory) + create(:permalink, directory:, slug: 'some-title-extra') + + expect(directory.first_available_slug('some-title')).to eq('some-title') + end + + it 'ignores excepted permalink' do + directory = create(:permalink_directory) + permalink = create(:permalink, directory:, slug: 'some-title') + + expect(directory.first_available_slug('some-title', except: permalink.id)) + .to eq('some-title') + end + end end end From a966fb19d0583e55e99b8c33b6f7a4d6caac4b64 Mon Sep 17 00:00:00 2001 From: Tim Fischbach Date: Thu, 15 Oct 2026 16:05:12 +0200 Subject: [PATCH 3/3] Require account publisher role to duplicate entries Duplicating creates a new entry in the account, but entry publishers were allowed to do so even though only account publishers may create entries. Align the two permissions. Entry publishers without account publisher role can no longer copy entries. --- app/policies/pageflow/entry_policy.rb | 2 +- .../admin/entries_controller_spec.rb | 50 +++++++++++-------- .../duplicating_an_entry_spec.rb | 6 +-- spec/policies/pageflow/entry_policy_spec.rb | 8 ++- 4 files changed, 40 insertions(+), 26 deletions(-) rename spec/features/{entry_publisher => account_publisher}/duplicating_an_entry_spec.rb (85%) diff --git a/app/policies/pageflow/entry_policy.rb b/app/policies/pageflow/entry_policy.rb index 43d5b6bf3c..ebde81319c 100644 --- a/app/policies/pageflow/entry_policy.rb +++ b/app/policies/pageflow/entry_policy.rb @@ -105,7 +105,7 @@ def create? end def duplicate? - query.has_at_least_role?(:publisher) + query.has_at_least_account_role?(:publisher) end def manage_translations? diff --git a/spec/controllers/admin/entries_controller_spec.rb b/spec/controllers/admin/entries_controller_spec.rb index a7324bf63c..cbd9096820 100644 --- a/spec/controllers/admin/entries_controller_spec.rb +++ b/spec/controllers/admin/entries_controller_spec.rb @@ -1721,7 +1721,8 @@ def self.name describe 'get #duplicate' do it 'renders form with default title and does not create entry' do user = create(:user) - entry = create(:entry, title: 'Some Title', with_publisher: user) + account = create(:account, with_publisher: user) + entry = create(:entry, account:, title: 'Some Title') sign_in(user, scope: :user) @@ -1731,9 +1732,9 @@ def self.name expect(response.body).to have_field('entry_title', with: 'Copy of Some Title') end - it 'does not allow entry editor to see form' do + it 'does not allow entry publisher to see form' do user = create(:user) - entry = create(:entry, with_editor: user) + entry = create(:entry, with_publisher: user) sign_in(user, scope: :user) get(:duplicate, params: {id: entry.id}) @@ -1797,12 +1798,24 @@ def self.name }.not_to(change { Pageflow::Entry.count }) end - it 'allows entry publisher to duplicate entry' do + it 'does not allow entry publisher to duplicate entry' do user = create(:user) entry = create(:entry, with_publisher: user) sign_in(user, scope: :user) + expect { + post(:duplicate, params: {id: entry.id}) + }.not_to(change { Pageflow::Entry.count }) + end + + it 'allows account publisher to duplicate entry' do + user = create(:user) + account = create(:account, with_publisher: user) + entry = create(:entry, account:) + + sign_in(user, scope: :user) + expect { post(:duplicate, params: {id: entry.id}) }.to(change { Pageflow::Entry.count }) @@ -1821,7 +1834,8 @@ def self.name it 'redirects to admin page of new entry' do user = create(:user) - entry = create(:entry, with_publisher: user) + account = create(:account, with_publisher: user) + entry = create(:entry, account:) sign_in(user, scope: :user) post(:duplicate, params: {id: entry.id}) @@ -1831,8 +1845,9 @@ def self.name it 'uses passed title and permalink slug' do user = create(:user) + account = create(:account, with_publisher: user) entry = create(:entry, - with_publisher: user, + account:, permalink_attributes: {slug: 'some-slug', directory_path: 'en/'}) sign_in(user, scope: :user) @@ -1854,17 +1869,20 @@ def self.name it 'copies memberships by default' do user = create(:user) - entry = create(:entry, with_publisher: user) + account = create(:account, with_publisher: user) + member = create(:user) + entry = create(:entry, account:, with_editor: member) sign_in(user, scope: :user) post(:duplicate, params: {id: entry.id}) - expect(Pageflow::Entry.last.users).to include(user) + expect(Pageflow::Entry.last.users).to include(member) end it 'supports skipping memberships' do user = create(:user) - entry = create(:entry, with_publisher: user) + account = create(:account, with_publisher: user) + entry = create(:entry, account:, with_editor: create(:user)) sign_in(user, scope: :user) post(:duplicate, params: {id: entry.id, duplicate: {copy_memberships: '0'}}) @@ -1896,17 +1914,6 @@ def self.name expect(Pageflow::Entry.last.account).to eq(account) end - it 'does not allow entry publisher to place duplicate in other account' do - user = create(:user) - entry = create(:entry, with_publisher: user) - other_account = create(:account, with_publisher: user) - - sign_in(user, scope: :user) - post(:duplicate, params: {id: entry.id, entry: {account_id: other_account.id}}) - - expect(Pageflow::Entry.last.account).to eq(entry.account) - end - it 'allows account publisher to place duplicate in other site of account' do user = create(:user) account = create(:account, with_publisher: user) @@ -1955,7 +1962,8 @@ def self.name it 're-renders form and does not create entry if title is blank' do user = create(:user) - entry = create(:entry, with_publisher: user) + account = create(:account, with_publisher: user) + entry = create(:entry, account:) sign_in(user, scope: :user) diff --git a/spec/features/entry_publisher/duplicating_an_entry_spec.rb b/spec/features/account_publisher/duplicating_an_entry_spec.rb similarity index 85% rename from spec/features/entry_publisher/duplicating_an_entry_spec.rb rename to spec/features/account_publisher/duplicating_an_entry_spec.rb index 914706540c..ab79ea4c9c 100644 --- a/spec/features/entry_publisher/duplicating_an_entry_spec.rb +++ b/spec/features/account_publisher/duplicating_an_entry_spec.rb @@ -1,9 +1,9 @@ require 'spec_helper' -feature 'as entry publisher, duplicating an entry' do +feature 'as account publisher, duplicating an entry' do scenario 'entry is duplicated with selected title' do entry = create(:entry, title: 'Test Entry') - Dom::Admin::Page.sign_in_as(:publisher, on: entry) + Dom::Admin::Page.sign_in_as(:publisher, on: entry.account) visit(admin_entry_path(entry)) Dom::Admin::EntryPage.first.duplicate_button.click @@ -14,7 +14,7 @@ scenario 'leaving duplicate form without submitting' do entry = create(:entry, title: 'Test Entry') - Dom::Admin::Page.sign_in_as(:publisher, on: entry) + Dom::Admin::Page.sign_in_as(:publisher, on: entry.account) visit(admin_entry_path(entry)) diff --git a/spec/policies/pageflow/entry_policy_spec.rb b/spec/policies/pageflow/entry_policy_spec.rb index 720b68880b..e6e6e88485 100644 --- a/spec/policies/pageflow/entry_policy_spec.rb +++ b/spec/policies/pageflow/entry_policy_spec.rb @@ -53,7 +53,13 @@ module Pageflow it_behaves_like 'a membership-based permission that', allows: :publisher, but_forbids: :editor, - of_entry_or_its_account: ->(topic) { topic }, + of_account: ->(topic) { topic.account }, + to: :duplicate, + topic: -> { create(:entry) } + + it_behaves_like 'a membership-based permission that', + forbids: :manager, + of_entry: ->(topic) { topic }, to: :duplicate, topic: -> { create(:entry) }