Class: Attachment

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/attachment.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#temp_fileObject

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



146
147
148
# File 'app/models/attachment.rb', line 146

def self.find_live_by_file_path(file_path)
  Attachment.published.not_archived.first(:conditions => {:file_path => file_path})
end

.storage_locationObject

—– Class Methods ———————————————————



138
139
140
# File 'app/models/attachment.rb', line 138

def self.storage_location
  @storage_location ||= File.join(Rails.root, "/uploads")
end

.storage_location=(storage_location) ⇒ Object



142
143
144
# File 'app/models/attachment.rb', line 142

def self.storage_location=(storage_location)
  @storage_location = storage_location
end

Instance Method Details

#clear_ivarsObject



130
131
132
133
134
# File 'app/models/attachment.rb', line 130

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



188
189
190
191
# File 'app/models/attachment.rb', line 188

def dirty!
  # Seems like a hack, is there a better way?
  self.updated_at = Time.now
end

#extract_file_extension_from_file_nameObject



77
78
79
80
81
# File 'app/models/attachment.rb', line 77

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_fileObject



89
90
91
92
93
# File 'app/models/attachment.rb', line 89

def extract_file_size_from_temp_file  
  unless temp_file.blank?
    self.file_size = temp_file.size
  end    
end

#extract_file_type_from_temp_fileObject



83
84
85
86
87
# File 'app/models/attachment.rb', line 83

def extract_file_type_from_temp_file
  unless temp_file.blank?
    self.file_type = temp_file.content_type
  end    
end

#file_nameObject

—– Instance Methods ——————————————————



152
153
154
# File 'app/models/attachment.rb', line 152

def file_name
  file_path ? file_path.split('/').last : nil
end

#full_file_locationObject



182
183
184
# File 'app/models/attachment.rb', line 182

def full_file_location
  File.join(Attachment.storage_location, file_location)
end

#iconObject



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'app/models/attachment.rb', line 160

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_fileObject

—– Callbacks Methods —————————————————–



67
68
69
# File 'app/models/attachment.rb', line 67

def make_dirty_if_temp_file
  dirty! if temp_file
end

#nameObject



156
157
158
# File 'app/models/attachment.rb', line 156

def name
  file_name
end

#prepend_file_path_with_slashObject



71
72
73
74
75
# File 'app/models/attachment.rb', line 71

def prepend_file_path_with_slash    
  unless file_path.blank?
    self.file_path = "/#{file_path}" unless file_path =~ /^\//
  end
end

#process_sectionObject



106
107
108
109
110
111
112
113
# File 'app/models/attachment.rb', line 106

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

Returns:

  • (Boolean)


178
179
180
# File 'app/models/attachment.rb', line 178

def public?
  section ? section.public? : false
end

#sectionObject



53
54
55
# File 'app/models/attachment.rb', line 53

def section
  @section ||= section_node ? section_node.section : nil
end

#section=(section) ⇒ Object



57
58
59
60
61
62
63
# File 'app/models/attachment.rb', line 57

def section=(section)
  if @section != section
    dirty!
    @section_id = section ? section.id : nil
    @section = section
  end
end

#section_idObject

—– Virtual Attributes —————————————————-



42
43
44
# File 'app/models/attachment.rb', line 42

def section_id
  @section_id ||= section_node ? section_node.section_id : nil
end

#section_id=(section_id) ⇒ Object



46
47
48
49
50
51
# File 'app/models/attachment.rb', line 46

def section_id=(section_id)
  if @section_id != section_id 
    dirty!
    @section_id = section_id
  end
end

#set_file_locationObject

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



99
100
101
102
103
104
# File 'app/models/attachment.rb', line 99

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_locationObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/models/attachment.rb', line 115

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.attachment_file_permission
      FileUtils.chmod Cms.attachment_file_permission, full_file_location
    end
  end
end