Class: Cms::Attachment
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Cms::Attachment
- Defined in:
- app/models/cms/attachment.rb
Constant Summary collapse
- MULTIPLE =
'multiple'
- SANITIZATION_REGEXES =
[[/\s/, '_'], [/[&+()]/, '-'], [/[=?!'"{}\[\]#<>%]/, '']]
- FILE_BLOCKS =
"Cms::AbstractFileBlock"
- @@definitions =
{}.with_indifferent_access
Instance Attribute Summary collapse
-
#attachable_class ⇒ Object
Returns the value of attribute attachable_class.
Class Method Summary collapse
- .configuration ⇒ Object
-
.configuration_value(block_class, name_of_attachment, key) ⇒ Object
Looks up the configuration value given:.
- .configure_paperclip ⇒ Object
- .definitions_for(klass, type) ⇒ Object
-
.dynamically_return_styles ⇒ Object
Returns a Proc that can be used to dynamically determine styles based on the Cms::Attachment class.
- .find_live_by_file_path(path) ⇒ Object
-
.rail_config(key) ⇒ Object
Looks up a value from Rails config.
-
.sanitize_file_path(file_path) ⇒ Object
Makes file paths more URL friendly.
Instance Method Summary collapse
-
#attachment_version_path ⇒ Object
For authorized users, return the path to get the specific version of the file associated with this attachment.
-
#config ⇒ Hash
Returns the definitions for this particular attachment type.
- #config_value_for(key) ⇒ Object
- #content_block_class ⇒ Object
- #content_type ⇒ Object (also: #file_type)
-
#has_assigned_content_type? ⇒ Boolean
Determines if this Attachment has access to configuration information yet.
- #icon ⇒ Object
- #is_image? ⇒ Boolean
- #original_filename ⇒ Object (also: #file_name)
-
#path(style_name = configuration.default_style) ⇒ Object
Returns the absolute file location of the underlying asset.
- #public? ⇒ Boolean
- #section=(section) ⇒ Object
- #size ⇒ Object
-
#url(style_name = configuration.default_style) ⇒ Object
Returns a Paperclip generated relative path to the file (with thumbnail sizing).
Methods included from Cms::Addressable::DeprecatedPageAccessors
#build_node, #section, #section_id, #section_id=
Methods included from Cms::Addressable::NodeAccessors
Methods included from Cms::Addressable::LeafNode
Methods included from Addressable
#ancestors, #cache_parent, #parent, #parent=, #partial_for
Instance Attribute Details
#attachable_class ⇒ Object
Returns the value of attribute attachable_class.
14 15 16 |
# File 'app/models/cms/attachment.rb', line 14 def attachable_class @attachable_class end |
Class Method Details
.configuration ⇒ Object
63 64 65 |
# File 'app/models/cms/attachment.rb', line 63 def configuration @@configuration ||= Cms::Attachments.configuration end |
.configuration_value(block_class, name_of_attachment, key) ⇒ Object
Looks up the configuration value given:
121 122 123 124 125 126 127 128 129 130 131 |
# File 'app/models/cms/attachment.rb', line 121 def configuration_value(block_class, , key) class_definitions = definitions[block_class] if class_definitions == nil raise "Couldn't find any definitions for '#{block_class}'." end = class_definitions[] if == nil raise "Verify that '#{block_class}' defines an attachment named ':#{}'." end [key] || configuration.send(key) end |
.configure_paperclip ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'app/models/cms/attachment.rb', line 88 def configure_paperclip # @todo This might be better done using subclasses of Attachment for each document instance. # We could use single table inheritance to avoid needing to do meta configurations. has_attached_file :data, :url => configuration.url, :path => configuration.path, :styles => dynamically_return_styles, # Needed for versioning so that we keep all previous files. :preserve_files => true, #TODO: enable custom processors :processors => configuration.processors, :default_url => configuration.default_url, :default_style => configuration.default_style, :use_timestamp => configuration., :whiny => configuration.whiny, :storage => rail_config(:storage), :s3_credentials => rail_config(:s3_credentials), :bucket => rail_config(:s3_bucket) end |
.definitions_for(klass, type) ⇒ Object
59 60 61 |
# File 'app/models/cms/attachment.rb', line 59 def definitions_for(klass, type) definitions[klass].inject({}) { |d, (k, v)| d[k.capitalize] = v if v["type"] == type; d } end |
.dynamically_return_styles ⇒ Object
Returns a Proc that can be used to dynamically determine styles based on the Cms::Attachment class
Paperclip can handle a :styles parameter which responds to :call(Paperclip::Attachment)
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'app/models/cms/attachment.rb', line 70 def dynamically_return_styles lambda do || = .instance # Look up the style for the given block if .has_assigned_content_type? configuration_value(.content_block_class, ., :styles) else # New attachments that aren't associated with an Attaching type yet have no styles {} end end end |
.find_live_by_file_path(path) ⇒ Object
83 84 85 |
# File 'app/models/cms/attachment.rb', line 83 def find_live_by_file_path(path) Attachment.published.not_archived.find_by_data_file_path path end |
.rail_config(key) ⇒ Object
Looks up a value from Rails config
112 113 114 |
# File 'app/models/cms/attachment.rb', line 112 def rail_config(key) Rails.configuration.cms.[key] end |
.sanitize_file_path(file_path) ⇒ Object
Makes file paths more URL friendly
53 54 55 56 57 |
# File 'app/models/cms/attachment.rb', line 53 def sanitize_file_path(file_path) SANITIZATION_REGEXES.inject(file_path.to_s) do |s, (regex, replace)| s.gsub(regex, replace) end end |
Instance Method Details
#attachment_version_path ⇒ Object
For authorized users, return the path to get the specific version of the file associated with this attachment. Guests should always get ‘data_file_path’ which is the public version of the asset.
169 170 171 |
# File 'app/models/cms/attachment.rb', line 169 def "/cms/attachments/#{id}?version=#{version}" end |
#config ⇒ Hash
Returns the definitions for this particular attachment type.
209 210 211 212 |
# File 'app/models/cms/attachment.rb', line 209 def config content_defs = definitions[content_block_class] ? definitions[content_block_class] : {} content_defs[] ? content_defs[] : {} end |
#config_value_for(key) ⇒ Object
141 142 143 |
# File 'app/models/cms/attachment.rb', line 141 def config_value_for(key) self.class.configuration_value(content_block_class, , key) end |
#content_block_class ⇒ Object
145 146 147 |
# File 'app/models/cms/attachment.rb', line 145 def content_block_class attachable_class || attachable.try(:class).try(:name) || attachable_type end |
#content_type ⇒ Object Also known as: file_type
201 202 203 |
# File 'app/models/cms/attachment.rb', line 201 def content_type data_content_type end |
#has_assigned_content_type? ⇒ Boolean
Determines if this Attachment has access to configuration information yet. Until it is assigned to an Attaching object, it will lack style information.
216 217 218 |
# File 'app/models/cms/attachment.rb', line 216 def has_assigned_content_type?() attachable_type && end |
#icon ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'app/models/cms/attachment.rb', line 149 def icon { :doc => %w[doc], :gif => %w[gif jpg jpeg png tiff bmp], :htm => %w[htm html], :pdf => %w[pdf], :ppt => %w[ppt], :swf => %w[swf], :txt => %w[txt], :xls => %w[xls], :xml => %w[xml], :zip => %w[zip rar tar gz tgz] }.each do |icon, extensions| return icon if extensions.include?(data_file_extension) end :file end |
#is_image? ⇒ Boolean
177 178 179 |
# File 'app/models/cms/attachment.rb', line 177 def is_image? %w[jpg gif png jpeg].include?(data_file_extension) end |
#original_filename ⇒ Object Also known as: file_name
191 192 193 |
# File 'app/models/cms/attachment.rb', line 191 def original_filename data_file_name end |
#path(style_name = configuration.default_style) ⇒ Object
Returns the absolute file location of the underlying asset
187 188 189 |
# File 'app/models/cms/attachment.rb', line 187 def path(style_name = configuration.default_style) data.path(style_name) end |
#public? ⇒ Boolean
173 174 175 |
# File 'app/models/cms/attachment.rb', line 173 def public? section ? section.public? : false end |
#section=(section) ⇒ Object
136 137 138 139 |
# File 'app/models/cms/attachment.rb', line 136 def section=(section) dirty! if self.section != section super(section) end |
#size ⇒ Object
197 198 199 |
# File 'app/models/cms/attachment.rb', line 197 def size data_file_size end |
#url(style_name = configuration.default_style) ⇒ Object
Returns a Paperclip generated relative path to the file (with thumbnail sizing)
182 183 184 |
# File 'app/models/cms/attachment.rb', line 182 def url(style_name = configuration.default_style) data.url(style_name) end |