Module: Activeadmin::Dropzone::ClassMethods
- Defined in:
- lib/activeadmin-dropzone/active_record/dropzone.rb
Instance Method Summary collapse
Instance Method Details
#dropzone(association_name) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/activeadmin-dropzone/active_record/dropzone.rb', line 6 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 }.size 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 |
#dropzone_item(options = {}) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/activeadmin-dropzone/active_record/dropzone.rb', line 55 def dropzone_item( = {}) = { title: :title, position: :position, data: :data, file_size: :data_file_size, url: :data_url }.deep_merge() class_eval %Q( def self.dropzone_field(key) #{ }[key.to_sym] end def self.dropzone_field?(key) #{ }.keys.include? key.to_sym end def #{ [:data] }_url #{ [:data] }.url end ) end |