Last active
February 10, 2017 11:43
-
-
Save HK49/acdf0050eb1fc2adcf10f75373e1d601 to your computer and use it in GitHub Desktop.
Update action for Carrierwave 1.0 Multifile upload with single controller (delete several files or delete all)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= content_tag :div, :class => "gallery_rows" do -%> | |
<% gallery.photo.each do |image| %> | |
<%= content_tag :div, :class => "gallery_image", :id => "gallery_image_#{gallery.photo.index(image)+1}" do -%><%# delete by index %> | |
<%= content_tag :div, :class => "imagethumb" do -%> | |
<%= image_tag(image.thumb.url) if gallery.photo? -%> | |
<% end %> | |
<p>Delete photo?</p> | |
<div class="imageswitch"> | |
<%= check_box_tag 'delete_this_images[]', gallery.photo.index(image), | |
false, id: "delete_image_#{gallery.photo.index(image)+1}", class: 'imageswitch-cb' | |
-%> | |
<%= | |
label_tag "delete_image_#{gallery.photo.index(image)+1}", nil, class: 'imageswitch_label' do | |
concat content_tag(:div, '', class: 'imageswitch-inner') | |
concat content_tag(:div, '', class: 'imageswitch-switch') | |
end | |
-%> | |
</div> | |
<% end %> | |
<% end %> | |
<% end unless gallery.new_record? || !gallery.photo? %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ArrayRefinement | |
refine Array do | |
def delete_at_multi(arr) | |
arr = arr.sort.reverse! | |
arr.each { |i| self.delete_at i } | |
self | |
end | |
end | |
end | |
=begin | |
from http://stackoverflow.com/a/7964648/4814907 | |
delete several by index items from array | |
delete highest indexes first: iterate in reverse, so | |
it deletes behind the iteration not ahead of it, so | |
elements are not shifted | |
array.delete_at is mutating the array while you are iterating. | |
does not save db if arr is nil, see controller | |
=end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using ArrayRefinement | |
# ... | |
def update | |
if (gallery.update_attributes gallery_params.except(:photo)) # if success - operate photos: | |
if params[params[:controller].classify.underscore.to_sym][:photo].present? || params[:delete_this_images].present? | |
photos = gallery.photo # getter | |
# add new image to the old ones: | |
photos += params[params[:controller].classify.underscore.to_sym][:photo] unless | |
params[params[:controller].classify.underscore.to_sym][:photo].nil? #no implicit conversion of nil into array | |
# delete several old images(only in db, not in public folder): | |
if params[:delete_this_images].present? # nilClass check | |
photos.delete_at_multi(params[:delete_this_images].map(&:to_i)) #delete pictures by index got by checkbox in view | |
end # see module bellow for details | |
gallery.photo = photos # setter | |
end | |
if gallery.photo.size.zero? #if deleted all photos | |
FileUtils.rm_rf("public/uploads/#{params[:controller].classify.underscore.to_sym}/photo/#{params[:id]}") #empty folder | |
gallery.update_column(:photo, nil) #empty column | |
end | |
if gallery.save | |
flash[:notice] = t(:edited) + " " + t(:success).concat("!") | |
redirect_to (send("#{params[:controller].classify.underscore.to_sym}_path")) | |
else | |
render :edit | |
end | |
else | |
render :edit | |
end | |
end | |
# ... | |
private | |
helper_method def gallery | |
@gallery ||= found_gallery || nil # ... | |
end | |
# ... | |
def found_gallery | |
%w(show edit update destroy). | |
include?(params[:action]) && params[:controller]. | |
classify.constantize.find(params[:id]) | |
end | |
# ... | |
def gallery_params | |
arr = [{photo: []}] # ... | |
arr += [{delete_this_images: []}] if %w(edit update).include?(params[:action]) | |
params.require(params[:controller].classify.underscore.to_sym).permit(arr) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment