Module: Cms::Behaviors::Attaching::InstanceMethods
- Defined in:
- lib/cms/behaviors/attaching.rb
Instance Method Summary collapse
- #after_as_of_version ⇒ Object
- #after_publish ⇒ Object
- #attachment_file ⇒ Object
- #attachment_file=(file) ⇒ Object
- #attachment_file_name ⇒ Object
- #attachment_file_path ⇒ Object
- #attachment_file_path=(file_path) ⇒ Object
- #attachment_link ⇒ Object
- #attachment_section ⇒ Object
- #attachment_section=(section) ⇒ Object
- #attachment_section_id ⇒ Object
- #attachment_section_id=(section_id) ⇒ Object
- #clear_attachment_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.
-
#file_size ⇒ Object
Size in kilobytes.
-
#handle_setting_attachment_path ⇒ Object
Define at :set_attachment_path if you would like to override the way file_path is set.
-
#handle_setting_attachment_section ⇒ Object
Implement a :set_attachment_section method if you would like to override the way the section is set.
- #process_attachment ⇒ Object
- #sanitize_file_path(file_path) ⇒ Object
- #update_attachment_if_changed ⇒ Object
- #use_default_attachment_path ⇒ Object
-
#use_default_attachment_section ⇒ Object
Default behavior for assigning a section, if a block does not define its own.
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 && self. = Cms::Attachment.find().as_of_version() end end |
#after_publish ⇒ Object
175 176 177 |
# File 'lib/cms/behaviors/attaching.rb', line 175 def after_publish .publish if end |
#attachment_file ⇒ Object
39 40 41 |
# File 'lib/cms/behaviors/attaching.rb', line 39 def @attachment_file ||= ? .temp_file : nil end |
#attachment_file=(file) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/cms/behaviors/attaching.rb', line 43 def (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 ||= ? .file_name : nil end |
#attachment_file_path ⇒ Object
54 55 56 |
# File 'lib/cms/behaviors/attaching.rb', line 54 def @attachment_file_path ||= ? .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 (file_path) fp = sanitize_file_path(file_path) if @attachment_file_path != fp dirty! @attachment_file_path = fp end end |
#attachment_link ⇒ Object
190 191 192 193 194 195 196 |
# File 'lib/cms/behaviors/attaching.rb', line 190 def if (published? && live_version?) ? : "/cms/attachments/#{}?version=#{}" else nil end end |
#attachment_section ⇒ Object
77 78 79 |
# File 'lib/cms/behaviors/attaching.rb', line 77 def @attachment_section ||= ? .section : nil end |
#attachment_section=(section) ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/cms/behaviors/attaching.rb', line 81 def (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 ||= ? .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 (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 @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 ? "%0.2f" % (.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 if self.respond_to? :set_attachment_path else 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 if self.respond_to? :set_attachment_section else 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 Rails.logger.warn "Processing attachment (#{})" if .nil? && .blank? unless .blank? errors.add(:attachment_file, "You must upload a file") return false end unless .blank? errors.add(:attachment_file, "You must upload a file") return false end else if .nil? .temp_file = if .file_path.blank? errors.add(:attachment_file_path, "File Name is required for attachment") return false end unless .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 logger.debug { "UPDATE ATTACHMENT if changed" } if .archived = archived if self.class.archivable? if respond_to?(:revert_to_version) && revert_to_version .revert_to(revert_to_version.) elsif new_record? || .changed? || .temp_file saved_attach = .save logger.warn "Attachment was saved = #{saved_attach}" end self. = .draft.version end end |
#use_default_attachment_path ⇒ Object
149 150 151 152 153 |
# File 'lib/cms/behaviors/attaching.rb', line 149 def if !.blank? .file_path = "/attachments/#{File.basename(.original_filename).to_s.downcase}" end end |