Module: Technoweenie::AttachmentFu::ClassMethods
- Defined in:
- lib/technoweenie/attachment_fu.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#after_attachment_saved(&block) ⇒ Object
Callback after an attachment has been saved either to the file system or the DB.
-
#after_resize(&block) ⇒ Object
Callback after an image has been resized.
-
#before_thumbnail_saved(&block) ⇒ Object
Callback before a thumbnail is saved.
-
#copy_to_temp_file(file, temp_base_name) ⇒ Object
Copies the given file path to a new tempfile, returning the closed tempfile.
-
#image?(content_type) ⇒ Boolean
Returns true or false if the given content type is recognized as an image.
- #polymorphic_relation_type_column ⇒ Object
-
#thumbnail_class ⇒ Object
Get the thumbnail class, which is the current attachment class by default.
-
#validates_as_attachment ⇒ Object
Performs common validations for attachment models.
-
#write_to_temp_file(data, temp_base_name) ⇒ Object
Writes the given data to a new tempfile, returning the closed tempfile.
Class Method Details
.extended(base) ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/technoweenie/attachment_fu.rb', line 193 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 :after_resize, :after_attachment_saved, :before_thumbnail_saved #end end |
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
227 228 229 |
# File 'lib/technoweenie/attachment_fu.rb', line 227 def (&block) write_inheritable_array(:after_attachment_saved, [block]) end |
#after_resize(&block) ⇒ Object
Callback after an image has been resized.
class Foo < ActiveRecord::Base
after_resize do |record, img|
record.aspect_ratio = img.columns.to_f / img.rows.to_f
end
end
214 215 216 |
# File 'lib/technoweenie/attachment_fu.rb', line 214 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
240 241 242 |
# File 'lib/technoweenie/attachment_fu.rb', line 240 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.
253 254 255 256 257 258 |
# File 'lib/technoweenie/attachment_fu.rb', line 253 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.
189 190 191 |
# File 'lib/technoweenie/attachment_fu.rb', line 189 def image?(content_type) content_types.include?(content_type) end |
#polymorphic_relation_type_column ⇒ Object
269 270 271 272 273 274 |
# File 'lib/technoweenie/attachment_fu.rb', line 269 def polymorphic_relation_type_column return @@_polymorphic_relation_type_column if defined?(@@_polymorphic_relation_type_column) # Checked against ActiveRecord 1.15.6 through Edge @ 2009-08-05. ref = reflections.values.detect { |r| r.macro == :belongs_to && r.[:polymorphic] } @@_polymorphic_relation_type_column = ref && ref.[:foreign_type] end |
#thumbnail_class ⇒ Object
Get the thumbnail class, which is the current attachment class by default. Configure this with the :thumbnail_class option.
247 248 249 250 |
# File 'lib/technoweenie/attachment_fu.rb', line 247 def thumbnail_class [:thumbnail_class] = [:thumbnail_class].constantize unless [:thumbnail_class].is_a?(Class) [:thumbnail_class] end |
#validates_as_attachment ⇒ Object
Performs common validations for attachment models.
183 184 185 186 |
# File 'lib/technoweenie/attachment_fu.rb', line 183 def 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.
261 262 263 264 265 266 267 |
# File 'lib/technoweenie/attachment_fu.rb', line 261 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 |