Sha256: 200e09749f05722631730ca3c3b401bd11637d8e65dc8d9c71ff1437e778a69d
Contents?: true
Size: 1.91 KB
Versions: 1
Compression:
Stored size: 1.91 KB
Contents
class Attachment attr_accessor :field, :record, :name, :mime def initialize(value, record, field) @field = field @record = record value ||= {} @name = value['name'] @mime = value['mime'] end def to_hash {'name' => name, 'mime' => mime} end def url @url ||= Pathname.new('/').join(Yodel::ATTACHMENTS_DIRECTORY_NAME, relative_path) end def relative_path @relative_path ||= File.join(relative_directory_path, @name) end def relative_directory_path @relative_directory_path ||= begin if @record.is_a?(EmbeddedRecord) File.join("#{@record.embedded_field.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 def path @path ||= File.join(@record.site.attachments_directory, relative_path) end def directory_path @directory_path ||= File.join(@record.site.attachments_directory, relative_directory_path) end def exist? return false if @name.nil? File.exist?(path) end def empty? !exist? end def remove_files FileUtils.rm_r directory_path if exist? end def reset_memoised_values @url = @relative_path = @relative_directory_path = @path = @directory_path = nil end 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 def length return 0 unless self.exist? return File.size(path) end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
yodel-0.0.7 | lib/yodel/models/core/attachments/attachment.rb |