Module: Activeadmin::Dropzone::ClassMethods

Defined in:
lib/activeadmin-dropzone/active_record/dropzone.rb

Instance Method Summary collapse

Instance Method Details

#dropzone(association_name) ⇒ Object



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
# File 'lib/activeadmin-dropzone/active_record/dropzone.rb', line 9

def dropzone(association_name)
  class_eval %Q(
    def #{ association_name }_attributes=(attributes)
      reflection = self.class.reflect_on_association(:#{ association_name })

      p "self.class.reflect_on_association(:#{ association_name })"

      self.#{ association_name } = reflection.class_name.constantize.find(attributes.select{ |id, hash| !id.blank? and id != '-1' }.map{ |id, hash| id.to_i })

      self.#{ association_name }.each do |dropzone_object|
        if dropzone_object.title != attributes[dropzone_object.id.to_s]['title'] or dropzone_object.position != attributes[dropzone_object.id.to_s]['position'].to_i
          dropzone_object.update_attributes title: attributes[dropzone_object.id.to_s]['title'], position: attributes[dropzone_object.id.to_s]['position']
        end
      end

      self.update_attribute :#{ association_name }_count, self.#{ association_name }.size
    end
  )

  class_eval do
    def self.dropzone_title_field
      :title
    end

    def self.dropzone_position_field
      :position
    end

    def self.dropzone_field
      :image
    end
  end
end