Class: Cms::Attachment

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Addressable, Cms::Addressable::DeprecatedPageAccessors, DefaultAccessible
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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Cms::Addressable::DeprecatedPageAccessors

#build_node, #section, #section_id, #section_id=

Methods included from Cms::Addressable::NodeAccessors

#node=

Methods included from Cms::Addressable::LeafNode

#access_status

Methods included from Addressable

#ancestors, #cache_parent, #parent, #parent=, #partial_for

Instance Attribute Details

#attachable_classObject

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

.configurationObject



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:

Parameters:

  • block_class (Class)

    The class of a block which has an attachment.

  • name_of_attachment (String)

    The name of the attachment association (i.e. if was ‘has_attachment :photos’ then pass ‘photo’)

  • key (Symbol)

    The key for the value to be fetched (i.e. :styles)



121
122
123
124
125
126
127
128
129
130
131
# File 'app/models/cms/attachment.rb', line 121

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}'."
  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

.configure_paperclipObject



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.use_timestamp,
                    :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_stylesObject

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 |paperclip_attachment|
    cms_attachment = paperclip_attachment.instance

    # Look up the style for the given block
    if cms_attachment.has_assigned_content_type?
      configuration_value(cms_attachment.content_block_class, cms_attachment.attachment_name, :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.attachments[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_pathObject

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 attachment_version_path
  "/cms/attachments/#{id}?version=#{version}"
end

#configHash

Returns the definitions for this particular attachment type.

Returns:

  • (Hash)

    Empty Hash if no definition have been defined for this attachment.



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[attachment_name] ? content_defs[attachment_name] : {}
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, attachment_name, key)
end

#content_block_classObject



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_typeObject 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.

Returns:

  • (Boolean)


216
217
218
# File 'app/models/cms/attachment.rb', line 216

def has_assigned_content_type?()
  attachable_type && attachment_name
end

#iconObject



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

Returns:

  • (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_filenameObject 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

Returns:

  • (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

#sizeObject



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