Class: Sequel::Plugins::Paperclip::Attachment

Inherits:
Object
  • Object
show all
Defined in:
lib/sequel_paperclip/attachment.rb

Constant Summary collapse

STORAGE_UPDATE_SAVE =
1
STORAGE_UPDATE_DELETE =
2

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, name, preprocessed_options) ⇒ Attachment

Returns a new instance of Attachment.



36
37
38
39
40
41
# File 'lib/sequel_paperclip/attachment.rb', line 36

def initialize(model, name, preprocessed_options)
  @model = model
  @name = name
  @options = preprocessed_options
  @storage_updates = []
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



5
6
7
# File 'lib/sequel_paperclip/attachment.rb', line 5

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/sequel_paperclip/attachment.rb', line 6

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/sequel_paperclip/attachment.rb', line 7

def options
  @options
end

#queued_fileObject (readonly)

Returns the value of attribute queued_file.



8
9
10
# File 'lib/sequel_paperclip/attachment.rb', line 8

def queued_file
  @queued_file
end

Class Method Details

.preprocess_options(options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/sequel_paperclip/attachment.rb', line 13

def self.preprocess_options(options = {})
  unless options[:styles]
    options[:styles] = {
      :original => {}
    }
  end

  unless options[:processors]
    options[:processors] = [
      {
        :type => :dummy,
      }
    ]
  end

  options[:processors].each_with_index do |processor, i|
    if processor.is_a?(Hash)
      klass = "Sequel::Plugins::Paperclip::Processors::#{processor[:type].to_s.capitalize}"
      options[:processors][i] = klass.constantize.new(self, processor)
    end
  end
end

Instance Method Details

#consistent?Boolean

Returns:

  • (Boolean)


70
71
72
73
74
75
# File 'lib/sequel_paperclip/attachment.rb', line 70

def consistent?
  options[:styles].each_key do |style|
    return false unless File.exists?(path(style))
  end
  true
end

#exists?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/sequel_paperclip/attachment.rb', line 66

def exists?
  !!model.send("#{name}_basename")
end

#path(style = :original) ⇒ Object



77
78
79
# File 'lib/sequel_paperclip/attachment.rb', line 77

def path(style = :original)
  Interpolations.interpolate(options[:path], self, model, style)
end

#processObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/sequel_paperclip/attachment.rb', line 85

def process
  return unless @queued_file
  src_path = @queued_file.path
  options[:processors].each do |processor|
    processor.pre_runs(model, src_path)
    options[:styles].each_pair do |style, style_options|
      tmp_file = Tempfile.new("paperclip")
      Sequel::Plugins::Paperclip.logger.debug("processing #{name} for style #{style} with processor #{processor.name}")
      processor.run(style, style_options, tmp_file)
      @storage_updates << {
        :type => STORAGE_UPDATE_SAVE,
        :src_file => tmp_file,
        :dst_path => path(style),
      }
    end
    processor.post_runs
  end
  @queued_file = nil
end

#update(file) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/sequel_paperclip/attachment.rb', line 43

def update(file)
  if file
    if file.respond_to?(:tempfile)
      file = file.tempfile
    end

    unless file.is_a?(File) || file.is_a?(Tempfile)
      raise ArgumentError, "#{name}: #{file} is not a File"
    end
  else
    if exists?
      options[:styles].each_pair do |style, style_options|
        @storage_updates << {
          :type => STORAGE_UPDATE_DELETE,
          :path => path(style),
        }
      end
    end
  end

  @queued_file = file
end

#update_storageObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/sequel_paperclip/attachment.rb', line 105

def update_storage
  @storage_updates.each do |update|
    case update[:type]
      when STORAGE_UPDATE_SAVE
        Sequel::Plugins::Paperclip.logger.debug("saving #{update[:dst_path]} (#{update[:src_file].size} bytes)")
        FileUtils.mkdir_p(File.dirname(update[:dst_path]))
        FileUtils.cp(update[:src_file].path, update[:dst_path])
        FileUtils.chmod(0664, update[:dst_path])
        update[:src_file].close!
      when STORAGE_UPDATE_DELETE
        Sequel::Plugins::Paperclip.logger.debug("deleting #{update[:path]}")
        begin
          FileUtils.rm(update[:path])
        rescue Errno::ENOENT => error
        end
      else
        raise ArgumentError, "invalid type '#{update[:type]}'"
    end
  end
  @storage_updates = []
end

#url(style = :original) ⇒ Object



81
82
83
# File 'lib/sequel_paperclip/attachment.rb', line 81

def url(style = :original)
  Interpolations.interpolate(options[:url], self, model, style)
end