Module: Technoweenie::AttachmentFu::ClassMethods

Defined in:
lib/technoweenie/attachment_fu.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/technoweenie/attachment_fu.rb', line 185

def self.extended(base)
  base.class_attribute :attachment_options
  base.before_destroy :destroy_thumbnails
  base.before_validation :set_size_from_temp_path
  base.after_save :after_process_attachment
  base.after_destroy :destroy_file
  base.after_validation :process_attachment
  if defined?(::ActiveSupport::Callbacks)
    base.define_callbacks :resize, :kind => :after
    base.define_callbacks :thumbnail_saved, :kind => :before
  end
end

Instance Method Details

#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


207
208
209
# File 'lib/technoweenie/attachment_fu.rb', line 207

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 |thumbnail|
    record = thumbnail.parent
    ...
  end
end


220
221
222
# File 'lib/technoweenie/attachment_fu.rb', line 220

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

#copy_to_temp_file(file, temp_base_name) ⇒ Object

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



233
234
235
236
237
238
# File 'lib/technoweenie/attachment_fu.rb', line 233

def copy_to_temp_file(file, temp_base_name)
  Tempfile.new(temp_base_name, Technoweenie::AttachmentFu.tempfile_path).tap 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)


181
182
183
# File 'lib/technoweenie/attachment_fu.rb', line 181

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.



227
228
229
230
# File 'lib/technoweenie/attachment_fu.rb', line 227

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.



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

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.



241
242
243
244
245
246
247
# File 'lib/technoweenie/attachment_fu.rb', line 241

def write_to_temp_file(data, temp_base_name)
  Tempfile.new(temp_base_name, Technoweenie::AttachmentFu.tempfile_path).tap do |tmp|
    tmp.binmode
    tmp.write data
    tmp.close
  end
end