Class: Paperclip::Attachment

Inherits:
Object
  • Object
show all
Defined in:
lib/paperclip-meta.rb

Instance Method Summary collapse

Instance Method Details

#image_size(style = default_style) ⇒ Object



52
53
54
# File 'lib/paperclip-meta.rb', line 52

def image_size(style = default_style)
  "#{width(style)}x#{height(style)}"
end

#original_post_process_stylesObject



3
# File 'lib/paperclip-meta.rb', line 3

alias :original_post_process_styles :post_process_styles

#original_saveObject



4
# File 'lib/paperclip-meta.rb', line 4

alias :original_save :save

#post_process_stylesObject

If model has #name_meta column we getting sizes of processed thumbnails and saving it to #name_meta column.



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
# File 'lib/paperclip-meta.rb', line 16

def post_process_styles
  original_post_process_styles

  if instance.respond_to?(:"#{name}_meta=")
    meta = {}

    @queued_for_write.each do |style, file|
      if File.extname(file) == 'swf'
        begin
          header = SwfFile::FlashFile.header file
          meta[style] = {:width => header.width.to_i, :height => header.height.to_i, :size => File.size(file) }
        rescue SwfFileHeaderError => e
          meta[style] = {}
        end
      else
        begin
          geo = Geometry.from_file file
          meta[style] = {:width => geo.width.to_i, :height => geo.height.to_i, :size => File.size(file) }
        rescue NotIdentifiedByImageMagickError => e
          meta[style] = {}
        end
      end
    end

    instance_write(:meta, ActiveSupport::Base64.encode64(Marshal.dump(meta)))
  end
end

#saveObject

If attachment deleted - destroy meta data



7
8
9
10
11
12
# File 'lib/paperclip-meta.rb', line 7

def save
  if (not @queued_for_delete.empty?) and @queued_for_write.empty?
    instance_write(:meta, ActiveSupport::Base64.encode64(Marshal.dump({}))) if instance.respond_to?(:"#{name}_meta=")
  end
  original_save
end