Class: AssetType

Inherits:
Object
  • Object
show all
Defined in:
app/models/asset_type.rb,
lib/trusty_cms/deprecation.rb

Constant Summary collapse

@@types =

The Asset Type encapsulates a type of attachment. Conventionally this would a sensible category like ‘image’ or ‘video’ that should be processed and presented in a particular way. An AssetType currently provides:

* processor definitions for paperclip
* styles definitions for paperclip
* mime type list for file recognition
* selectors and scopes for retrieving this (or not this) category of asset
* radius tags for those subsets of assets (temporarily removed pending discussion of interface)
[]
@@type_lookup =
{}
@@extension_lookup =
{}
@@mime_lookup =
{}
@@default_type =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ AssetType

Returns a new instance of AssetType.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/models/asset_type.rb', line 19

def initialize(name, options = {})
  options = options.symbolize_keys
  @name = name
  @icon_name = options[:icon] || name
  @processors = options[:processors] || []
  @styles = options[:styles] || {}
  @styles = standard_styles if @styles == :standard
  @default_radius_tag = options[:default_radius_tag] || 'link'
  @extensions = options[:extensions] || []
  @extensions.each { |ext| @@extension_lookup[ext] ||= self }
  @mimes = options[:mime_types] || []
  @mimes.each { |mimetype| @@mime_lookup[mimetype] ||= self }

  this = self
  Asset.send :define_method, "#{name}?".intern do this.mime_types.include?(asset_content_type) end
  Asset.send :define_class_method, "#{name}_condition".intern do this.condition; end
  Asset.send :define_class_method, "not_#{name}_condition".intern do this.non_condition; end
  Asset.send :scope, plural.to_sym, -> { where(conditions: condition) }
  Asset.send :scope, "not_#{plural}".to_sym, -> { where(conditions: non_condition) }

  define_radius_tags
  @@types.push self
  @@type_lookup[@name] = self
end

Instance Attribute Details

#catchallObject (readonly)

Returns the value of attribute catchall.



17
18
19
# File 'app/models/asset_type.rb', line 17

def catchall
  @catchall
end

#default_radius_tagObject (readonly)

Returns the value of attribute default_radius_tag.



17
18
19
# File 'app/models/asset_type.rb', line 17

def default_radius_tag
  @default_radius_tag
end

#icon_nameObject (readonly)

Returns the value of attribute icon_name.



17
18
19
# File 'app/models/asset_type.rb', line 17

def icon_name
  @icon_name
end

#nameObject (readonly)

Returns the value of attribute name.



17
18
19
# File 'app/models/asset_type.rb', line 17

def name
  @name
end

#processorsObject (readonly)

Returns the value of attribute processors.



17
18
19
# File 'app/models/asset_type.rb', line 17

def processors
  @processors
end

#stylesObject (readonly)

Returns the value of attribute styles.



17
18
19
# File 'app/models/asset_type.rb', line 17

def styles
  @styles
end

Class Method Details

.[](type) ⇒ Object



204
205
206
# File 'app/models/asset_type.rb', line 204

def self.[](type)
  find(type)
end

.allObject



208
209
210
# File 'app/models/asset_type.rb', line 208

def self.all
  @@types
end

.catchallObject



188
189
190
# File 'app/models/asset_type.rb', line 188

def self.catchall
  @@default_type ||= find(:other)
end

.conditions_for(*names) ⇒ Object



224
225
226
# File 'app/models/asset_type.rb', line 224

def self.conditions_for(*names)
  names.collect { |name| find(name).sanitized_condition }.join(' OR ')
end

.find(type) ⇒ Object



200
201
202
# File 'app/models/asset_type.rb', line 200

def self.find(type)
  @@type_lookup[type] if type
end

.for(attachment) ⇒ Object

class methods



175
176
177
178
# File 'app/models/asset_type.rb', line 175

def self.for(attachment)
  extension = attachment.record.original_extension
  from_extension(extension) || from_mimetype(attachment.content_type) || catchall
end

.from_extension(extension) ⇒ Object



180
181
182
# File 'app/models/asset_type.rb', line 180

def self.from_extension(extension)
  @@extension_lookup[extension]
end

.from_mimetype(mimetype) ⇒ Object



184
185
186
# File 'app/models/asset_type.rb', line 184

def self.from_mimetype(mimetype)
  @@mime_lookup[mimetype]
end

.known?(name) ⇒ Boolean

Returns:

  • (Boolean)


192
193
194
# File 'app/models/asset_type.rb', line 192

def self.known?(name)
  !find(name).nil?
end

.known_mimetypesObject



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

def self.known_mimetypes
  @@mime_lookup.keys
end

.known_typesObject



212
213
214
# File 'app/models/asset_type.rb', line 212

def self.known_types
  @@types.map(&:name) # to preserve order
end

.mime_types_for(*names) ⇒ Object



220
221
222
# File 'app/models/asset_type.rb', line 220

def self.mime_types_for(*names)
  names.collect { |name| find(name).mime_types }.flatten
end

.non_other_conditionObject



228
229
230
# File 'app/models/asset_type.rb', line 228

def self.non_other_condition
  ["asset_content_type IN (#{known_mimetypes.map { '?' }.join(',')})", *known_mimetypes]
end

.other_conditionObject



232
233
234
# File 'app/models/asset_type.rb', line 232

def self.other_condition
  ["NOT asset_content_type IN (#{known_mimetypes.map { '?' }.join(',')})", *known_mimetypes]
end

.slice(*types) ⇒ Object



196
197
198
# File 'app/models/asset_type.rb', line 196

def self.slice(*types)
  @@type_lookup.slice(*types.map(&:to_sym)).values if types # Hash#slice is provided by will_paginate
end

Instance Method Details

#active_storage_stylesObject



107
108
109
# File 'app/models/asset_type.rb', line 107

def active_storage_styles
  @active_storage_styles ||= normalize_style_rules(configured_styles.merge(styles))
end

#conditionObject



60
61
62
63
64
65
66
# File 'app/models/asset_type.rb', line 60

def condition
  if @mimes.any?
    ["asset_content_type IN (#{@mimes.map { '?' }.join(',')})", *@mimes]
  else
    self.class.other_condition
  end
end

#configured_stylesObject

Paperclip styles are defined in the config entry ‘assets.thumbnails.asset_type`, with the format: foo:key-x,key=y,key=z|bar:key-x,key=y,key=z where ’key’ can be any parameter understood by your paperclip processors. Usually they include :geometry and :format. A typical entry would be:

standard:geometry=640x640>,format=jpg

This method parses that string and returns the defined styles as a hash of style-defining strings that will later be normalized into hashes.



144
145
146
147
148
149
150
151
152
153
# File 'app/models/asset_type.rb', line 144

def configured_styles
  @configured_styles ||= if style_definitions = TrustyCms.config["assets.thumbnails.#{name}"]
                           style_definitions.split('|').each_with_object({}) do |definition, styles|
                             name, rule = definition.split(':')
                             styles[name.strip.to_sym] = rule.to_s.strip
                           end
                         else
                           {}
  end
end

#define_radius_tagsObject



161
162
163
164
165
166
167
168
169
170
171
# File 'app/models/asset_type.rb', line 161

def define_radius_tags
  type = name
  Page.class_eval do
    tag "asset:if_#{type}" do |tag|
      tag.expand if find_asset(tag, tag.attr.dup).send("#{type}?".to_sym)
    end
    tag "asset:unless_#{type}" do |tag|
      tag.expand unless find_asset(tag, tag.attr.dup).send("#{type}?".to_sym)
    end
  end
end

#icon(style_name = 'icon') ⇒ Object



48
49
50
51
52
53
54
# File 'app/models/asset_type.rb', line 48

def icon(style_name = 'icon')
  if File.exist?(Rails.root + "public/images/admin/assets/#{icon_name}_#{style_name}.png")
    "/assets/admin/#{icon_name}_#{style_name}.png"
  else
    "/assets/admin/#{icon_name}_icon.png"
  end
end

#icon_path(style_name = 'icon') ⇒ Object



56
57
58
# File 'app/models/asset_type.rb', line 56

def icon_path(style_name = 'icon')
  Rails.root + "public#{icon(style_name)}"
end

#mime_typesObject



84
85
86
# File 'app/models/asset_type.rb', line 84

def mime_types
  @mimes
end

#non_conditionObject



72
73
74
75
76
77
78
# File 'app/models/asset_type.rb', line 72

def non_condition
  if @mimes.any?
    ["NOT asset_content_type IN (#{@mimes.map { '?' }.join(',')})", *@mimes]
  else
    self.class.non_other_condition
  end
end

#normalize_style_rules(styles = {}) ⇒ Object

Takes a motley collection of differently-defined styles and renders them into the standard hash-of-hashes format. Solitary strings are assumed to be #



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/models/asset_type.rb', line 113

def normalize_style_rules(styles = {})
  styles.each_pair do |name, rule|
    unless rule.is_a? Hash
      if rule =~ /\=/
        parameters = rule.split(',').collect { |parameter| parameter.split('=') } # array of pairs
        rule = Hash[parameters].symbolize_keys # into hash of :first => last
      else
        rule = { geometry: rule } # simplest case: name:geom|name:geom
      end
    end
    rule[:geometry] ||= rule.delete(:size)
    styles[name.to_sym] = rule
  end
  styles
end

#paperclip_processorsObject



88
89
90
# File 'app/models/asset_type.rb', line 88

def paperclip_processors
  TrustyCms.config["assets.create_#{name}_thumbnails?"] ? processors : []
end

#paperclip_stylesObject

Parses and combines the various ways in which paperclip styles can be defined, and normalises them into the format that paperclip expects. Note that :styles => :standard has already been replaced with the results of a call to standard_styles. Styles are passed to paperclip as a hash and arbitrary keys can be passed through from configuration.



97
98
99
100
101
102
103
104
105
# File 'app/models/asset_type.rb', line 97

def paperclip_styles
  # Styles are not relevant if processors are not defined.
  @paperclip_styles ||= if paperclip_processors.any?
                          normalize_style_rules(configured_styles.merge(styles))
                        else
                          {}
  end
  @paperclip_styles
end

#pluralObject



44
45
46
# File 'app/models/asset_type.rb', line 44

def plural
  name.to_s.pluralize
end

#sanitized_conditionObject



68
69
70
# File 'app/models/asset_type.rb', line 68

def sanitized_condition
  ActiveRecord::Base.send :sanitize_sql_array, condition
end

#sanitized_non_conditionObject



80
81
82
# File 'app/models/asset_type.rb', line 80

def sanitized_non_condition
  ActiveRecord::Base.send :sanitize_sql_array, non_condition
end

#standard_stylesObject



129
130
131
132
133
# File 'app/models/asset_type.rb', line 129

def standard_styles
  {
    thumbnail: { geometry: '100x100#', format: :png },
  }
end

#style_dimensions(style_name) ⇒ Object



155
156
157
158
159
# File 'app/models/asset_type.rb', line 155

def style_dimensions(style_name)
  if style = paperclip_styles[style_name.to_sym]
    style[:size]
  end
end