Class Attachment
In: app/models/attachment.rb
Parent: ActiveRecord::Base

Methods

Attributes

temp_file  [RW] 

Public Class methods

[Source]

     # File app/models/attachment.rb, line 146
146:   def self.find_live_by_file_path(file_path)
147:     Attachment.published.not_archived.first(:conditions => {:file_path => file_path})
148:   end

[Source]

     # File app/models/attachment.rb, line 138
138:   def self.storage_location
139:     @storage_location ||= File.join(Rails.root, "/tmp/uploads")
140:   end

[Source]

     # File app/models/attachment.rb, line 142
142:   def self.storage_location=(storage_location)
143:     @storage_location = storage_location
144:   end

Public Instance methods

[Source]

     # File app/models/attachment.rb, line 130
130:   def clear_ivars
131:     @temp_file = nil
132:     @section = nil
133:     @section_id = nil
134:   end

Forces this record to be changed, even if nothing has changed This is necessary if just the section.id has changed, for example

[Source]

     # File app/models/attachment.rb, line 188
188:   def dirty!
189:     # Seems like a hack, is there a better way?
190:     self.updated_at = Time.now
191:   end

[Source]

    # File app/models/attachment.rb, line 77
77:   def extract_file_extension_from_file_name
78:     if file_name && file_name['.']
79:       self.file_extension = file_name.split('.').last.to_s.downcase
80:     end    
81:   end

[Source]

    # File app/models/attachment.rb, line 89
89:   def extract_file_size_from_temp_file  
90:     unless temp_file.blank?
91:       self.file_size = temp_file.size
92:     end    
93:   end

[Source]

    # File app/models/attachment.rb, line 83
83:   def extract_file_type_from_temp_file
84:     unless temp_file.blank?
85:       self.file_type = temp_file.content_type
86:     end    
87:   end

[Source]

     # File app/models/attachment.rb, line 152
152:   def file_name
153:     file_path ? file_path.split('/').last : nil
154:   end

[Source]

     # File app/models/attachment.rb, line 182
182:   def full_file_location
183:     File.join(Attachment.storage_location, file_location)
184:   end

[Source]

     # File app/models/attachment.rb, line 160
160:   def icon
161:     {
162:         :doc => %w[doc],
163:         :gif => %w[gif jpg jpeg png tiff bmp],
164:         :htm => %w[htm html],
165:         :pdf => %w[pdf],
166:         :ppt => %w[ppt],
167:         :swf => %w[swf],
168:         :txt => %w[txt],
169:         :xls => %w[xls],
170:         :xml => %w[xml],
171:         :zip => %w[zip rar tar gz tgz]
172:     }.each do |icon, extensions|
173:       return icon if extensions.include?(file_extension.to_s)
174:     end
175:     :file
176:   end

[Source]

    # File app/models/attachment.rb, line 67
67:   def make_dirty_if_temp_file
68:     dirty! if temp_file
69:   end

[Source]

     # File app/models/attachment.rb, line 156
156:   def name
157:     file_name
158:   end

[Source]

    # File app/models/attachment.rb, line 71
71:   def prepend_file_path_with_slash    
72:     unless file_path.blank?
73:       self.file_path = "/#{file_path}" unless file_path =~ /^\//
74:     end
75:   end

[Source]

     # File app/models/attachment.rb, line 106
106:   def process_section
107:     #logger.info "processing section, section_id => #{section_id}, section_node => #{section_node.inspect}"
108:     if section_node && !section_node.new_record? && section_node.section_id != section_id
109:       section_node.move_to_end(Section.find(section_id))
110:     else
111:       build_section_node(:node => self, :section_id => section_id)
112:     end    
113:   end

[Source]

     # File app/models/attachment.rb, line 178
178:   def public?
179:     section ? section.public? : false
180:   end

[Source]

    # File app/models/attachment.rb, line 53
53:   def section
54:     @section ||= section_node ? section_node.section : nil
55:   end

[Source]

    # File app/models/attachment.rb, line 57
57:   def section=(section)
58:     if @section != section
59:       dirty!
60:       @section_id = section ? section.id : nil
61:       @section = section
62:     end
63:   end

[Source]

    # File app/models/attachment.rb, line 42
42:   def section_id
43:     @section_id ||= section_node ? section_node.section_id : nil
44:   end

[Source]

    # File app/models/attachment.rb, line 46
46:   def section_id=(section_id)
47:     if @section_id != section_id 
48:       dirty!
49:       @section_id = section_id
50:     end
51:   end

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

[Source]

     # File app/models/attachment.rb, line 99
 99:   def set_file_location
100:     unless temp_file.blank?
101:       sha1 = Digest::SHA1.hexdigest("#{temp_file.original_filename}#{Time.now.to_f}")
102:       self.file_location = "#{Time.now.strftime("%Y/%m/%d")}/#{sha1}"
103:     end
104:   end

[Source]

     # File app/models/attachment.rb, line 115
115:   def write_temp_file_to_storage_location
116:     unless temp_file.blank?
117:       FileUtils.mkdir_p File.dirname(full_file_location)
118:       if temp_file.local_path
119:         File.copy temp_file.local_path, full_file_location
120:       else
121:         open(full_file_location, 'w') {|f| f << temp_file.read }
122:       end
123:       
124:       if Cms.attachment_file_permission
125:         FileUtils.chmod Cms.attachment_file_permission, full_file_location
126:       end
127:     end
128:   end

[Validate]