Module: Grip::HasAttachment::InstanceMethods
- Defined in:
- lib/grip/has_attachment.rb
Instance Method Summary collapse
- #create_variant(attachment, variant, dimensions) ⇒ Object
- #save_attachments ⇒ Object
- #update_attachment_attributes!(new_attachment, new_file, opts) ⇒ Object
- #uploaded_files ⇒ Object
Instance Method Details
#create_variant(attachment, variant, dimensions) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/grip/has_attachment.rb', line 63 def create_variant , variant, dimensions tmp_file = uploaded_files[.name.to_sym][:file] begin tmp = Tempfile.new("#{.name}_#{variant}") image = Miso::Image.new(tmp_file.path) image.crop(dimensions[:width], dimensions[:height]) if dimensions[:crop] image.fit(dimensions[:width], dimensions[:height]) unless dimensions[:crop] image.write(tmp.path) rescue RuntimeError => e warn "Image was not resized. #{e}" tmp = tmp_file end file_hash = {:resized_file => tmp,:uploaded_file => tmp_file} .send("#{variant}=", file_hash) end |
#save_attachments ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/grip/has_attachment.rb', line 55 def .each do || .variants.each do |variant,dimensions| create_variant(,variant,dimensions) end end end |
#update_attachment_attributes!(new_attachment, new_file, opts) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/grip/has_attachment.rb', line 45 def , new_file, opts .owner_type = self.class.to_s .file_name = File.basename(new_file.path) .file_size = File.size(new_file.path) .content_type = MIME::Types.type_for(new_file.path) .file = new_file .variants = opts[:variants] || {} .save! end |
#uploaded_files ⇒ Object
42 43 44 |
# File 'lib/grip/has_attachment.rb', line 42 def uploaded_files @uploaded_files ||= {} end |