@@ -147,14 +147,10 @@ module Pageflow
147147
148148 action_item ( :duplicate , only : :show , priority : 5 ) do
149149 if authorized? ( :duplicate , entry )
150- button_to ( I18n . t ( 'pageflow.admin.entries.duplicate' ) ,
151- duplicate_admin_entry_path ( entry ) ,
152- method : :post ,
153- form_class : 'action_item duplicate' ,
154- data : {
155- rel : 'duplicate' ,
156- confirm : I18n . t ( 'pageflow.admin.entries.confirm_duplicate' )
157- } )
150+ link_to ( I18n . t ( 'pageflow.admin.entries.duplicate' ) ,
151+ duplicate_admin_entry_path ( entry ) ,
152+ class : 'duplicate' ,
153+ data : { rel : 'duplicate' } )
158154 end
159155 end
160156
@@ -184,11 +180,23 @@ module Pageflow
184180 end
185181 end
186182
187- member_action :duplicate , method : :post do
188- entry = Entry . find ( params [ :id ] )
189- authorize! ( :duplicate , entry )
190- new_entry = entry . duplicate
191- redirect_to ( edit_admin_entry_path ( new_entry ) )
183+ member_action :duplicate , method : [ :get , :post ] do
184+ original_entry = Entry . find ( params [ :id ] )
185+ authorize! ( :duplicate , original_entry )
186+
187+ duplicate = EntryDuplicate . of (
188+ original_entry ,
189+ attributes : permitted_duplicate_attributes ( original_entry ) ,
190+ copy_memberships : params . dig ( :duplicate , :copy_memberships ) != '0'
191+ )
192+
193+ if request . post? && duplicate . save
194+ redirect_to ( admin_entry_path ( duplicate . new_entry ) )
195+ else
196+ @page_title = I18n . t ( 'pageflow.admin.entries.duplicate_form.title' ,
197+ title : original_entry . title )
198+ render ( :duplicate , locals : { original_entry :, entry : duplicate . new_entry , duplicate :} )
199+ end
192200 end
193201
194202 member_action :snapshot , method : :post do
@@ -364,6 +372,33 @@ def permitted_attributes # rubocop:todo Metrics/AbcSize, Metrics/CyclomaticCompl
364372 end
365373 # rubocop:enable Metrics/PerceivedComplexity
366374
375+ def permitted_duplicate_attributes ( original_entry )
376+ attributes = [ :title , { permalink_attributes : [ :slug , :directory_id ] } ]
377+ attributes << :account_id if moving_duplicate_to_other_account_allowed? ( original_entry )
378+ attributes << :folder_id if authorized? ( :configure_folder_for , original_entry )
379+
380+ if authorized? ( :update_site_on , original_entry ) && site_params_present? &&
381+ site_in_allowed_sites_for? ( duplicate_target_account ( original_entry ) )
382+ attributes << :site_id
383+ end
384+
385+ params . fetch ( :entry , { } ) . permit ( attributes )
386+ end
387+
388+ def moving_duplicate_to_other_account_allowed? ( original_entry )
389+ account_params_present? &&
390+ authorized? ( :update_account_on , original_entry ) &&
391+ account_policy_scope . entry_movable . include? ( Account . find ( params [ :entry ] [ :account_id ] ) )
392+ end
393+
394+ def duplicate_target_account ( original_entry )
395+ if moving_duplicate_to_other_account_allowed? ( original_entry )
396+ Account . find ( params [ :entry ] [ :account_id ] )
397+ else
398+ original_entry . account
399+ end
400+ end
401+
367402 def permit_feature_states ( attributes )
368403 return unless params [ :id ] && authorized? ( :update_feature_configuration_on , resource )
369404
0 commit comments