From 2f8fe08d0d69da404b8acc39963b09eb4c86cbfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cezary=20K=C5=82os?= Date: Mon, 20 Mar 2023 11:38:28 +0100 Subject: [PATCH 1/6] Update trestle-active_storage.gemspec Monkey patch gemfile --- trestle-active_storage.gemspec | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/trestle-active_storage.gemspec b/trestle-active_storage.gemspec index 62e100e..1cb4309 100644 --- a/trestle-active_storage.gemspec +++ b/trestle-active_storage.gemspec @@ -17,6 +17,6 @@ Gem::Specification.new do |s| s.files = Dir['{app,config,db,lib}/**/*', 'MIT-LICENSE', 'Rakefile', 'README.md'] s.require_paths = ['lib'] - s.add_dependency 'rails', '>= 5.2', '< 8' - s.add_dependency "trestle", "~> 0.9.0", ">= 0.9.3" + s.add_dependency 'rails', '>= 5.2', '~>7' + s.add_dependency "trestle", "~> 0.9", ">= 0.9.3" end From 2e50a45c34b1034f06a54c383b866f8caf6dd3fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cezary=20K=C5=82os?= Date: Mon, 20 Mar 2023 11:46:30 +0100 Subject: [PATCH 2/6] update gemfile.lock --- Gemfile.lock | 132 +++++++++++++++++++++++++-------------------------- 1 file changed, 66 insertions(+), 66 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 9794e77..83513f4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,83 +2,83 @@ PATH remote: . specs: trestle-active_storage (3.0.1) - rails (>= 5.2, < 8) - trestle (~> 0.9.0, >= 0.9.3) + rails (~> 7, >= 5.2) + trestle (~> 0.9, >= 0.9.3) GEM remote: https://rubygems.org/ specs: - actioncable (7.0.4.1) - actionpack (= 7.0.4.1) - activesupport (= 7.0.4.1) + actioncable (7.0.4.3) + actionpack (= 7.0.4.3) + activesupport (= 7.0.4.3) nio4r (~> 2.0) websocket-driver (>= 0.6.1) - actionmailbox (7.0.4.1) - actionpack (= 7.0.4.1) - activejob (= 7.0.4.1) - activerecord (= 7.0.4.1) - activestorage (= 7.0.4.1) - activesupport (= 7.0.4.1) + actionmailbox (7.0.4.3) + actionpack (= 7.0.4.3) + activejob (= 7.0.4.3) + activerecord (= 7.0.4.3) + activestorage (= 7.0.4.3) + activesupport (= 7.0.4.3) mail (>= 2.7.1) net-imap net-pop net-smtp - actionmailer (7.0.4.1) - actionpack (= 7.0.4.1) - actionview (= 7.0.4.1) - activejob (= 7.0.4.1) - activesupport (= 7.0.4.1) + actionmailer (7.0.4.3) + actionpack (= 7.0.4.3) + actionview (= 7.0.4.3) + activejob (= 7.0.4.3) + activesupport (= 7.0.4.3) mail (~> 2.5, >= 2.5.4) net-imap net-pop net-smtp rails-dom-testing (~> 2.0) - actionpack (7.0.4.1) - actionview (= 7.0.4.1) - activesupport (= 7.0.4.1) + actionpack (7.0.4.3) + actionview (= 7.0.4.3) + activesupport (= 7.0.4.3) rack (~> 2.0, >= 2.2.0) rack-test (>= 0.6.3) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.2.0) - actiontext (7.0.4.1) - actionpack (= 7.0.4.1) - activerecord (= 7.0.4.1) - activestorage (= 7.0.4.1) - activesupport (= 7.0.4.1) + actiontext (7.0.4.3) + actionpack (= 7.0.4.3) + activerecord (= 7.0.4.3) + activestorage (= 7.0.4.3) + activesupport (= 7.0.4.3) globalid (>= 0.6.0) nokogiri (>= 1.8.5) - actionview (7.0.4.1) - activesupport (= 7.0.4.1) + actionview (7.0.4.3) + activesupport (= 7.0.4.3) builder (~> 3.1) erubi (~> 1.4) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.1, >= 1.2.0) - activejob (7.0.4.1) - activesupport (= 7.0.4.1) + activejob (7.0.4.3) + activesupport (= 7.0.4.3) globalid (>= 0.3.6) - activemodel (7.0.4.1) - activesupport (= 7.0.4.1) - activerecord (7.0.4.1) - activemodel (= 7.0.4.1) - activesupport (= 7.0.4.1) - activestorage (7.0.4.1) - actionpack (= 7.0.4.1) - activejob (= 7.0.4.1) - activerecord (= 7.0.4.1) - activesupport (= 7.0.4.1) + activemodel (7.0.4.3) + activesupport (= 7.0.4.3) + activerecord (7.0.4.3) + activemodel (= 7.0.4.3) + activesupport (= 7.0.4.3) + activestorage (7.0.4.3) + actionpack (= 7.0.4.3) + activejob (= 7.0.4.3) + activerecord (= 7.0.4.3) + activesupport (= 7.0.4.3) marcel (~> 1.0) mini_mime (>= 1.1.0) - activesupport (7.0.4.1) + activesupport (7.0.4.3) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (>= 1.6, < 2) minitest (>= 5.1) tzinfo (~> 2.0) builder (3.2.4) - concurrent-ruby (1.1.10) + concurrent-ruby (1.2.2) crass (1.0.6) date (3.3.3) erubi (1.12.0) - globalid (1.0.1) + globalid (1.1.0) activesupport (>= 5.0) i18n (1.12.0) concurrent-ruby (~> 1.0) @@ -97,7 +97,7 @@ GEM loofah (2.19.1) crass (~> 1.0.2) nokogiri (>= 1.5.9) - mail (2.8.0.1) + mail (2.8.1) mini_mime (>= 0.1.1) net-imap net-pop @@ -106,7 +106,7 @@ GEM method_source (1.0.0) mini_mime (1.1.2) mini_portile2 (2.8.1) - minitest (5.17.0) + minitest (5.18.0) net-imap (0.3.4) date net-protocol @@ -117,35 +117,35 @@ GEM net-smtp (0.3.3) net-protocol nio4r (2.5.8) - nokogiri (1.14.0) + nokogiri (1.14.2) mini_portile2 (~> 2.8.0) racc (~> 1.4) racc (1.6.2) - rack (2.2.6.2) - rack-test (2.0.2) + rack (2.2.6.4) + rack-test (2.1.0) rack (>= 1.3) - rails (7.0.4.1) - actioncable (= 7.0.4.1) - actionmailbox (= 7.0.4.1) - actionmailer (= 7.0.4.1) - actionpack (= 7.0.4.1) - actiontext (= 7.0.4.1) - actionview (= 7.0.4.1) - activejob (= 7.0.4.1) - activemodel (= 7.0.4.1) - activerecord (= 7.0.4.1) - activestorage (= 7.0.4.1) - activesupport (= 7.0.4.1) + rails (7.0.4.3) + actioncable (= 7.0.4.3) + actionmailbox (= 7.0.4.3) + actionmailer (= 7.0.4.3) + actionpack (= 7.0.4.3) + actiontext (= 7.0.4.3) + actionview (= 7.0.4.3) + activejob (= 7.0.4.3) + activemodel (= 7.0.4.3) + activerecord (= 7.0.4.3) + activestorage (= 7.0.4.3) + activesupport (= 7.0.4.3) bundler (>= 1.15.0) - railties (= 7.0.4.1) + railties (= 7.0.4.3) rails-dom-testing (2.0.3) activesupport (>= 4.2.0) nokogiri (>= 1.6) rails-html-sanitizer (1.5.0) loofah (~> 2.19, >= 2.19.1) - railties (7.0.4.1) - actionpack (= 7.0.4.1) - activesupport (= 7.0.4.1) + railties (7.0.4.3) + actionpack (= 7.0.4.3) + activesupport (= 7.0.4.3) method_source rake (>= 12.2) thor (~> 1.0) @@ -159,18 +159,18 @@ GEM activesupport (>= 5.2) sprockets (>= 3.0.0) thor (1.2.1) - timeout (0.3.1) + timeout (0.3.2) trestle (0.9.7) activemodel (>= 4.2.0) kaminari (>= 1.1.0) railties (>= 4.2.0) sprockets-rails (>= 2.0.0) - tzinfo (2.0.5) + tzinfo (2.0.6) concurrent-ruby (~> 1.0) websocket-driver (0.7.5) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.5) - zeitwerk (2.6.6) + zeitwerk (2.6.7) PLATFORMS ruby @@ -179,4 +179,4 @@ DEPENDENCIES trestle-active_storage! BUNDLED WITH - 1.16.1 + 2.1.2 From 5bccc71553952cbdb267b7d0603c60f0e7b755ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cezary=20K=C5=82os?= Date: Mon, 20 Mar 2023 12:00:08 +0100 Subject: [PATCH 3/6] Update gemspec and gemfile.lock --- Gemfile.lock | 2 +- trestle-active_storage.gemspec | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 83513f4..103b736 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,7 +2,7 @@ PATH remote: . specs: trestle-active_storage (3.0.1) - rails (~> 7, >= 5.2) + rails (>= 5.2, < 8) trestle (~> 0.9, >= 0.9.3) GEM diff --git a/trestle-active_storage.gemspec b/trestle-active_storage.gemspec index 1cb4309..8e99e4d 100644 --- a/trestle-active_storage.gemspec +++ b/trestle-active_storage.gemspec @@ -17,6 +17,6 @@ Gem::Specification.new do |s| s.files = Dir['{app,config,db,lib}/**/*', 'MIT-LICENSE', 'Rakefile', 'README.md'] s.require_paths = ['lib'] - s.add_dependency 'rails', '>= 5.2', '~>7' + s.add_dependency 'rails', '>= 5.2', '< 8' s.add_dependency "trestle", "~> 0.9", ">= 0.9.3" end From 7e4845553e68ade0de5b3bc0dcb05d21670f98e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cezary=20K=C5=82os?= Date: Mon, 8 May 2023 10:06:55 +0200 Subject: [PATCH 4/6] Use attach method to add files in has_many_attachments relation. THIS is supported for rails >= 6. bump minor version --- Gemfile.lock | 8 +++----- .../active_storage/_has_many_field.html.erb | 4 +++- .../active_storage/controller_concern.rb | 17 +++++++++++++++++ lib/trestle/active_storage/version.rb | 2 +- trestle-active_storage.gemspec | 2 +- 5 files changed, 25 insertions(+), 8 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 103b736..8c84aa6 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,8 +1,8 @@ PATH remote: . specs: - trestle-active_storage (3.0.1) - rails (>= 5.2, < 8) + trestle-active_storage (3.1.0) + rails (>= 6, < 8) trestle (~> 0.9, >= 0.9.3) GEM @@ -105,7 +105,6 @@ GEM marcel (1.0.2) method_source (1.0.0) mini_mime (1.1.2) - mini_portile2 (2.8.1) minitest (5.18.0) net-imap (0.3.4) date @@ -117,8 +116,7 @@ GEM net-smtp (0.3.3) net-protocol nio4r (2.5.8) - nokogiri (1.14.2) - mini_portile2 (~> 2.8.0) + nokogiri (1.14.2-x86_64-darwin) racc (~> 1.4) racc (1.6.2) rack (2.2.6.4) diff --git a/app/views/trestle/active_storage/_has_many_field.html.erb b/app/views/trestle/active_storage/_has_many_field.html.erb index 228b05d..8c0529b 100644 --- a/app/views/trestle/active_storage/_has_many_field.html.erb +++ b/app/views/trestle/active_storage/_has_many_field.html.erb @@ -10,11 +10,13 @@ <% elsif attachment.variable? %> <%= link_to image_tag(main_app.url_for(attachment.variant(resize_to_limit: [300, 300])), class: "active-storage__image"), main_app.rails_blob_path(attachment, disposition: "attachment") %> - <% elsif attachment.persisted? %> + <% else %> <%= link_to main_app.rails_blob_path(attachment, disposition: "attachment"), class: "btn btn-info" do %> Download <%= field_name %> <% end %> + <% end %> + <% if attachment.persisted? %>
<%= attachment.blob.filename %> <%= builder.check_box("delete_#{field_name}_#{attachment.blob_id.to_s.gsub('-', '')}", label: "Delete this #{field_name.to_s.singularize}") %> diff --git a/lib/trestle/active_storage/controller_concern.rb b/lib/trestle/active_storage/controller_concern.rb index 43ab3a3..fff6353 100644 --- a/lib/trestle/active_storage/controller_concern.rb +++ b/lib/trestle/active_storage/controller_concern.rb @@ -5,10 +5,27 @@ module ControllerConcern included do before_action :define_attachment_accessors, only: [:show, :edit, :update, :destroy] + before_action :filter_has_many_attachments, only: [:update] after_action :purge_attachments, only: [:update] + after_action :attach_attachments, only: [:update] end private + def filter_has_many_attachments + admin.active_storage_fields.each do |field| + if instance.send(field).respond_to?(:each) + if params.key?(admin.parameter_name) + @removed_attachments_field = field + @removed_attachments = params[admin.parameter_name].delete field.to_sym + end + end + end + end + + def attach_attachments + instance.send(@removed_attachments_field).attach(@removed_attachments) + end + def define_attachment_accessors self.instance = admin.find_instance(params) diff --git a/lib/trestle/active_storage/version.rb b/lib/trestle/active_storage/version.rb index 3ab6241..62ee2e0 100644 --- a/lib/trestle/active_storage/version.rb +++ b/lib/trestle/active_storage/version.rb @@ -1,5 +1,5 @@ module Trestle module ActiveStorage - VERSION = '3.0.1' + VERSION = '3.1.0' end end diff --git a/trestle-active_storage.gemspec b/trestle-active_storage.gemspec index 8e99e4d..fba7618 100644 --- a/trestle-active_storage.gemspec +++ b/trestle-active_storage.gemspec @@ -17,6 +17,6 @@ Gem::Specification.new do |s| s.files = Dir['{app,config,db,lib}/**/*', 'MIT-LICENSE', 'Rakefile', 'README.md'] s.require_paths = ['lib'] - s.add_dependency 'rails', '>= 5.2', '< 8' + s.add_dependency 'rails', '>= 6', '< 8' s.add_dependency "trestle", "~> 0.9", ">= 0.9.3" end From 282e570ead8b937b19adb0f610db7674a793dd0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cezary=20K=C5=82os?= Date: Mon, 29 May 2023 09:54:26 +0200 Subject: [PATCH 5/6] Add guard clause to prevent any update from attaching not existent fields --- .../active_storage/controller_concern.rb | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/lib/trestle/active_storage/controller_concern.rb b/lib/trestle/active_storage/controller_concern.rb index fff6353..3acab77 100644 --- a/lib/trestle/active_storage/controller_concern.rb +++ b/lib/trestle/active_storage/controller_concern.rb @@ -11,20 +11,26 @@ module ControllerConcern end private - def filter_has_many_attachments - admin.active_storage_fields.each do |field| - if instance.send(field).respond_to?(:each) - if params.key?(admin.parameter_name) - @removed_attachments_field = field - @removed_attachments = params[admin.parameter_name].delete field.to_sym - end + def filter_has_many_attachments + return if admin.active_storage_fields.empty? + + @removed_attachments = Hash.new + admin.active_storage_fields.each do |field| + if instance.send(field).respond_to?(:each) + if params.key?(admin.parameter_name) + @removed_attachments[field] = params[admin.parameter_name].delete field.to_sym end end end + end + + def attach_attachments + return if @removed_attachments.nil? || @removed_attachments.empty? - def attach_attachments - instance.send(@removed_attachments_field).attach(@removed_attachments) + @removed_attachments.each do |field, attachments| + instance.send(field).attach(attachments) end + end def define_attachment_accessors self.instance = admin.find_instance(params) From 7c091e62c9cae6f9906fc6a9c0142ba3e0959804 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cezary=20K=C5=82os?= Date: Wed, 7 Feb 2024 16:55:39 +0100 Subject: [PATCH 6/6] Update lib/trestle/active_storage/controller_concern.rb Co-authored-by: taogt --- lib/trestle/active_storage/controller_concern.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/trestle/active_storage/controller_concern.rb b/lib/trestle/active_storage/controller_concern.rb index 3acab77..74fbc0e 100644 --- a/lib/trestle/active_storage/controller_concern.rb +++ b/lib/trestle/active_storage/controller_concern.rb @@ -25,7 +25,7 @@ def filter_has_many_attachments end def attach_attachments - return if @removed_attachments.nil? || @removed_attachments.empty? + return if @removed_attachments.blank? @removed_attachments.each do |field, attachments| instance.send(field).attach(attachments)