Module: Cms::Behaviors::Attaching::InstanceMethods

Defined in:
lib/cms/behaviors/attaching.rb

Instance Method Summary collapse

Instance Method Details

#after_as_of_versionObject



160
161
162
163
164
# File 'lib/cms/behaviors/attaching.rb', line 160

def after_as_of_version
  if attachment_id && attachment_version
    self.attachment = Attachment.find(attachment_id).as_of_version(attachment_version)
  end
end

#after_publishObject



151
152
153
# File 'lib/cms/behaviors/attaching.rb', line 151

def after_publish
  attachment.publish if attachment
end

#attachment_fileObject



36
37
38
# File 'lib/cms/behaviors/attaching.rb', line 36

def attachment_file
  @attachment_file ||= attachment ? attachment.temp_file : nil
end

#attachment_file=(file) ⇒ Object



40
41
42
43
44
45
# File 'lib/cms/behaviors/attaching.rb', line 40

def attachment_file=(file)
  if @attachment_file != file
    dirty!
    @attachment_file = file
  end
end

#attachment_file_nameObject



47
48
49
# File 'lib/cms/behaviors/attaching.rb', line 47

def attachment_file_name
  @attachment_file_name ||= attachment ? attachment.file_name : nil
end

#attachment_file_pathObject



51
52
53
# File 'lib/cms/behaviors/attaching.rb', line 51

def attachment_file_path
  @attachment_file_path ||= attachment ? attachment.file_path : nil
end

#attachment_file_path=(file_path) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/cms/behaviors/attaching.rb', line 55

def attachment_file_path=(file_path)
  fp = sanitize_file_path(file_path)
  if @attachment_file_path != fp
    dirty!
    @attachment_file_path = fp
  end
end


166
167
168
169
170
171
172
# File 'lib/cms/behaviors/attaching.rb', line 166

def attachment_link
  if attachment
    (published? && live_version?) ? attachment_file_path : "/cms/attachments/#{attachment_id}?version=#{attachment_version}"
  else
    nil
  end  
end

#attachment_sectionObject



74
75
76
# File 'lib/cms/behaviors/attaching.rb', line 74

def attachment_section
  @attachment_section ||= attachment ? attachment.section : nil
end

#attachment_section=(section) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/cms/behaviors/attaching.rb', line 78

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

#attachment_section_idObject



63
64
65
# File 'lib/cms/behaviors/attaching.rb', line 63

def attachment_section_id
  @attachment_section_id ||= attachment ? attachment.section_id : nil
end

#attachment_section_id=(section_id) ⇒ Object



67
68
69
70
71
72
# File 'lib/cms/behaviors/attaching.rb', line 67

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

#clear_attachment_ivarsObject



112
113
114
115
116
117
# File 'lib/cms/behaviors/attaching.rb', line 112

def clear_attachment_ivars
  @attachment_file = nil
  @attachment_file_path = nil
  @attachment_section_id = nil
  @attachment_section = 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



176
177
178
179
# File 'lib/cms/behaviors/attaching.rb', line 176

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

#file_sizeObject

Size in kilobytes



156
157
158
# File 'lib/cms/behaviors/attaching.rb', line 156

def file_size
  attachment ? "%0.2f" % (attachment.file_size / 1024.0) : "?"
end

#process_attachmentObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/cms/behaviors/attaching.rb', line 86

def process_attachment
  if attachment.nil? && attachment_file.blank?
    unless attachment_file_path.blank?
      errors.add(:attachment_file, "You must upload a file")
      return false
    end
    unless attachment_section_id.blank?
      errors.add(:attachment_file, "You must upload a file")
      return false
    end              
  else
    build_attachment if attachment.nil?  
    attachment.temp_file = attachment_file 
    set_attachment_file_path
    if attachment.file_path.blank?
      errors.add(:attachment_file_path, "File Name is required for attachment")
      return false
    end
    set_attachment_section
    if attachment.section_id.blank?
      errors.add(:attachment_file, "Section is required for attachment")
      return false
    end
  end
end

#sanitize_file_path(file_path) ⇒ Object



133
134
135
136
137
# File 'lib/cms/behaviors/attaching.rb', line 133

def sanitize_file_path(file_path)
  SANITIZATION_REGEXES.inject(file_path.to_s) do |s, (regex, replace)|
    s.gsub(regex, replace)
  end
end

#set_attachment_file_pathObject

Override this method if you would like to override the way file_path is set



127
128
129
130
131
# File 'lib/cms/behaviors/attaching.rb', line 127

def set_attachment_file_path
  if !attachment_file.blank?
    attachment.file_path = "/attachments/#{File.basename(attachment_file.original_filename).to_s.downcase}"
  end
end

#set_attachment_sectionObject

Override this method if you would like to override the way the section is set



120
121
122
123
124
# File 'lib/cms/behaviors/attaching.rb', line 120

def set_attachment_section
  if !attachment_file.blank?
    attachment.section = Section.root.first
  end
end

#update_attachment_if_changedObject



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/cms/behaviors/attaching.rb', line 139

def update_attachment_if_changed
  if attachment
    attachment.archived = archived if self.class.archivable?
    if respond_to?(:revert_to_version) && revert_to_version
      attachment.revert_to(revert_to_version.attachment_version)
    elsif new_record? || attachment.changed? || attachment.temp_file
      attachment.save
    end
    self.attachment_version = attachment.draft.version
  end
end