Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate #3434

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Duplicate #3434

wants to merge 3 commits into from

Conversation

anitagraham
Copy link
Contributor

This is a very short PR.

It puts in place the basic features needed to implement a duplicate action.

The default duplicate action is very naive - it copies the item and opens the copy to be edited.
There is no attempt to copy any associations apart from :belongs_to, which is held on the model.

_A full implementation of a duplicate action will need a more sophisticated duplicate method - it will depend on the model and the implementor. Although the example below, using refinerycms-pages, adds some more steps, a true duplicator for refinerycms-pages needs attention paid to translations and other associations added by refinery extensions. I don't think it is ready to be submitted as a PR yet.

Each model will have different considerations - starting with whether it is appropriate to duplicate at all.
_

For an extension to implement the duplicate action it will need to

  • add a route to the action
  • change index/model view to add a link for the action
  • perhaps override the default duplicate method to extend the duplication to copy associations.

To implement 'duplicate' in an extension

This is still a simple duplication.

Adding a route

# refinerycms/pages/config/routes/rb
...
namespace :admin, path: Refinery::Core.backend_route do
  get 'pages/*path/edit', to: 'pages#edit', as: 'edit_page'
  get 'pages/*path/duplicate', to: 'pages#duplicate', as: 'duplicate_page'
...

Override the default duplicate action so page-parts are duplicated

# refinerycms/pages/app/controllers/refinery/admin/pages_controller.rb
module Refinery
  module Admin
    class PagesController < Refinery::AdminController
...
def duplicate
  original_page = @page
  @page = original_page.dup
  @page.title += ' copy'
  original_page.parts.each |page_part| do
      @page.parts << page_part.dup
   end
   @page.save
  render :edit
end

add a link to the duplicate action in the admin/index

# refinerycms/pages/app/views/refinery/admin/pages/_page.html.erb
...
<%= action_icon(:duplicate, refinery.admin_duplicate_page_path(page.nested_url),  t('duplicate',   scope: 'refinery.admin.pages' ) ) %>

Note

The example duplicate action saves the page before sending it to be edited. Seems to work.

Finally, I tried to write a routing test but didn't manage to get it working. Help appreciated.

@anitagraham
Copy link
Contributor Author

Well, I think I've made that change. I even tried to squash my commits, but I fear they are as tangled as ever.

@anitagraham
Copy link
Contributor Author

Not sure these failures are related to the this PR.

@parndt
Copy link
Member

parndt commented Aug 28, 2019

Yeah, the tests failures seem to be happening in every PR.. it's very strange. I think we'll need to fix it before we can merge anything though 😦

@parndt
Copy link
Member

parndt commented Aug 29, 2019

Tests started failing between Rails v5.2.2.1 (passing) and v5.2.3.rc1 (failing)

@parndt
Copy link
Member

parndt commented Aug 29, 2019

Tests fail from this change: rails/rails#35162

core/lib/refinery/crud.rb Outdated Show resolved Hide resolved
Add duplicated to the locale texts
Using a translation string to create the title for the new object
changes as suggested
Fix spacing in en.yml
@anitagraham anitagraham deleted the duplicate branch July 21, 2022 05:40
@anitagraham anitagraham restored the duplicate branch September 12, 2022 04:57
@anitagraham anitagraham reopened this Sep 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants