Class: Attachs::Storages::Local

Inherits:
Base
  • Object
show all
Defined in:
lib/attachs/storages/local.rb

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Attachs::Storages::Base

Instance Method Details

#destroyObject



48
49
50
51
# File 'lib/attachs/storages/local.rb', line 48

def destroy
  delete realpath
  destroy_styles
end

#destroy_stylesObject



53
54
55
56
57
58
59
# File 'lib/attachs/storages/local.rb', line 53

def destroy_styles
  if attachment.image?
    attachment.styles.each do |style|
      delete realpath(style)
    end
  end
end

#process(force = false) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/attachs/storages/local.rb', line 17

def process(force=false)
  if attachment.upload?
    FileUtils.mkdir_p realpath.dirname
    attachment.upload.rewind
    File.open(realpath, 'wb') do |file|
      while chunk = attachment.upload.read(16 * 1024)
        file.write chunk
      end
    end
    attachment.uploaded!
  end
  process_styles force
end

#process_styles(force = false) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/attachs/storages/local.rb', line 31

def process_styles(force=false)
  if attachment.image?
    attachment.processors.each do |klass|
      processor = klass.new(attachment, realpath)
      attachment.styles.each do |style|
        if force == true
          delete realpath(style)
        end
        unless File.exist? realpath(style)
          FileUtils.mkdir_p realpath(style).dirname
          processor.process style, realpath(style)
        end
      end
    end
  end
end

#url(*args) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/attachs/storages/local.rb', line 5

def url(*args)
  if attachment.url?
    options = args.extract_options!
    style = (args[0] || :original)
    base_url.join(path(style)).to_s.tap do |url|
      if find_option(options, :cachebuster, Attachs.config.cachebuster)
        url << "?#{attachment.updated_at.to_i}"
      end
    end
  end
end