Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 48 additions & 13 deletions admins/pageflow/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

Expand Down
17 changes: 12 additions & 5 deletions app/assets/javascripts/pageflow/admin/entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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'));
Expand All @@ -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);
}
Expand All @@ -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();
});
Expand Down
9 changes: 9 additions & 0 deletions app/assets/stylesheets/pageflow/admin/forms.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
padding: 5px 10px;
}

.choice_title {
font-weight: bold;

&::after {
content: " –";
font-weight: normal;
}
}

fieldset.actions .link_action {
padding: 5px;

Expand Down
15 changes: 15 additions & 0 deletions app/helpers/pageflow/admin/entries_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,26 @@ 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')
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
4 changes: 4 additions & 0 deletions app/helpers/pageflow/admin/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
76 changes: 56 additions & 20 deletions app/models/pageflow/entry_duplicate.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,62 @@
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
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)

def create_entry
@new_entry = Entry.create!(new_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 copy_permalink
return if original_entry.permalink.blank?
def build_permalink_in_original_directory(entry)
directory = original_entry.permalink&.directory
return unless directory && directory.site_id == entry.site_id

new_entry.create_permalink!(directory: original_entry.permalink.directory)
entry.build_permalink(directory:, slug: '')
end

def copy_draft
Expand All @@ -37,25 +66,32 @@ 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,

skip_draft_creation: true
}
end

def new_title
def default_title
I18n.t('pageflow.entry.duplicated_title', title: original_entry.title)
end
end
Expand Down
9 changes: 8 additions & 1 deletion app/models/pageflow/permalink.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
14 changes: 14 additions & 0 deletions app/models/pageflow/permalink_directory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion app/policies/pageflow/entry_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
24 changes: 24 additions & 0 deletions app/views/admin/entries/_base_inputs.html.erb
Original file line number Diff line number Diff line change
@@ -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 %>
6 changes: 6 additions & 0 deletions app/views/admin/entries/_folder_input.html.erb
Original file line number Diff line number Diff line change
@@ -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 %>
Loading
Loading