From 333c192425c699c004ece57878a3d0719fca1721 Mon Sep 17 00:00:00 2001 From: Edwin Cruz Date: Wed, 21 Aug 2019 10:56:51 -0500 Subject: [PATCH] Making taxon form to render attachment definitions dynamically Solidus now allows to inject easily more attachment definitions via configurable modules, if a user adds a new attachment, it would have to override the partial _form to add this new attachment, this change takes all definitions and render the inputs dinalycally, it also allows to specify what partial to render depending on the attachment adapter. --- backend/app/views/spree/admin/taxons/_form.html.erb | 6 +----- .../admin/taxons/attachment_forms/_paperclip.html.erb | 7 +++++++ core/app/models/spree/taxon/paperclip_attachment.rb | 4 ++++ 3 files changed, 12 insertions(+), 5 deletions(-) create mode 100644 backend/app/views/spree/admin/taxons/attachment_forms/_paperclip.html.erb diff --git a/backend/app/views/spree/admin/taxons/_form.html.erb b/backend/app/views/spree/admin/taxons/_form.html.erb index 6c93d998402..5b720beaa2d 100644 --- a/backend/app/views/spree/admin/taxons/_form.html.erb +++ b/backend/app/views/spree/admin/taxons/_form.html.erb @@ -20,11 +20,7 @@ <% end %> - <%= f.field_container :icon do %> - <%= f.label :icon %>
- <%= f.file_field :icon %> - <%= image_tag f.object.icon(:mini) if f.object.icon_present? %> - <% end %> + <%= render "spree/admin/taxons/attachment_forms/#{f.object.attachment_partial_name}", f: f %>
diff --git a/backend/app/views/spree/admin/taxons/attachment_forms/_paperclip.html.erb b/backend/app/views/spree/admin/taxons/attachment_forms/_paperclip.html.erb new file mode 100644 index 00000000000..939dcbf02e6 --- /dev/null +++ b/backend/app/views/spree/admin/taxons/attachment_forms/_paperclip.html.erb @@ -0,0 +1,7 @@ +<% f.object.class.attachment_definitions.each do |attachment, definition| %> + <%= f.field_container attachment do %> + <%= f.label attachment %>
+ <%= f.file_field attachment %> + <%= image_tag f.object.send(attachment, definition[:default_style]) if f.object.send(attachment).present? %> + <% end %> +<% end %> diff --git a/core/app/models/spree/taxon/paperclip_attachment.rb b/core/app/models/spree/taxon/paperclip_attachment.rb index cb4a83437cc..b688e5f8fd3 100644 --- a/core/app/models/spree/taxon/paperclip_attachment.rb +++ b/core/app/models/spree/taxon/paperclip_attachment.rb @@ -18,4 +18,8 @@ module Spree::Taxon::PaperclipAttachment def icon_present? icon.present? end + + def attachment_partial_name + 'paperclip' + end end