Module: Uploadbox::ImageUploader

Defined in:
lib/uploadbox/image_uploader.rb

Instance Method Summary collapse

Instance Method Details

#uploads_many(upload_name, options = {}) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/uploadbox/image_uploader.rb', line 144

def uploads_many(upload_name, options={})
  default_options = {
    default: false,
    placeholder: false,
    removable: true,
    retina: Uploadbox.retina,
    quality: Uploadbox.retina ? (Uploadbox.retina_quality || 40) : (Uploadbox.image_quality || 80)
  }
  options = options.reverse_merge(default_options)
  upload_versions = options.select{ |key| default_options.keys.exclude? key }
  options = options.select{ |key| default_options.keys.include? key }

  imageable_type = self.to_s
  upload_class_name = imageable_type + upload_name.to_s.camelize
  upload_class = Class.new(Image)

  Uploadbox.instance_eval {remove_const upload_class_name} if Uploadbox.const_defined?(upload_class_name)
  Uploadbox.const_set(upload_class_name, upload_class)

  # @post.images?
  define_method("#{upload_name}?") do
    upload = send(upload_name)
    upload and upload.any?
  end

  # @post.images=([id, id])
  define_method("#{upload_name}=") do |ids|


    # deals with ie8 and ie9
    if ids[0].is_a? ActionDispatch::Http::UploadedFile
      upload = upload_class.create(file: ids[0])
      self.send(upload_name).send('<<', upload)
    else
      self.send(upload_name).send('replace', [])
      for id in ids.select(&:present?)
        self.send(upload_name).send('<<', upload_class.find(id))
      end
    end
  end

  # @post.add_remote_image_url('http://exemple.com/image.jpg')
  define_method("add_remote_#{upload_name.to_s.singularize}_url") do |url|
    upload = upload_class.create!(remote_file_url: url)
    self.send(upload_name).send('<<', upload)
  end

  # Post.update_images_versions!
  self.define_singleton_method "update_#{upload_name}_versions!" do
    Uploadbox.const_get(upload_class_name).find_each{|upload| upload.file.recreate_versions!}
  end

  # Uploadbox::PostImages < Image
  upload_class.define_singleton_method :versions do
    upload_versions
  end

  upload_class.define_singleton_method :removable? do
    options[:removable]
  end

  upload_class.instance_eval do
    delegate *upload_versions.keys, to: :file

    default_scope -> { where(imageable_type: imageable_type).where(upload_name: upload_name.to_s) }

    # Uploabox::PostPictureUploader < UploadBox::ImgProcessing < CarrierWave
    dynamic_uploader = Class.new(Uploadbox::ImageProcessingUploader)
    unless Uploadbox.const_defined?(self.name.demodulize + 'Uploader')
      Uploadbox.const_set(self.name.demodulize + 'Uploader', dynamic_uploader)
    end
    dynamic_uploader.class_eval do
      upload_versions.each do |version_name, dimensions|
        if options[:retina]
          dimensions = dimensions.map{ |d| d * 2 }
        end

        version version_name do
          process resize_to_fill: dimensions
          process quality: quality = options[:quality]
        end

        def dimensions
          model.class.versions[version_name]
        end

        def width
          dimensions[0]
        end

        def height
          dimensions[1]
        end

        def processing?
          model.processing?
        end

        def original_file
          model.original_file
        end
      end
    end
    mount_uploader :file, dynamic_uploader
  end

  has_many upload_name, as: :imageable, class_name: "Uploadbox::#{self.to_s + upload_name.to_s.camelize}"
end

#uploads_one(upload_name, options = {}) ⇒ Object



3
4
5
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/uploadbox/image_uploader.rb', line 3

def uploads_one(upload_name, options={})
  default_options = {
    default: false,
    placeholder: false,
    removable: true,
    retina: Uploadbox.retina,
    quality: Uploadbox.retina ? (Uploadbox.retina_quality || 40) : (Uploadbox.image_quality || 80)
  }
  options = options.reverse_merge(default_options)
  upload_versions = options.select{ |key| default_options.keys.exclude? key }
  options = options.select{ |key| default_options.keys.include? key }

  imageable_type = self.to_s
  upload_class_name = imageable_type + upload_name.to_s.camelize
  upload_class = Class.new(Image)

  Uploadbox.instance_eval {remove_const upload_class_name} if Uploadbox.const_defined?(upload_class_name)
  Uploadbox.const_set(upload_class_name, upload_class)

  # @post.picture?
  define_method("#{upload_name}?") do
    upload = send("#{upload_name}_upload")
    !!(upload and (upload.processing? or upload.file?))
  end

  # @post.picture
  define_method(upload_name) do
    upload = send("#{upload_name}_upload")

    if upload
      upload
    else
      image = Struct.new(:url, :width, :height) do
        def to_s
          url
        end

        def processing?
          false
        end
      end

      placeholder = Class.new do
        upload_versions.keys.each do |version|
          define_method(version) do
            width = upload_versions[version][0]
            height = upload_versions[version][1]
            image.new("#{version}_#{options[:placeholder]}", width, height)
          end
        end
      end
      placeholder.new
    end
  end

  # @post.picture=(id)
  define_method("#{upload_name}=") do |upload_or_id|
    # deals with ie8 and ie9
    if upload_or_id.is_a? ActionDispatch::Http::UploadedFile
      upload = upload_class.create(file: upload_or_id)
      self.send("#{upload_name}_upload=", upload)
    elsif upload_or_id.present?
      self.send("#{upload_name}_upload=", upload_class.find(upload_or_id))
    end
  end

  # @post.remote_picture_url=('http://exemple.com/image.jpg')
  define_method("remote_#{upload_name}_url=") do |url|
    upload = Uploadbox.const_get(upload_class_name).create!(remote_file_url: url)
    self.send("#{upload_name}_upload=", upload)
  end

  # Post.update_picture_versions!
  self.define_singleton_method "update_#{upload_name}_versions!" do
    Uploadbox.const_get(upload_class_name).find_each{ |upload| upload.file.recreate_versions! if upload.file? }
  end

  # Uploadbox::PostPicture < Image
  upload_class.define_singleton_method :versions do
    upload_versions
  end

  upload_class.define_singleton_method :removable? do
    options[:removable]
  end

  upload_class.instance_eval do
    delegate *upload_versions.keys, to: :file

    default_scope -> { where(imageable_type: imageable_type).where(upload_name: upload_name.to_s) }

    # Uploabox::PostPictureUploader < UploadBox::ImgProcessing < CarrierWave
    dynamic_uploader = Class.new(Uploadbox::ImageProcessingUploader)

    unless Uploadbox.const_defined?(self.name.demodulize + 'Uploader')
      Uploadbox.const_set(self.name.demodulize + 'Uploader', dynamic_uploader)
    end
    dynamic_uploader.class_eval do
      upload_versions.each do |version_name, dimensions|
        if options[:retina]
          dimensions = dimensions.map{ |d| d * 2 }
        end

        version version_name do
          process resize_to_fill: dimensions
          process quality: quality = options[:quality]
        end

        def dimensions
          model.class.versions[version_name]
        end

        def width
          dimensions[0]
        end

        def height
          dimensions[1]
        end

        def processing?
          model.processing?
        end

        def original_file
          model.original_file
        end

      end
    end
    mount_uploader :file, dynamic_uploader

  end

  has_one "#{upload_name}_upload".to_sym, as: :imageable,
                                          class_name: "Uploadbox::#{self.to_s + upload_name.to_s.camelize}",
                                          autosave: true
  accepts_nested_attributes_for "#{upload_name}_upload".to_sym
end