moiristo / deep_cloneable

This gem gives every ActiveRecord::Base object the possibility to do a deep clone that includes user specified associations.
MIT License
786 stars 88 forks source link

How to handle :has_rich_text associations in rails 6 #122

Open rossinkiwi opened 4 years ago

rossinkiwi commented 4 years ago

Thank you so much for your amazing Gem! Rails 6 added Action Text. How would you recommend cloning models with "has_rich_text" associations?

rossinkiwi commented 4 years ago

I think I figured this out. Rails magic is creating a polymorphic association behind the scenes. So, if in your model you declare

class Event < ApplicationRecord
has_rich_text :description

Then in deep cloneable

@event = orig_event.deep_clone include: [ :rich_text_description ], use_dictionary: true do |orig, clone|
...
moiristo commented 4 years ago

Hi! Glad you figured it out :) Maybe a good idea to add it to the README?

rossinkiwi commented 4 years ago

In my use case the ActionText content contains only styled text and for that case the solution I provided works well.  

However, the full ActionText implementation allows embedded attachments (images) as well.  I believe cloning an ActionText field with embedded attachments would take more work - probably something similar to how ActiveStorage attachments are handled.

Ideally, a README entry would provide a solution for the full ActionText implementation rather than my special (no embedded attachments) case, and unfortunately I don't know how to do that yet :(