Class: ImageThread::Uploaders::ImageUploader

Inherits:
CarrierWave::Uploader::Base
  • Object
show all
Includes:
CarrierWave::MiniMagick
Defined in:
lib/image_thread/uploaders/image_uploader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#name=(value) ⇒ Object (writeonly)

Sets the attribute name

Parameters:

  • value

    the value to set the attribute name to.



6
7
8
# File 'lib/image_thread/uploaders/image_uploader.rb', line 6

def name=(value)
  @name = value
end

Instance Method Details

#extension_white_listObject



41
42
43
# File 'lib/image_thread/uploaders/image_uploader.rb', line 41

def extension_white_list
  %w(jpg jpeg gif png)
end

#filenameObject



45
46
47
# File 'lib/image_thread/uploaders/image_uploader.rb', line 45

def filename
  @name ||= "#{secure_token(25)}.jpg" if original_filename.present?
end

#move_to_storeObject



55
56
57
# File 'lib/image_thread/uploaders/image_uploader.rb', line 55

def move_to_store
  true
end

#store_dirObject



49
50
51
52
53
# File 'lib/image_thread/uploaders/image_uploader.rb', line 49

def store_dir
  dir = model.dir || 'all'

  ['uploads', 'images', dir, model.thread_id].join('/')
end

#thumb(size) ⇒ Object



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
# File 'lib/image_thread/uploaders/image_uploader.rb', line 12

def thumb(size)
  uploader = Class.new(self.class)
  ::CarrierWave::Uploader.const_set("Uploader#{uploader.object_id}".gsub('-', '_'), uploader)

  uploader.versions.clear
  uploader.version_names = [size]

  img = uploader.new(self.model)
  img.retrieve_from_store!(self.file.identifier)
  cached = File.join(CarrierWave.root, img.url)

  unless File.exist?(cached)
    file.copy_to(cached)

    size   = size.split('x').map(&:to_i)
    resize = case size
                when /!$/ then :resize_to_fit
                when /\#$/ then :resize_to_limit
                else :resize_to_fill
             end

    img.send resize, *size
    img.name = file.original_filename
    img.store!
  end

  img
end