Module | Cms::Behaviors::Attaching::InstanceMethods |
In: |
lib/cms/behaviors/attaching.rb
|
# File lib/cms/behaviors/attaching.rb, line 160 160: def after_as_of_version 161: if attachment_id && attachment_version 162: self.attachment = Attachment.find(attachment_id).as_of_version(attachment_version) 163: end 164: end
# File lib/cms/behaviors/attaching.rb, line 151 151: def after_publish 152: attachment.publish if attachment 153: end
# File lib/cms/behaviors/attaching.rb, line 36 36: def attachment_file 37: @attachment_file ||= attachment ? attachment.temp_file : nil 38: end
# File lib/cms/behaviors/attaching.rb, line 40 40: def attachment_file=(file) 41: if @attachment_file != file 42: dirty! 43: @attachment_file = file 44: end 45: end
# File lib/cms/behaviors/attaching.rb, line 47 47: def attachment_file_name 48: @attachment_file_name ||= attachment ? attachment.file_name : nil 49: end
# File lib/cms/behaviors/attaching.rb, line 51 51: def attachment_file_path 52: @attachment_file_path ||= attachment ? attachment.file_path : nil 53: end
# File lib/cms/behaviors/attaching.rb, line 55 55: def attachment_file_path=(file_path) 56: fp = sanitize_file_path(file_path) 57: if @attachment_file_path != fp 58: dirty! 59: @attachment_file_path = fp 60: end 61: end
# File lib/cms/behaviors/attaching.rb, line 166 166: def attachment_link 167: if attachment 168: (published? && live_version?) ? attachment_file_path : "/cms/attachments/#{attachment_id}?version=#{attachment_version}" 169: else 170: nil 171: end 172: end
# File lib/cms/behaviors/attaching.rb, line 74 74: def attachment_section 75: @attachment_section ||= attachment ? attachment.section : nil 76: end
# File lib/cms/behaviors/attaching.rb, line 78 78: def attachment_section=(section) 79: if @attachment_section != section 80: dirty! 81: @attachment_section_id = section ? section.id : nil 82: @attachment_section = section 83: end 84: end
# File lib/cms/behaviors/attaching.rb, line 63 63: def attachment_section_id 64: @attachment_section_id ||= attachment ? attachment.section_id : nil 65: end
# File lib/cms/behaviors/attaching.rb, line 67 67: def attachment_section_id=(section_id) 68: if @attachment_section_id != section_id 69: dirty! 70: @attachment_section_id = section_id 71: end 72: end
# File lib/cms/behaviors/attaching.rb, line 112 112: def clear_attachment_ivars 113: @attachment_file = nil 114: @attachment_file_path = nil 115: @attachment_section_id = nil 116: @attachment_section = nil 117: end
Forces this record to be changed, even if nothing has changed This is necessary if just the section.id has changed, for example
# File lib/cms/behaviors/attaching.rb, line 176 176: def dirty! 177: # Seems like a hack, is there a better way? 178: self.updated_at = Time.now 179: end
Size in kilobytes
# File lib/cms/behaviors/attaching.rb, line 156 156: def file_size 157: attachment ? "%0.2f" % (attachment.file_size / 1024.0) : "?" 158: end
# File lib/cms/behaviors/attaching.rb, line 86 86: def process_attachment 87: if attachment.nil? && attachment_file.blank? 88: unless attachment_file_path.blank? 89: errors.add(:attachment_file, "You must upload a file") 90: return false 91: end 92: unless attachment_section_id.blank? 93: errors.add(:attachment_file, "You must upload a file") 94: return false 95: end 96: else 97: build_attachment if attachment.nil? 98: attachment.temp_file = attachment_file 99: set_attachment_file_path 100: if attachment.file_path.blank? 101: errors.add(:attachment_file_path, "File Name is required for attachment") 102: return false 103: end 104: set_attachment_section 105: if attachment.section_id.blank? 106: errors.add(:attachment_file, "Section is required for attachment") 107: return false 108: end 109: end 110: end
# File lib/cms/behaviors/attaching.rb, line 133 133: def sanitize_file_path(file_path) 134: SANITIZATION_REGEXES.inject(file_path.to_s) do |s, (regex, replace)| 135: s.gsub(regex, replace) 136: end 137: end
Override this method if you would like to override the way file_path is set
# File lib/cms/behaviors/attaching.rb, line 127 127: def set_attachment_file_path 128: if !attachment_file.blank? 129: attachment.file_path = "/attachments/#{File.basename(attachment_file.original_filename).to_s.downcase}" 130: end 131: end
Override this method if you would like to override the way the section is set
# File lib/cms/behaviors/attaching.rb, line 120 120: def set_attachment_section 121: if !attachment_file.blank? 122: attachment.section = Section.root.first 123: end 124: end
# File lib/cms/behaviors/attaching.rb, line 139 139: def update_attachment_if_changed 140: if attachment 141: attachment.archived = archived if self.class.archivable? 142: if respond_to?(:revert_to_version) && revert_to_version 143: attachment.revert_to(revert_to_version.attachment_version) 144: elsif new_record? || attachment.changed? || attachment.temp_file 145: attachment.save 146: end 147: self.attachment_version = attachment.draft.version 148: end 149: end