Class: Cms::Attachment
Constant Summary
collapse
- MULTIPLE =
'multiple'
- SANITIZATION_REGEXES =
[[/\s/, '_'], [/[&+()]/, '-'], [/[=?!'"{}\[\]#<>%]/, '']]
- FILE_BLOCKS =
"Cms::AbstractFileBlock"
- @@definitions =
{}.with_indifferent_access
Class Method Summary
collapse
Instance Method Summary
collapse
non_permitted_params, permitted_params
#build_node, #section, #section_id, #section_id=
Class Method Details
.configuration ⇒ Object
56
57
58
|
# File 'app/models/cms/attachment.rb', line 56
def configuration
@@configuration ||= Cms::Attachments.configuration
end
|
.configuration_value(block_class, name_of_attachment, key) ⇒ Object
Looks up the configuration value given:
115
116
117
118
119
120
121
122
123
124
125
|
# File 'app/models/cms/attachment.rb', line 115
def configuration_value(block_class, name_of_attachment, key)
class_definitions = definitions[block_class]
if class_definitions == nil
raise "Couldn't find any definitions for '#{block_class}'. Available definitions are #{definitions.inspect}."
end
attachment_definition = class_definitions[name_of_attachment]
if attachment_definition == nil
raise "Verify that '#{block_class}' defines an attachment named ':#{name_of_attachment}'."
end
attachment_definition[key] || configuration.send(key)
end
|
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'app/models/cms/attachment.rb', line 81
def configure_paperclip
has_attached_file :data,
:url => configuration.url,
:path => configuration.path,
:styles => dynamically_return_styles,
:preserve_files => true,
:processors => configuration.processors,
:default_url => configuration.default_url,
:default_style => configuration.default_style,
:use_timestamp => configuration.use_timestamp,
:whiny => configuration.whiny,
:storage => rail_config(:storage),
:s3_credentials => rail_config(:s3_credentials),
:bucket => rail_config(:s3_bucket),
:s3_host_name => rail_config(:s3_host_name),
:s3_host_alias => rail_config(:s3_host_alias)
end
|
.definitions_for(klass, type) ⇒ Object
52
53
54
|
# File 'app/models/cms/attachment.rb', line 52
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)
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'app/models/cms/attachment.rb', line 63
def dynamically_return_styles
lambda do |paperclip_attachment|
cms_attachment = paperclip_attachment.instance
if cms_attachment.has_assigned_content_type?
configuration_value(cms_attachment.content_block_class, cms_attachment.attachment_name, :styles)
else
{}
end
end
end
|
.find_live_by_file_path(path) ⇒ Object
76
77
78
|
# File 'app/models/cms/attachment.rb', line 76
def find_live_by_file_path(path)
Attachment.published.not_archived.find_by_data_file_path path
end
|
.permitted_params ⇒ Object
42
43
44
|
# File 'app/models/cms/attachment.rb', line 42
def permitted_params
super + [:attachable_class, :data]
end
|
.rail_config(key) ⇒ Object
Looks up a value from Rails config
107
108
109
|
# File 'app/models/cms/attachment.rb', line 107
def rail_config(key)
Rails.configuration.cms.attachments[key]
end
|
.sanitize_file_path(file_path) ⇒ Object
Makes file paths more URL friendly
46
47
48
49
50
|
# File 'app/models/cms/attachment.rb', line 46
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
#attachable_class ⇒ Object
129
130
131
|
# File 'app/models/cms/attachment.rb', line 129
def attachable_class
attachable_type
end
|
#attachable_class=(klass) ⇒ Object
134
135
136
|
# File 'app/models/cms/attachment.rb', line 134
def attachable_class=(klass)
self.attachable_type = klass
end
|
#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.
172
173
174
|
# File 'app/models/cms/attachment.rb', line 172
def attachment_version_path
"/cms/attachments/#{id}?version=#{version}"
end
|
#config ⇒ Hash
Returns the definitions for this particular attachment type.
212
213
214
215
|
# File 'app/models/cms/attachment.rb', line 212
def config
content_defs = definitions[content_block_class] ? definitions[content_block_class] : {}
content_defs[attachment_name] ? content_defs[attachment_name] : {}
end
|
#config_value_for(key) ⇒ Object
143
144
145
|
# File 'app/models/cms/attachment.rb', line 143
def config_value_for(key)
self.class.configuration_value(content_block_class, attachment_name, key)
end
|
#content_block_class ⇒ Object
147
148
149
150
|
# File 'app/models/cms/attachment.rb', line 147
def content_block_class
attachable.try(:class).try(:name) || attachable_type
end
|
#content_type ⇒ Object
Also known as:
file_type
204
205
206
|
# File 'app/models/cms/attachment.rb', line 204
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.
219
220
221
|
# File 'app/models/cms/attachment.rb', line 219
def has_assigned_content_type?()
attachable_type && attachment_name
end
|
#icon ⇒ Object
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
# File 'app/models/cms/attachment.rb', line 152
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
180
181
182
|
# File 'app/models/cms/attachment.rb', line 180
def is_image?
%w[jpg gif png jpeg].include?(data_file_extension)
end
|
#original_filename ⇒ Object
Also known as:
file_name
194
195
196
|
# File 'app/models/cms/attachment.rb', line 194
def original_filename
data_file_name
end
|
#path(style_name = configuration.default_style) ⇒ Object
Returns the absolute file location of the underlying asset
190
191
192
|
# File 'app/models/cms/attachment.rb', line 190
def path(style_name = configuration.default_style)
data.path(style_name)
end
|
#public? ⇒ Boolean
176
177
178
|
# File 'app/models/cms/attachment.rb', line 176
def public?
section ? section.public? : false
end
|
#section=(section) ⇒ Object
138
139
140
141
|
# File 'app/models/cms/attachment.rb', line 138
def section=(section)
dirty! if self.section != section
super(section)
end
|
#size ⇒ Object
200
201
202
|
# File 'app/models/cms/attachment.rb', line 200
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)
185
186
187
|
# File 'app/models/cms/attachment.rb', line 185
def url(style_name = configuration.default_style)
data.url(style_name)
end
|