Class: AssetType
- Inherits:
-
Object
- Object
- AssetType
- 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
-
#catchall ⇒ Object
readonly
Returns the value of attribute catchall.
-
#default_radius_tag ⇒ Object
readonly
Returns the value of attribute default_radius_tag.
-
#icon_name ⇒ Object
readonly
Returns the value of attribute icon_name.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#processors ⇒ Object
readonly
Returns the value of attribute processors.
-
#styles ⇒ Object
readonly
Returns the value of attribute styles.
Class Method Summary collapse
- .[](type) ⇒ Object
- .all ⇒ Object
- .catchall ⇒ Object
- .conditions_for(*names) ⇒ Object
- .find(type) ⇒ Object
-
.for(attachment) ⇒ Object
class methods.
- .from_extension(extension) ⇒ Object
- .from_mimetype(mimetype) ⇒ Object
- .known?(name) ⇒ Boolean
- .known_mimetypes ⇒ Object
- .known_types ⇒ Object
- .mime_types_for(*names) ⇒ Object
- .non_other_condition ⇒ Object
- .other_condition ⇒ Object
- .slice(*types) ⇒ Object
Instance Method Summary collapse
- #active_storage_styles ⇒ Object
- #condition ⇒ Object
-
#configured_styles ⇒ Object
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.
- #define_radius_tags ⇒ Object
- #icon(style_name = 'icon') ⇒ Object
- #icon_path(style_name = 'icon') ⇒ Object
-
#initialize(name, options = {}) ⇒ AssetType
constructor
A new instance of AssetType.
- #mime_types ⇒ Object
- #non_condition ⇒ Object
-
#normalize_style_rules(styles = {}) ⇒ Object
Takes a motley collection of differently-defined styles and renders them into the standard hash-of-hashes format.
- #paperclip_processors ⇒ Object
-
#paperclip_styles ⇒ Object
Parses and combines the various ways in which paperclip styles can be defined, and normalises them into the format that paperclip expects.
- #plural ⇒ Object
- #sanitized_condition ⇒ Object
- #sanitized_non_condition ⇒ Object
- #standard_styles ⇒ Object
- #style_dimensions(style_name) ⇒ Object
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, = {}) = .symbolize_keys @name = name @icon_name = [:icon] || name @processors = [:processors] || [] @styles = [:styles] || {} @styles = standard_styles if @styles == :standard @default_radius_tag = [:default_radius_tag] || 'link' @extensions = [:extensions] || [] @extensions.each { |ext| @@extension_lookup[ext] ||= self } @mimes = [: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) } @@types.push self @@type_lookup[@name] = self end |
Instance Attribute Details
#catchall ⇒ Object (readonly)
Returns the value of attribute catchall.
17 18 19 |
# File 'app/models/asset_type.rb', line 17 def catchall @catchall end |
#default_radius_tag ⇒ Object (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_name ⇒ Object (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 |
#name ⇒ Object (readonly)
Returns the value of attribute name.
17 18 19 |
# File 'app/models/asset_type.rb', line 17 def name @name end |
#processors ⇒ Object (readonly)
Returns the value of attribute processors.
17 18 19 |
# File 'app/models/asset_type.rb', line 17 def processors @processors end |
#styles ⇒ Object (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 |
.all ⇒ Object
208 209 210 |
# File 'app/models/asset_type.rb', line 208 def self.all @@types end |
.catchall ⇒ Object
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() extension = .record.original_extension from_extension(extension) || from_mimetype(.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
192 193 194 |
# File 'app/models/asset_type.rb', line 192 def self.known?(name) !find(name).nil? end |
.known_mimetypes ⇒ Object
216 217 218 |
# File 'app/models/asset_type.rb', line 216 def self.known_mimetypes @@mime_lookup.keys end |
.known_types ⇒ Object
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_condition ⇒ Object
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_condition ⇒ Object
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_styles ⇒ Object
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 |
#condition ⇒ Object
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_styles ⇒ Object
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_tags ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 |
# File 'app/models/asset_type.rb', line 161 def type = name Page.class_eval do tag "asset:if_#{type}" do |tag| tag. if find_asset(tag, tag.attr.dup).send("#{type}?".to_sym) end tag "asset:unless_#{type}" do |tag| tag. 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_types ⇒ Object
84 85 86 |
# File 'app/models/asset_type.rb', line 84 def mime_types @mimes end |
#non_condition ⇒ Object
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_processors ⇒ Object
88 89 90 |
# File 'app/models/asset_type.rb', line 88 def paperclip_processors TrustyCms.config["assets.create_#{name}_thumbnails?"] ? processors : [] end |
#paperclip_styles ⇒ Object
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 |
#plural ⇒ Object
44 45 46 |
# File 'app/models/asset_type.rb', line 44 def plural name.to_s.pluralize end |
#sanitized_condition ⇒ Object
68 69 70 |
# File 'app/models/asset_type.rb', line 68 def sanitized_condition ActiveRecord::Base.send :sanitize_sql_array, condition end |
#sanitized_non_condition ⇒ Object
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_styles ⇒ Object
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 |