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.
- #process_attachment ⇒ Object
- #sanitize_file_path(file_path) ⇒ Object
-
#set_attachment_file_path ⇒ Object
Override this method if you would like to override the way file_path is set.
-
#set_attachment_section ⇒ Object
Override this method if you would like to override the way the section is set.
- #update_attachment_if_changed ⇒ Object
Instance Method Details
#after_as_of_version ⇒ Object
160 161 162 163 164 |
# File 'lib/cms/behaviors/attaching.rb', line 160 def after_as_of_version if && self. = Attachment.find().as_of_version() end end |
#after_publish ⇒ Object
151 152 153 |
# File 'lib/cms/behaviors/attaching.rb', line 151 def after_publish .publish if end |
#attachment_file ⇒ Object
36 37 38 |
# File 'lib/cms/behaviors/attaching.rb', line 36 def @attachment_file ||= ? .temp_file : nil end |
#attachment_file=(file) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/cms/behaviors/attaching.rb', line 40 def (file) if @attachment_file != file dirty! @attachment_file = file end end |
#attachment_file_name ⇒ Object
47 48 49 |
# File 'lib/cms/behaviors/attaching.rb', line 47 def @attachment_file_name ||= ? .file_name : nil end |
#attachment_file_path ⇒ Object
51 52 53 |
# File 'lib/cms/behaviors/attaching.rb', line 51 def @attachment_file_path ||= ? .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 (file_path) fp = sanitize_file_path(file_path) if @attachment_file_path != fp dirty! @attachment_file_path = fp end end |
#attachment_link ⇒ Object
166 167 168 169 170 171 172 |
# File 'lib/cms/behaviors/attaching.rb', line 166 def if (published? && live_version?) ? : "/cms/attachments/#{}?version=#{}" else nil end end |
#attachment_section ⇒ Object
74 75 76 |
# File 'lib/cms/behaviors/attaching.rb', line 74 def @attachment_section ||= ? .section : nil end |
#attachment_section=(section) ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/cms/behaviors/attaching.rb', line 78 def (section) if @attachment_section != section dirty! @attachment_section_id = section ? section.id : nil @attachment_section = section end end |
#attachment_section_id ⇒ Object
63 64 65 |
# File 'lib/cms/behaviors/attaching.rb', line 63 def @attachment_section_id ||= ? .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 (section_id) if @attachment_section_id != section_id dirty! @attachment_section_id = section_id end end |
#clear_attachment_ivars ⇒ Object
112 113 114 115 116 117 |
# File 'lib/cms/behaviors/attaching.rb', line 112 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
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_size ⇒ Object
Size in kilobytes
156 157 158 |
# File 'lib/cms/behaviors/attaching.rb', line 156 def file_size ? "%0.2f" % (.file_size / 1024.0) : "?" end |
#process_attachment ⇒ Object
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 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 if .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_path ⇒ Object
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 if !.blank? .file_path = "/attachments/#{File.basename(.original_filename).to_s.downcase}" end end |
#set_attachment_section ⇒ Object
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 if !.blank? .section = Section.root.first end end |
#update_attachment_if_changed ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/cms/behaviors/attaching.rb', line 139 def 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 .save end self. = .draft.version end end |