From e741ac5dcc31167355ad3d9b6fc57872abcf62ed Mon Sep 17 00:00:00 2001 From: Krylov Alexey Date: Tue, 19 May 2015 13:01:54 +1000 Subject: [PATCH 1/2] =?UTF-8?q?=D0=A4=D0=B8=D0=BA=D1=81=20=D0=BE=D1=88?= =?UTF-8?q?=D0=B8=D0=B1=D0=BA=D0=B8=20Unpermission=20Params,=20=D0=BE?= =?UTF-8?q?=D0=B1=D0=BD=D0=BE=D0=B2=D0=B8=D0=BB=20Readme:=20=D0=B4=D0=BE?= =?UTF-8?q?=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20=D0=B8=D0=BD=D1=84=D0=BE=D1=80?= =?UTF-8?q?=D0=BC=D0=B0=D1=86=D0=B8=D1=8E=20=D0=BE=20=D0=BD=D0=B5=D0=BE?= =?UTF-8?q?=D0=B1=D1=85=D0=BE=D0=B4=D0=B8=D0=BC=D0=BE=D1=81=D1=82=D0=B8=20?= =?UTF-8?q?=D0=B0=D1=82=D1=80=D0=B8=D0=B1=D1=83=D1=82=D0=B0=20title?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 10 ++++++++++ app/views/application/_dropzone.html.erb | 1 - lib/activeadmin-dropzone/active_record/dropzone.rb | 3 ++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c1d4c90..b5ce798 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,16 @@ Add `dropzone_item` to your file's class: dropzone_item end +Add method `title` in the model if there is no such attribute: + + class Image + has_attached_file :file + + def title + file_file_name + end + end + Add `input` to your ActiveAdmin form: f.input :images, as: :dropzone diff --git a/app/views/application/_dropzone.html.erb b/app/views/application/_dropzone.html.erb index 8a8c458..76b3278 100644 --- a/app/views/application/_dropzone.html.erb +++ b/app/views/application/_dropzone.html.erb @@ -1,6 +1,5 @@
<%=t 'dropzone.drag_or_click' %> - <%= hidden_field_tag "#{ object.class.model_name.singular }[#{ method }_attributes][-1]", "" %> <% object.send(method).each_with_index do |dropzone_object, index| %> <%= hidden_field_tag "#{ object.class.model_name.singular }[#{ method }_attributes][#{ dropzone_object.id }][#{ object.class.send("dropzone_#{ method }_field", :title) }]", dropzone_object.send(object.class.send("dropzone_#{ method }_field", :title)) %> <% end %> diff --git a/lib/activeadmin-dropzone/active_record/dropzone.rb b/lib/activeadmin-dropzone/active_record/dropzone.rb index 26b2d01..0acf1a1 100644 --- a/lib/activeadmin-dropzone/active_record/dropzone.rb +++ b/lib/activeadmin-dropzone/active_record/dropzone.rb @@ -76,7 +76,8 @@ def data_url def as_json(options = { }) super(options).merge({ - url: data_url + url: data_url, + title: title }) end ) From cfa57222a3bd77a29492d1cf93ed23cf80406c9d Mon Sep 17 00:00:00 2001 From: Krylov Alexey Date: Mon, 28 Sep 2015 15:26:48 +1000 Subject: [PATCH 2/2] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8=D0=BB?= =?UTF-8?q?=20=D1=81=D0=BF=D0=BE=D1=81=D0=BE=D0=B1=20=D1=81=D0=B2=D1=8F?= =?UTF-8?q?=D0=B7=D1=8B=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F=20=D0=BC=D0=BE=D0=B4?= =?UTF-8?q?=D0=B5=D0=BB=D0=B5=D0=B9:=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8?= =?UTF-8?q?=D0=B7=D0=B0=D1=86=D0=B8=D1=8F=20=D1=87=D0=B5=D1=80=D0=B5=D0=B7?= =?UTF-8?q?=20accepts=5Fnested=5Fattributes=5Ffor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 26 ++++++---- .../view_helpers/dropzone_helper.rb | 5 +- app/views/application/_dropzone.html.erb | 23 +++++---- .../active_record/dropzone.rb | 51 +------------------ 4 files changed, 34 insertions(+), 71 deletions(-) diff --git a/README.md b/README.md index b5ce798..caadbfb 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,10 @@ Easy to use integration of drag&drop files upload via [dropzone.js](http://www.dropzonejs.com) for [ActiveAdmin](http://www.activeadmin.info). +## History + +* 28.09.2015 - change model association: delete self-writed association, add `accepts_nested_attributes_for`. + ## Requirements * [ActiveAdmin](http://www.activeadmin.info); @@ -13,18 +17,12 @@ Easy to use integration of drag&drop files upload via [dropzone.js](http://www.d Add line to your Gemfile: - gem 'activeadmin-dropzone', '~> 0.2.1' - -Add `dropzone` to your file container's class: - - class Post - dropzone :images - end + gem 'activeadmin-dropzone', github: 'farpostdesign/activeadmin-dropzone' Add `dropzone_item` to your file's class: class Image - dropzone_item + dropzone_item container_id: :page_id end Add method `title` in the model if there is no such attribute: @@ -37,6 +35,14 @@ Add method `title` in the model if there is no such attribute: end end +Add permitions to `permit_params` method for your Active Admin file, for example: + + permit_params :title, + :annotation, + :description, + ..., + images_attributes: [:id, :title, :position, :_destroy] # for model Image + Add `input` to your ActiveAdmin form: f.input :images, as: :dropzone @@ -53,7 +59,9 @@ You can customize columns used for upload by passing `Hash` to the `dropzone_ite position: :position, data: :data, file_size: :data_file_size, - url: :data_url + url: :data_url, + container_type: :page_type, # only for polymorphic associations! + container_id: :page_id, # required! ## Contributing to activeadmin-dropzone diff --git a/app/helpers/active_admin/view_helpers/dropzone_helper.rb b/app/helpers/active_admin/view_helpers/dropzone_helper.rb index b5ee203..030a8fc 100644 --- a/app/helpers/active_admin/view_helpers/dropzone_helper.rb +++ b/app/helpers/active_admin/view_helpers/dropzone_helper.rb @@ -1,12 +1,13 @@ module ActiveAdmin::ViewHelpers::DropzoneHelper def render_mock_dropzone_files(dropzone_objects) - dropzone_objects.map do |dropzone_object| + dropzone_objects.map.with_index do |dropzone_object, index| { id: dropzone_object.id, name: dropzone_object.send(dropzone_object.class.dropzone_field(:title)).squish, size: dropzone_object.send(dropzone_object.class.dropzone_field(:file_size)), - url: dropzone_object.send(dropzone_object.class.dropzone_field(:url)) + url: dropzone_object.send(dropzone_object.class.dropzone_field(:url)), + index: index, } end.to_json.html_safe end diff --git a/app/views/application/_dropzone.html.erb b/app/views/application/_dropzone.html.erb index 76b3278..66a1a59 100644 --- a/app/views/application/_dropzone.html.erb +++ b/app/views/application/_dropzone.html.erb @@ -1,7 +1,7 @@
<%=t 'dropzone.drag_or_click' %> <% object.send(method).each_with_index do |dropzone_object, index| %> - <%= hidden_field_tag "#{ object.class.model_name.singular }[#{ method }_attributes][#{ dropzone_object.id }][#{ object.class.send("dropzone_#{ method }_field", :title) }]", dropzone_object.send(object.class.send("dropzone_#{ method }_field", :title)) %> + <%= hidden_field_tag "#{ object.class.model_name.singular }[#{ method }_attributes][#{ index }][id]", dropzone_object.id %> <% end %>
@@ -14,9 +14,10 @@ Dropzone.autoDiscover = false; var mockFiles = <%= render_mock_dropzone_files(object.send(method)) %>; + var mockFilesCount = mockFiles.length; var dropzone_id = "#dropzone_<%= method %>_<%= current_cycle(method) %>"; var dropzone = new Dropzone(dropzone_id, { - url: '<%= raw upload_dropzone_path(dropzonable_id: (object.new_record? ? nil : object.id), dropzonable_class: object.class, dropzone_class: object.class.send("dropzone_#{ method }_class")) %>', + url: '<%= raw upload_dropzone_path(dropzonable_id: (object.new_record? ? nil : object.id), dropzonable_class: object.class, dropzone_class: object.send(method).klass.to_s) %>', addRemoveLinks: true, dictCancelUpload: '<%=t "dropzone.cancel" %>', dictRemoveFile: '<%=t "dropzone.remove" %>', @@ -25,15 +26,15 @@ xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').prop('content')); }, success: function(file, data) { - $(dropzone_id).append(']" value="' + data.title + '" />'); + $(dropzone_id).append(''); $(file.previewElement).attr('data-id', data.id); $(file.previewElement).attr('data-url', data.url); + $(file.previewElement).attr('data-index', mockFilesCount++); $(file.previewElement).find('.dz-filename span').text(data.title); return file.previewElement.classList.add("dz-success"); }, removedfile: function(file) { - $(dropzone_id + ' input[name="<%= object.class.model_name.singular %>[<%= method %>_attributes][' + file.id + '][<%= object.class.send("dropzone_#{ method }_field", :title) %>]"]').remove(); - $(dropzone_id + ' input[name="<%= object.class.model_name.singular %>[<%= method %>_attributes][' + file.id + '][<%= object.class.send("dropzone_#{ method }_field", :position) %>]"]').remove(); + $(dropzone_id).append(''); $(file.previewElement).remove(); $(dropzone_id).sortable('refresh'); } @@ -45,12 +46,14 @@ }); var func = function(file) { - if (file.id) + if (file.id) { $(file.previewElement).attr('data-id', file.id); + $(file.previewElement).attr('data-index', file.index); + } $(file.previewElement).find('.dz-filename, img').on('click', function() { - var id = $(this).parents('.dz-preview:first').attr('data-id'); - var input = $(dropzone_id + ' input[name="<%= object.class.model_name.singular %>[<%= method %>_attributes][' + id + '][<%= object.class.send("dropzone_#{ method }_field", :title) %>]"]'); + var index = $(this).parents('.dz-preview:first').attr('data-index'); + var input = $(dropzone_id + ' input[name="<%= object.class.model_name.singular %>[<%= method %>_attributes][' + index + '][id]"]'); var title = prompt("<%=t 'dropzone.title' %>: ", $(file.previewElement).find('.dz-filename span').text()); $(file.previewElement).find('.dz-filename span').text(title); input.val(title); @@ -60,8 +63,8 @@ var generate_dropzone_position_elements = function() { var position = 0; $(dropzone_id + ' .dz-preview').each(function() { - var id = $(this).attr('data-id'); - $(dropzone_id).append(']" value="' + (position++) + '" />'); + var index = $(this).attr('data-index'); + $(dropzone_id).append(']" value="' + (position++) + '" />'); }); }; diff --git a/lib/activeadmin-dropzone/active_record/dropzone.rb b/lib/activeadmin-dropzone/active_record/dropzone.rb index 0acf1a1..28f6e48 100644 --- a/lib/activeadmin-dropzone/active_record/dropzone.rb +++ b/lib/activeadmin-dropzone/active_record/dropzone.rb @@ -3,62 +3,13 @@ module Dropzone extend ActiveSupport::Concern module ClassMethods - def dropzone(association_name) - class_eval %Q( - def self.dropzone_#{ association_name }_class - self.reflect_on_association(:#{ association_name }).class_name.constantize - end - - def #{ association_name }_attributes=(attributes) - original_collection = self.#{ association_name }.to_a - - ActiveRecord::Base.transaction do - self.#{ association_name } = self.class.dropzone_#{ association_name }_class.find(attributes.select{ |id, hash| !id.blank? and id != '-1' }.map{ |id, hash| id.to_i }) - - self.#{ association_name }.each do |dropzone_object| - attribute = attributes[dropzone_object.id.to_s] - changes = {} - - title_field = self.class.dropzone_#{ association_name }_class.dropzone_field(:title) - if dropzone_object.respond_to?(title_field) and dropzone_object.send(title_field) != attribute['title'] - changes[title_field] = attribute['title'] - end - - position_field = self.class.dropzone_#{ association_name }_class.dropzone_field(:position) - if dropzone_object.respond_to?(position_field) and dropzone_object.send(position_field) != attribute['position'].to_i - changes[position_field] = attribute['position'] - end - - unless changes.empty? - dropzone_object.update_attributes changes - end - end - - self.update_attribute :#{ association_name }_count, self.#{ association_name }.count if self.respond_to?(:#{ association_name }_count) - - (original_collection - self.#{ association_name }).each do |object| - object.destroy - end - end - end - - def self.dropzone_#{ association_name }_field(key) - dropzone_#{ association_name }_class.dropzone_field(key) - end - - def self.dropzone_#{ association_name }_field?(key) - dropzone_#{ association_name }_class.dropzone_field?(key) - end - ) - end - def dropzone_item(options = {}) options = { title: :title, position: :position, data: :data, file_size: :data_file_size, - url: :data_url + url: :data_url, }.deep_merge(options) class_eval %Q(