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

Use attach method for has many attachments #122

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 67 additions & 69 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,84 +1,84 @@
PATH
remote: .
specs:
trestle-active_storage (3.0.1)
rails (>= 5.2, < 8)
trestle (~> 0.9.0, >= 0.9.3)
trestle-active_storage (3.1.0)
rails (>= 6, < 8)
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)
Expand All @@ -97,16 +97,15 @@ 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
net-smtp
marcel (1.0.2)
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
Expand All @@ -117,35 +116,34 @@ GEM
net-smtp (0.3.3)
net-protocol
nio4r (2.5.8)
nokogiri (1.14.0)
mini_portile2 (~> 2.8.0)
nokogiri (1.14.2-x86_64-darwin)
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)
Expand All @@ -159,18 +157,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
Expand All @@ -179,4 +177,4 @@ DEPENDENCIES
trestle-active_storage!

BUNDLED WITH
1.16.1
2.1.2
4 changes: 3 additions & 1 deletion app/views/trestle/active_storage/_has_many_field.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>
<i class="fa fa-download"></i>
<span class="btn-label">Download <%= field_name %></span>
<% end %>
<% end %>
<% if attachment.persisted? %>
<div>
<small><%= attachment.blob.filename %></small>
<%= builder.check_box("delete_#{field_name}_#{attachment.blob_id.to_s.gsub('-', '')}", label: "Delete this #{field_name.to_s.singularize}") %>
Expand Down
23 changes: 23 additions & 0 deletions lib/trestle/active_storage/controller_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,33 @@ 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
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.blank?

@removed_attachments.each do |field, attachments|
instance.send(field).attach(attachments)
end
end

def define_attachment_accessors
self.instance = admin.find_instance(params)

Expand Down
2 changes: 1 addition & 1 deletion lib/trestle/active_storage/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Trestle
module ActiveStorage
VERSION = '3.0.1'
VERSION = '3.1.0'
end
end
4 changes: 2 additions & 2 deletions trestle-active_storage.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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', '>= 6', '< 8'
s.add_dependency "trestle", "~> 0.9", ">= 0.9.3"
end