Replies: 2 comments 5 replies
-
Hi, thanks for your note! I do not use ActireStorage myself, so I'm not sure -- I'm not sure I understand in what ways this would be integrated with ActiveStorage -- like attaching a stored file to a nested model? Hmm, I honestly have no idea! I personally am considering this gem fairly feature-complete at the moment. But if someone else figures out a way to approach this, please do share! I would be willing to review a PR, but it would have to be with fairly minimal/elegant/maintainable code additions here! |
Beta Was this translation helpful? Give feedback.
-
Hey! Thanks for your reply @jrochkind and apologies for the lack of mine. I paused the development of thing i've been working on using attr_json for some time but now i'm back and i think i [almost] nailed it. Basically, to do that, i decided to take a following path: I created a new class ContentBlock < ApplicationRecord
include AttrJson::NestedAttributes
class Section
include AttrJson::Model
attr_json :image, :binary
end
attr_json :sections, Section.to_type, array: true
attr_json_accepts_nested_attributes_for :sections, reject_if: :all_blank
before_save :resolve_images
private
def resolve_images
sections.each do |section|
# Here is the problematic part: How to tell if `image` attribute has changed?
# There is no such method as `*_changed?` so i can't use the default rails way of doing that.
# On the other hand, I apparently don't have access to `ContentBlock` record from the `Section`
# callback so i can't access the underlying json in the DB. However, even if i did, it would feel a bit
# hackish but maybe that's the only way. Or maybe nothing i wrote above is true and i have access
# to all that but i just missed that in the code and/or in the docs :)
next unless section.image_changed?
next if section.image.blank?
section.image = Spree::FileUpload.create!(file: section.image).to_global_id.to_s
end
end
end More clarification in the comment above. Thanks for any clues! 🙏 |
Beta Was this translation helpful? Give feedback.
-
Hey! After thinking a lot about it, i think that's not really possible to implement that without heavy hacking of ActiveStorage internals but maybe i miss something so i will give it a shot - do you think that supporting AS could be somehow possible in any way? That might be tricky as attachment relies on parent record existence but who knows :)
Beta Was this translation helpful? Give feedback.
All reactions