Class: Attachment
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Attachment
- Defined in:
- app/models/attachment.rb
Instance Attribute Summary collapse
-
#temp_file ⇒ Object
Returns the value of attribute temp_file.
Class Method Summary collapse
- .find_live_by_file_path(file_path) ⇒ Object
-
.storage_location ⇒ Object
—– Class Methods ———————————————————.
- .storage_location=(storage_location) ⇒ Object
Instance Method Summary collapse
- #clear_ivars ⇒ Object
-
#dirty! ⇒ Object
Forces this record to be changed, even if nothing has changed This is necessary if just the section.id has changed, for example.
- #extract_file_extension_from_file_name ⇒ Object
- #extract_file_size_from_temp_file ⇒ Object
- #extract_file_type_from_temp_file ⇒ Object
-
#file_name ⇒ Object
—– Instance Methods ——————————————————.
- #full_file_location ⇒ Object
- #icon ⇒ Object
- #live? ⇒ Boolean
- #live_version ⇒ Object
- #name ⇒ Object
-
#prepend_file_path_with_slash ⇒ Object
—– Callbacks Methods —————————————————–.
- #process_section ⇒ Object
- #public? ⇒ Boolean
- #section ⇒ Object
- #section=(section) ⇒ Object
-
#section_id ⇒ Object
—– Virtual Attributes —————————————————-.
- #section_id=(section_id) ⇒ Object
-
#set_file_location ⇒ Object
The file will be stored on disk at Attachment.storage_location/year/month/day/sha1 The sha1 is a 40 character hash based on the original_filename of the file uploaded and the current time.
- #write_temp_file_to_storage_location ⇒ Object
Instance Attribute Details
#temp_file ⇒ Object
Returns the value of attribute temp_file.
11 12 13 |
# File 'app/models/attachment.rb', line 11 def temp_file @temp_file end |
Class Method Details
.find_live_by_file_path(file_path) ⇒ Object
135 136 137 138 139 140 141 142 143 |
# File 'app/models/attachment.rb', line 135 def self.find_live_by_file_path(file_path) if = Attachment::Version.first(:conditions => { :file_path => file_path, :published => true}, :order => "version desc") = ..as_of_version(.version) (.live_version? && !.archived?) ? : nil end end |
.storage_location ⇒ Object
—– Class Methods ———————————————————
127 128 129 |
# File 'app/models/attachment.rb', line 127 def self.storage_location @storage_location ||= File.join(Rails.root, "/tmp/uploads") end |
.storage_location=(storage_location) ⇒ Object
131 132 133 |
# File 'app/models/attachment.rb', line 131 def self.storage_location=(storage_location) @storage_location = storage_location end |
Instance Method Details
#clear_ivars ⇒ Object
119 120 121 122 123 |
# File 'app/models/attachment.rb', line 119 def clear_ivars @temp_file = nil @section = nil @section_id = nil end |
#dirty! ⇒ Object
Forces this record to be changed, even if nothing has changed This is necessary if just the section.id has changed, for example
198 199 200 201 |
# File 'app/models/attachment.rb', line 198 def dirty! # Seems like a hack, is there a better way? self.updated_at = Time.now end |
#extract_file_extension_from_file_name ⇒ Object
70 71 72 73 74 |
# File 'app/models/attachment.rb', line 70 def extract_file_extension_from_file_name if file_name && file_name['.'] self.file_extension = file_name.split('.').last.to_s.downcase end end |
#extract_file_size_from_temp_file ⇒ Object
82 83 84 85 86 |
# File 'app/models/attachment.rb', line 82 def extract_file_size_from_temp_file unless temp_file.blank? self.file_size = temp_file.size end end |
#extract_file_type_from_temp_file ⇒ Object
76 77 78 79 80 |
# File 'app/models/attachment.rb', line 76 def extract_file_type_from_temp_file unless temp_file.blank? self.file_type = temp_file.content_type end end |
#file_name ⇒ Object
—– Instance Methods ——————————————————
147 148 149 |
# File 'app/models/attachment.rb', line 147 def file_name file_path ? file_path.split('/').last : nil end |
#full_file_location ⇒ Object
192 193 194 |
# File 'app/models/attachment.rb', line 192 def full_file_location File.join(Attachment.storage_location, file_location) end |
#icon ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'app/models/attachment.rb', line 170 def icon { :doc => %w[doc], :gif => %w[gif jpg jpeg png tiff bmp], :htm => %w[htm html], :pdf => %w[pdf], :ppt => %w[ppt], :swf => %w[swf], :txt => %w[txt], :xls => %w[xls], :xml => %w[xml], :zip => %w[zip rar tar gz tgz] }.each do |icon, extensions| return icon if extensions.include?(file_extension.to_s) end :file end |
#live? ⇒ Boolean
155 156 157 |
# File 'app/models/attachment.rb', line 155 def live? versionable? ? versions.count(:conditions => ['version > ? AND published = ?', version, true]) == 0 && published? : true end |
#live_version ⇒ Object
159 160 161 162 163 164 165 166 167 168 |
# File 'app/models/attachment.rb', line 159 def live_version if archived? || deleted? nil elsif published? self else live_version = versions.first(:conditions => {:published => true}, :order => "version desc, id desc") live_version ? as_of_version(live_version.version) : nil end end |
#name ⇒ Object
151 152 153 |
# File 'app/models/attachment.rb', line 151 def name file_name end |
#prepend_file_path_with_slash ⇒ Object
—– Callbacks Methods —————————————————–
64 65 66 67 68 |
# File 'app/models/attachment.rb', line 64 def prepend_file_path_with_slash unless file_path.blank? self.file_path = "/#{file_path}" unless file_path =~ /^\// end end |
#process_section ⇒ Object
99 100 101 102 103 104 105 106 |
# File 'app/models/attachment.rb', line 99 def process_section #logger.info "processing section, section_id => #{section_id}, section_node => #{section_node.inspect}" if section_node && !section_node.new_record? && section_node.section_id != section_id section_node.move_to_end(Section.find(section_id)) else build_section_node(:node => self, :section_id => section_id) end end |
#public? ⇒ Boolean
188 189 190 |
# File 'app/models/attachment.rb', line 188 def public? section ? section.public? : false end |
#section ⇒ Object
50 51 52 |
# File 'app/models/attachment.rb', line 50 def section @section ||= section_node ? section_node.section : nil end |
#section=(section) ⇒ Object
54 55 56 57 58 59 60 |
# File 'app/models/attachment.rb', line 54 def section=(section) if @section != section dirty! @section_id = section ? section.id : nil @section = section end end |
#section_id ⇒ Object
—– Virtual Attributes —————————————————-
39 40 41 |
# File 'app/models/attachment.rb', line 39 def section_id @section_id ||= section_node ? section_node.section_id : nil end |
#section_id=(section_id) ⇒ Object
43 44 45 46 47 48 |
# File 'app/models/attachment.rb', line 43 def section_id=(section_id) if @section_id != section_id dirty! @section_id = section_id end end |
#set_file_location ⇒ Object
The file will be stored on disk at Attachment.storage_location/year/month/day/sha1 The sha1 is a 40 character hash based on the original_filename of the file uploaded and the current time
92 93 94 95 96 97 |
# File 'app/models/attachment.rb', line 92 def set_file_location unless temp_file.blank? sha1 = Digest::SHA1.hexdigest("#{temp_file.original_filename}#{Time.now.to_f}") self.file_location = "#{Time.now.strftime("%Y/%m/%d")}/#{sha1}" end end |
#write_temp_file_to_storage_location ⇒ Object
108 109 110 111 112 113 114 115 116 117 |
# File 'app/models/attachment.rb', line 108 def write_temp_file_to_storage_location unless temp_file.blank? FileUtils.mkdir_p File.dirname(full_file_location) if temp_file.local_path File.copy temp_file.local_path, full_file_location else open(full_file_location, 'w') {|f| f << temp_file.read } end end end |