Module: AttachmentMagic::ClassMethods
- Defined in:
- lib/attachment_magic.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#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.
-
#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
120 121 122 123 124 125 126 |
# File 'lib/attachment_magic.rb', line 120 def self.extended(base) base.class_attribute :attachment_options base.before_validation :set_size_from_temp_path base.after_save :after_process_attachment base.after_destroy :destroy_file base.after_validation :process_attachment end |
Instance Method Details
#copy_to_temp_file(file, temp_base_name) ⇒ Object
Copies the given file path to a new tempfile, returning the closed tempfile.
129 130 131 132 133 134 |
# File 'lib/attachment_magic.rb', line 129 def copy_to_temp_file(file, temp_base_name) Tempfile.new(temp_base_name, AttachmentMagic.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.
116 117 118 |
# File 'lib/attachment_magic.rb', line 116 def image?(content_type) content_types.include?(content_type) end |
#validates_as_attachment ⇒ Object
Performs common validations for attachment models.
110 111 112 113 |
# File 'lib/attachment_magic.rb', line 110 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.
137 138 139 140 141 142 143 |
# File 'lib/attachment_magic.rb', line 137 def write_to_temp_file(data, temp_base_name) Tempfile.new(temp_base_name, AttachmentMagic.tempfile_path).tap do |tmp| tmp.binmode tmp.write data tmp.close end end |