Module: AttachmerbFu::ClassMethods

Defined in:
lib/attachmerb_fu.rb

Instance Method Summary collapse

Instance Method Details

#after_attachment_saved(&block) ⇒ Object

Callback after an attachment has been saved either to the file system or the DB. Only called if the file has been changed, not necessarily if the record is updated.

class Foo < ActiveRecord::Base
  acts_as_attachment
  after_attachment_saved do |record|
    ...
  end
end


157
158
159
# File 'lib/attachmerb_fu.rb', line 157

def after_attachment_saved(&block)
  write_inheritable_array(:after_attachment_saved, [block])
end

#after_resize(&block) ⇒ Object

Callback after an image has been resized.

class Foo < ActiveRecord::Base
  acts_as_attachment
  after_resize do |record, img| 
    record.aspect_ratio = img.columns.to_f / img.rows.to_f
  end
end


144
145
146
# File 'lib/attachmerb_fu.rb', line 144

def after_resize(&block)
  write_inheritable_array(:after_resize, [block])
end

#before_thumbnail_saved(&block) ⇒ Object

Callback before a thumbnail is saved. Use this to pass any necessary extra attributes that may be required.

class Foo < ActiveRecord::Base
  acts_as_attachment
  before_thumbnail_saved do |record, thumbnail|
    ...
  end
end


169
170
171
# File 'lib/attachmerb_fu.rb', line 169

def before_thumbnail_saved(&block)
  write_inheritable_array(:before_thumbnail_saved, [block])
end

#content_typesObject



121
122
123
# File 'lib/attachmerb_fu.rb', line 121

def content_types
  AttachmerbFu.content_types
end

#copy_to_temp_file(file, temp_base_name) ⇒ Object

Copies the given file path to a new tempfile, returning the closed tempfile.



181
182
183
184
185
186
# File 'lib/attachmerb_fu.rb', line 181

def copy_to_temp_file(file, temp_base_name)
  returning Tempfile.new(temp_base_name, AttachmerbFu.tempfile_path) do |tmp|
    tmp.close
    FileUtils.cp file, tmp.path
  end
end

#image?(content_type) ⇒ Boolean

Returns true or false if the given content type is recognized as an image.

Returns:

  • (Boolean)


132
133
134
# File 'lib/attachmerb_fu.rb', line 132

def image?(content_type)
  content_types.include?(content_type)
end

#thumbnail_classObject

Get the thumbnail class, which is the current attachment class by default. Configure this with the :thumbnail_class option.



175
176
177
178
# File 'lib/attachmerb_fu.rb', line 175

def thumbnail_class
  attachment_options[:thumbnail_class] = attachment_options[:thumbnail_class].constantize unless attachment_options[:thumbnail_class].is_a?(Class)
  attachment_options[:thumbnail_class]
end

#validates_as_attachmentObject

Performs common validations for attachment models.



126
127
128
129
# File 'lib/attachmerb_fu.rb', line 126

def validates_as_attachment
  validates_presence_of :size, :content_type, :filename
  validate              :attachment_attributes_valid?
end

#write_to_temp_file(data, temp_base_name) ⇒ Object

Writes the given data to a new tempfile, returning the closed tempfile.



189
190
191
192
193
194
195
# File 'lib/attachmerb_fu.rb', line 189

def write_to_temp_file(data, temp_base_name)
  returning Tempfile.new(temp_base_name, AttachmerbFu.tempfile_path) do |tmp|
    tmp.binmode
    tmp.write data
    tmp.close
  end
end