Class: Attachment
- Inherits:
-
Object
- Object
- Attachment
- Defined in:
- lib/yodel/models/core/attachments/attachment.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#field ⇒ Object
Returns the value of attribute field.
-
#mime ⇒ Object
Returns the value of attribute mime.
-
#name ⇒ Object
Returns the value of attribute name.
-
#record ⇒ Object
Returns the value of attribute record.
Instance Method Summary collapse
- #directory_path ⇒ Object
- #empty? ⇒ Boolean
- #exist? ⇒ Boolean
-
#initialize(value, record, field) ⇒ Attachment
constructor
A new instance of Attachment.
- #length ⇒ Object
- #path ⇒ Object
- #relative_directory_path ⇒ Object
- #relative_path ⇒ Object
- #remove_files ⇒ Object
- #reset_memoised_values ⇒ Object
- #set_file(file) ⇒ Object
- #to_hash ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(value, record, field) ⇒ Attachment
Returns a new instance of Attachment.
4 5 6 7 8 9 10 |
# File 'lib/yodel/models/core/attachments/attachment.rb', line 4 def initialize(value, record, field) @field = field @record = record value ||= {} @name = value['name'] @mime = value['mime'] end |
Instance Attribute Details
#field ⇒ Object
Returns the value of attribute field.
2 3 4 |
# File 'lib/yodel/models/core/attachments/attachment.rb', line 2 def field @field end |
#mime ⇒ Object
Returns the value of attribute mime.
2 3 4 |
# File 'lib/yodel/models/core/attachments/attachment.rb', line 2 def mime @mime end |
#name ⇒ Object
Returns the value of attribute name.
2 3 4 |
# File 'lib/yodel/models/core/attachments/attachment.rb', line 2 def name @name end |
#record ⇒ Object
Returns the value of attribute record.
2 3 4 |
# File 'lib/yodel/models/core/attachments/attachment.rb', line 2 def record @record end |
Instance Method Details
#directory_path ⇒ Object
38 39 40 |
# File 'lib/yodel/models/core/attachments/attachment.rb', line 38 def directory_path @directory_path ||= File.join(@record.site., relative_directory_path) end |
#empty? ⇒ Boolean
47 48 49 |
# File 'lib/yodel/models/core/attachments/attachment.rb', line 47 def empty? !exist? end |
#exist? ⇒ Boolean
42 43 44 45 |
# File 'lib/yodel/models/core/attachments/attachment.rb', line 42 def exist? return false if @name.nil? File.exist?(path) end |
#length ⇒ Object
79 80 81 82 |
# File 'lib/yodel/models/core/attachments/attachment.rb', line 79 def length return 0 unless self.exist? return File.size(path) end |
#path ⇒ Object
34 35 36 |
# File 'lib/yodel/models/core/attachments/attachment.rb', line 34 def path @path ||= File.join(@record.site., relative_path) end |
#relative_directory_path ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/yodel/models/core/attachments/attachment.rb', line 24 def relative_directory_path @relative_directory_path ||= begin if @record.is_a?(EmbeddedRecord) File.join("#{@record..name}_#{@field.name}", @record.parent_record.id.to_s, @record.id.to_s) else File.join(@field.name, @record.id.to_s) end end end |
#relative_path ⇒ Object
20 21 22 |
# File 'lib/yodel/models/core/attachments/attachment.rb', line 20 def relative_path @relative_path ||= File.join(relative_directory_path, @name) end |
#remove_files ⇒ Object
51 52 53 |
# File 'lib/yodel/models/core/attachments/attachment.rb', line 51 def remove_files FileUtils.rm_r directory_path if exist? end |
#reset_memoised_values ⇒ Object
55 56 57 |
# File 'lib/yodel/models/core/attachments/attachment.rb', line 55 def reset_memoised_values @url = @relative_path = @relative_directory_path = @path = @directory_path = nil end |
#set_file(file) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/yodel/models/core/attachments/attachment.rb', line 59 def set_file(file) # delete the old file and reset memoised paths unless @record.new? remove_files reset_memoised_values end # reset the name and mime type of the attachment @name = file[:filename] @mime = file[:type] temp = file[:tempfile] temp_path = temp.path temp.close # for simplicity we move the uploaded file (from /tmp) rather than copying FileUtils.mkpath directory_path FileUtils.mv(temp_path, path) FileUtils.chmod(0664, path) # (owner: rw, group: rw, other: r) end |
#to_hash ⇒ Object
12 13 14 |
# File 'lib/yodel/models/core/attachments/attachment.rb', line 12 def to_hash {'name' => name, 'mime' => mime} end |
#url ⇒ Object
16 17 18 |
# File 'lib/yodel/models/core/attachments/attachment.rb', line 16 def url @url ||= Pathname.new('/').join(Yodel::ATTACHMENTS_DIRECTORY_NAME, relative_path) end |