Module: SimpleAttachments::AttachmentModel::InstanceMethods
- Defined in:
- lib/simple_attachments/attachment_model.rb
Instance Method Summary collapse
-
#attached? ⇒ Boolean
Test if attached to any container.
-
#destroy_file ⇒ Object
Delete file from the filesystem.
-
#file=(file) ⇒ Object
Set file for attachment.
-
#file_path ⇒ Object
Generate path to file.
-
#full_file_path ⇒ Object
Generate full path to file.
-
#path ⇒ Object
Get url to the attachment.
-
#save_file ⇒ Object
Save file in the filesystem.
-
#serializable_hash ⇒ Object
:nodoc:.
-
#uploading_error ⇒ Object
Generate error connected to any server problems.
Instance Method Details
#attached? ⇒ Boolean
Test if attached to any container.
108 109 110 |
# File 'lib/simple_attachments/attachment_model.rb', line 108 def attached? not send(self.class.container_name).nil? end |
#destroy_file ⇒ Object
Delete file from the filesystem.
95 96 97 98 |
# File 'lib/simple_attachments/attachment_model.rb', line 95 def destroy_file File.delete full_file_path rescue end |
#file=(file) ⇒ Object
Set file for attachment. file
is UploadedFile
object
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/simple_attachments/attachment_model.rb', line 59 def file=(file) if file.nil? uploading_error else @file = file self.mimetype = @file.content_type self.filesize = @file.tempfile.size self.filename = File.basename(@file.original_filename) # old browsers hack self.filepath = file_path end end |
#file_path ⇒ Object
Generate path to file. Redefine this method if you want another naming logic.
74 75 76 |
# File 'lib/simple_attachments/attachment_model.rb', line 74 def file_path "#{Time.now.to_f}.#{filename}" end |
#full_file_path ⇒ Object
Generate full path to file. Redefine this method if you have another place to hold files.
81 82 83 |
# File 'lib/simple_attachments/attachment_model.rb', line 81 def full_file_path Rails.root.join('uploads', filepath).to_s end |
#path ⇒ Object
Get url to the attachment.
120 121 122 |
# File 'lib/simple_attachments/attachment_model.rb', line 120 def path Rails.application.routes.url_helpers.send(self.class.to_s.underscore.concat('_path'), id) end |
#save_file ⇒ Object
Save file in the filesystem.
87 88 89 90 91 |
# File 'lib/simple_attachments/attachment_model.rb', line 87 def save_file File.open(full_file_path, 'w') { |file| file.write @file.read } unless @file.nil? rescue uploading_error end |
#serializable_hash ⇒ Object
:nodoc:
100 101 102 103 104 |
# File 'lib/simple_attachments/attachment_model.rb', line 100 def serializable_hash # :nodoc: data = super data['filepath'] = path # changing filepath to file url data end |
#uploading_error ⇒ Object
Generate error connected to any server problems.
114 115 116 |
# File 'lib/simple_attachments/attachment_model.rb', line 114 def uploading_error errors[:base] << I18n.t('simple_attachments.uploading_error') end |