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
- #hack ⇒ Object
- #icon ⇒ Object
-
#make_dirty_if_temp_file ⇒ Object
—– Callbacks Methods —————————————————–.
- #name ⇒ Object
- #prepend_file_path_with_slash ⇒ Object
- #process_section ⇒ Object
- #public? ⇒ Boolean
-
#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.
13 14 15 |
# File 'app/models/attachment.rb', line 13 def temp_file @temp_file end |
Class Method Details
.find_live_by_file_path(file_path) ⇒ Object
130 131 132 |
# File 'app/models/attachment.rb', line 130 def self.find_live_by_file_path(file_path) Attachment.published.not_archived.first(:conditions => {:file_path => file_path}) end |
.storage_location ⇒ Object
—– Class Methods ———————————————————
122 123 124 |
# File 'app/models/attachment.rb', line 122 def self.storage_location @storage_location ||= File.join(Rails.root, "/tmp/uploads") end |
.storage_location=(storage_location) ⇒ Object
126 127 128 |
# File 'app/models/attachment.rb', line 126 def self.storage_location=(storage_location) @storage_location = storage_location end |
Instance Method Details
#clear_ivars ⇒ Object
114 115 116 117 118 |
# File 'app/models/attachment.rb', line 114 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
172 173 174 175 |
# File 'app/models/attachment.rb', line 172 def dirty! # Seems like a hack, is there a better way? self.updated_at = Time.now end |
#extract_file_extension_from_file_name ⇒ Object
58 59 60 61 62 |
# File 'app/models/attachment.rb', line 58 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
70 71 72 73 74 |
# File 'app/models/attachment.rb', line 70 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
64 65 66 67 68 |
# File 'app/models/attachment.rb', line 64 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 ——————————————————
136 137 138 |
# File 'app/models/attachment.rb', line 136 def file_name file_path ? file_path.split('/').last : nil end |
#full_file_location ⇒ Object
166 167 168 |
# File 'app/models/attachment.rb', line 166 def full_file_location File.join(Attachment.storage_location, file_location) end |
#hack ⇒ Object
87 88 89 |
# File 'app/models/attachment.rb', line 87 def hack section_id = -1 end |
#icon ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'app/models/attachment.rb', line 144 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 |
#make_dirty_if_temp_file ⇒ Object
—– Callbacks Methods —————————————————–
48 49 50 |
# File 'app/models/attachment.rb', line 48 def make_dirty_if_temp_file dirty! if temp_file end |
#name ⇒ Object
140 141 142 |
# File 'app/models/attachment.rb', line 140 def name file_name end |
#prepend_file_path_with_slash ⇒ Object
52 53 54 55 56 |
# File 'app/models/attachment.rb', line 52 def prepend_file_path_with_slash unless file_path.blank? self.file_path = "/#{file_path}" unless file_path =~ /^\// end end |
#process_section ⇒ Object
90 91 92 93 94 95 96 97 |
# File 'app/models/attachment.rb', line 90 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
162 163 164 |
# File 'app/models/attachment.rb', line 162 def public? section ? section.public? : false 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
80 81 82 83 84 85 |
# File 'app/models/attachment.rb', line 80 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
99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'app/models/attachment.rb', line 99 def write_temp_file_to_storage_location unless temp_file.blank? FileUtils.mkdir_p File.dirname(full_file_location) if temp_file.local_path FileUtils.copy temp_file.local_path, full_file_location else open(full_file_location, 'w') {|f| f << temp_file.read } end if Cms. FileUtils.chmod Cms., full_file_location end end end |