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

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

Instance Method Summary collapse

Instance Method Details

#after_as_of_version ⇒ Object



184
185
186
187
188
# File 'lib/cms/behaviors/attaching.rb', line 184

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

#after_publish ⇒ Object



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

def after_publish
  attachment.publish if attachment
end

#attachment_file ⇒ Object



39
40
41
# File 'lib/cms/behaviors/attaching.rb', line 39

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

#attachment_file=(file) ⇒ Object



43
44
45
46
47
48
# File 'lib/cms/behaviors/attaching.rb', line 43

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

#attachment_file_name ⇒ Object



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

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

#attachment_file_path ⇒ Object



54
55
56
# File 'lib/cms/behaviors/attaching.rb', line 54

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

#attachment_file_path=(file_path) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/cms/behaviors/attaching.rb', line 58

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


190
191
192
193
194
195
196
# File 'lib/cms/behaviors/attaching.rb', line 190

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

#attachment_section ⇒ Object



77
78
79
# File 'lib/cms/behaviors/attaching.rb', line 77

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

#attachment_section=(section) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/cms/behaviors/attaching.rb', line 81

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

#attachment_section_id ⇒ Object



66
67
68
# File 'lib/cms/behaviors/attaching.rb', line 66

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

#attachment_section_id=(section_id) ⇒ Object



70
71
72
73
74
75
# File 'lib/cms/behaviors/attaching.rb', line 70

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

#clear_attachment_ivars ⇒ Object



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

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



200
201
202
203
# File 'lib/cms/behaviors/attaching.rb', line 200

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

#file_size ⇒ Object

Size in kilobytes



180
181
182
# File 'lib/cms/behaviors/attaching.rb', line 180

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

#handle_setting_attachment_path ⇒ Object

Define at :set_attachment_path if you would like to override the way file_path is set



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

def handle_setting_attachment_path
  if self.respond_to? :set_attachment_path
    set_attachment_path
  else
    use_default_attachment_path
  end
end

#handle_setting_attachment_section ⇒ Object

Implement a :set_attachment_section method if you would like to override the way the section is set



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

def handle_setting_attachment_section
  if self.respond_to? :set_attachment_section
    set_attachment_section
  else
    use_default_attachment_section
  end
end

#process_attachment ⇒ Object



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

def process_attachment
  Rails.logger.warn "Processing attachment (#{attachment_file})"
  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
    handle_setting_attachment_path
    if attachment.file_path.blank?
      errors.add(:attachment_file_path, "File Name is required for attachment")
      return false
    end
    handle_setting_attachment_section
    unless attachment.section
      errors.add(:attachment_file, "Section is required for attachment")
      return false
    end

  end
end

#sanitize_file_path(file_path) ⇒ Object



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

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

#update_attachment_if_changed ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/cms/behaviors/attaching.rb', line 161

def update_attachment_if_changed
  logger.debug { "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
      saved_attach = attachment.save
      logger.warn "Attachment was saved = #{saved_attach}"
    end
    self.attachment_version = attachment.draft.version
  end
end

#use_default_attachment_path ⇒ Object



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

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

#use_default_attachment_section ⇒ Object

Default behavior for assigning a section, if a block does not define its own.



143
144
145
146
147
# File 'lib/cms/behaviors/attaching.rb', line 143

def use_default_attachment_section
  if !attachment_file.blank?
    attachment.section = Cms::Section.root.first
  end
end