From f6f64bacefc750d54ec07b7600056e11e85d01c4 Mon Sep 17 00:00:00 2001 From: Jody Albritton Date: Mon, 21 Jul 2014 04:17:46 -0500 Subject: [PATCH] add multiple upload to gallery --- Gemfile | 1 + Gemfile.lock | 4 +++ app/assets/javascripts/admin/admin.js | 3 ++ app/assets/javascripts/admin/photos.js.coffee | 24 +++++++++++++ app/controllers/admin/photos_controller.rb | 2 +- app/views/admin/photos/_form.html.erb | 34 ++++++++++++------- app/views/admin/photos/_upload.html.erb | 1 + app/views/admin/photos/create.js.erb | 5 +++ 8 files changed, 61 insertions(+), 13 deletions(-) mode change 100644 => 100755 app/assets/javascripts/admin/photos.js.coffee create mode 100755 app/views/admin/photos/_upload.html.erb create mode 100755 app/views/admin/photos/create.js.erb diff --git a/Gemfile b/Gemfile index 60c835f..1421237 100755 --- a/Gemfile +++ b/Gemfile @@ -63,6 +63,7 @@ gem 'high_voltage', github: 'thoughtbot/high_voltage' gem 'acts_as_list' gem 'pusher' gem 'jquery-datatables-rails', '~> 2.1.10.0.2' +gem 'jquery-fileupload-rails' gem 'lodash-rails' gem 'ancestry' diff --git a/Gemfile.lock b/Gemfile.lock index 5e4dfa3..44b5dd9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -121,6 +121,9 @@ GEM jquery-datatables-rails (2.1.10.0.3) jquery-rails sass-rails + jquery-fileupload-rails (0.4.1) + actionpack (>= 3.1) + railties (>= 3.1) jquery-rails (3.1.1) railties (>= 3.0, < 5.0) thor (>= 0.14, < 2.0) @@ -323,6 +326,7 @@ DEPENDENCIES high_voltage! jbuilder (~> 2.0) jquery-datatables-rails (~> 2.1.10.0.2) + jquery-fileupload-rails jquery-rails jquery-turbolinks jquery-ui-rails diff --git a/app/assets/javascripts/admin/admin.js b/app/assets/javascripts/admin/admin.js index 7011962..685e041 100755 --- a/app/assets/javascripts/admin/admin.js +++ b/app/assets/javascripts/admin/admin.js @@ -39,6 +39,9 @@ //= require dataTables/bootstrap/3/jquery.dataTables.bootstrap //= require dataTables/extras/dataTables.responsive //= require dataTables/extras/dataTables.tableTools +//= require jquery-fileupload/basic +//= require jquery-fileupload/vendor/tmpl +//= require admin/photos //= require admin/yt_player //= require cocoon //= require autonumeric diff --git a/app/assets/javascripts/admin/photos.js.coffee b/app/assets/javascripts/admin/photos.js.coffee old mode 100644 new mode 100755 index 24f83d1..aeb94db --- a/app/assets/javascripts/admin/photos.js.coffee +++ b/app/assets/javascripts/admin/photos.js.coffee @@ -1,3 +1,27 @@ # Place all the behaviors and hooks related to the matching controller here. # All this logic will automatically be available in application.js. # You can use CoffeeScript in this file: http://coffeescript.org/ + +jQuery -> + $('#photo_file').attr('name','photo[file]') + $('#new_photo').fileupload + dataType: 'script' + add: (e, data) -> + types = /(\.|\/)(gif|jpe?g|png|mov|mpeg|mpeg4|avi)$/i + file = data.files[0] + if types.test(file.type) || types.test(file.name) + data.context = $(tmpl("template-upload", file)) + $('#new_photo').append(data.context) + $('.actions input[type="submit"]').click (e) -> + $('.actions input[type="submit"]').attr("disabled", true) + data.submit() + e.preventDefault() + else + alert("#{file.name} is not a gif, jpg or png image file") + progress: (e, data) -> + if data.context + progress = parseInt(data.loaded / data.total * 100, 10) + data.context.find('.bar').css('width', progress + '%') + done: (e, data) -> + $('.actions input[type="submit"]').off('click') + $('.actions input[type="submit"]').attr("enabled", true) \ No newline at end of file diff --git a/app/controllers/admin/photos_controller.rb b/app/controllers/admin/photos_controller.rb index 56ac231..1c3c0fc 100755 --- a/app/controllers/admin/photos_controller.rb +++ b/app/controllers/admin/photos_controller.rb @@ -52,7 +52,7 @@ def set_photo # Never trust parameters from the scary internet, only allow the white list through. def photo_params - params.require(:photo).permit(:file, :gallery_id, :category_id, :gallery, :category) + params.require(:photo).permit(:file, :gallery_id, :category_id, :gallery, :category, :uploaded_file) end end diff --git a/app/views/admin/photos/_form.html.erb b/app/views/admin/photos/_form.html.erb index e525166..1c51c16 100755 --- a/app/views/admin/photos/_form.html.erb +++ b/app/views/admin/photos/_form.html.erb @@ -1,18 +1,28 @@ <% @photo = @gallery.photos.new %> -<%= form_for [:admin, @gallery, @photo], :class=>"form-horizontal panel" do |f| %> +<%= form_for [:admin, @gallery, @photo], :class=>"form-horizontal panel", :html=> {multipart: true} do |f| %>
- <%= f.file_field :file %> -
- + <% if @photo.errors.any? %> +
+

<%= pluralize(@photo.errors.count, "error") %> prohibited this photo from being saved:

+ +
+ <% end %> + + + + <%= f.file_field :file, multiple: true, name: 'photo[file]', :hint=> "You can select multiple files" %> + + + <% end %> - \ No newline at end of file +<% # jquery photo template # %> + diff --git a/app/views/admin/photos/_upload.html.erb b/app/views/admin/photos/_upload.html.erb new file mode 100755 index 0000000..516d251 --- /dev/null +++ b/app/views/admin/photos/_upload.html.erb @@ -0,0 +1 @@ +<%= image_tag(@photo.file.url(:small), class: 'thumb') %> \ No newline at end of file diff --git a/app/views/admin/photos/create.js.erb b/app/views/admin/photos/create.js.erb new file mode 100755 index 0000000..89bdaf3 --- /dev/null +++ b/app/views/admin/photos/create.js.erb @@ -0,0 +1,5 @@ +<% if @photo.new_record? %> + alert('Failed'); +<% else %> + $('ul.thumbnails').append("<%=j render partial: 'photos/upload', locals: { photo: @photo } %>"); +<% end %> \ No newline at end of file