Class: AssetType
- Inherits:
-
Object
- Object
- AssetType
- Defined in:
- app/models/asset_type.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:
* processing and variant definitions for ActiveStorage * 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
Parses and combines the various ways in which ActiveStorage styles can be defined, and normalises them into the format that ActiveStorage expects.
- #condition ⇒ Object
-
#configured_styles ⇒ Object
ActiveStorage 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 variant processor.
- #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.
- #plural ⇒ Object
- #processing_enabled? ⇒ Boolean
- #sanitized_condition ⇒ Object
- #sanitized_non_condition ⇒ Object
- #standard_styles ⇒ Object
- #style_dimensions(style_name) ⇒ Object
- #style_format(style_name) ⇒ Object
Constructor Details
#initialize(name, options = {}) ⇒ AssetType
Returns a new instance of AssetType.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'app/models/asset_type.rb', line 18 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?(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.
16 17 18 |
# File 'app/models/asset_type.rb', line 16 def catchall @catchall end |
#default_radius_tag ⇒ Object (readonly)
Returns the value of attribute default_radius_tag.
16 17 18 |
# File 'app/models/asset_type.rb', line 16 def default_radius_tag @default_radius_tag end |
#icon_name ⇒ Object (readonly)
Returns the value of attribute icon_name.
16 17 18 |
# File 'app/models/asset_type.rb', line 16 def icon_name @icon_name end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
16 17 18 |
# File 'app/models/asset_type.rb', line 16 def name @name end |
#processors ⇒ Object (readonly)
Returns the value of attribute processors.
16 17 18 |
# File 'app/models/asset_type.rb', line 16 def processors @processors end |
#styles ⇒ Object (readonly)
Returns the value of attribute styles.
16 17 18 |
# File 'app/models/asset_type.rb', line 16 def styles @styles end |
Class Method Details
.[](type) ⇒ Object
208 209 210 |
# File 'app/models/asset_type.rb', line 208 def self.[](type) find(type) end |
.all ⇒ Object
212 213 214 |
# File 'app/models/asset_type.rb', line 212 def self.all @@types end |
.catchall ⇒ Object
192 193 194 |
# File 'app/models/asset_type.rb', line 192 def self.catchall @@default_type ||= find(:other) end |
.conditions_for(*names) ⇒ Object
228 229 230 |
# File 'app/models/asset_type.rb', line 228 def self.conditions_for(*names) names.collect { |name| find(name).sanitized_condition }.join(' OR ') end |
.find(type) ⇒ Object
204 205 206 |
# File 'app/models/asset_type.rb', line 204 def self.find(type) @@type_lookup[type] if type end |
.for(attachment) ⇒ Object
class methods
177 178 179 180 181 182 |
# File 'app/models/asset_type.rb', line 177 def self.for() return catchall unless &.attached? extension = .blob&.filename&.extension&.downcase || .record.original_extension from_extension(extension) || from_mimetype(.blob&.content_type) || catchall end |
.from_extension(extension) ⇒ Object
184 185 186 |
# File 'app/models/asset_type.rb', line 184 def self.from_extension(extension) @@extension_lookup[extension] end |
.from_mimetype(mimetype) ⇒ Object
188 189 190 |
# File 'app/models/asset_type.rb', line 188 def self.from_mimetype(mimetype) @@mime_lookup[mimetype] end |
.known?(name) ⇒ Boolean
196 197 198 |
# File 'app/models/asset_type.rb', line 196 def self.known?(name) !find(name).nil? end |
.known_mimetypes ⇒ Object
220 221 222 |
# File 'app/models/asset_type.rb', line 220 def self.known_mimetypes @@mime_lookup.keys end |
.known_types ⇒ Object
216 217 218 |
# File 'app/models/asset_type.rb', line 216 def self.known_types @@types.map(&:name) # to preserve order end |
.mime_types_for(*names) ⇒ Object
224 225 226 |
# File 'app/models/asset_type.rb', line 224 def self.mime_types_for(*names) names.collect { |name| find(name).mime_types }.flatten end |
.non_other_condition ⇒ Object
232 233 234 |
# File 'app/models/asset_type.rb', line 232 def self.non_other_condition ["asset_content_type IN (#{known_mimetypes.map { '?' }.join(',')})", *known_mimetypes] end |
.other_condition ⇒ Object
236 237 238 |
# File 'app/models/asset_type.rb', line 236 def self.other_condition ["NOT asset_content_type IN (#{known_mimetypes.map { '?' }.join(',')})", *known_mimetypes] end |
.slice(*types) ⇒ Object
200 201 202 |
# File 'app/models/asset_type.rb', line 200 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
Parses and combines the various ways in which ActiveStorage styles can be defined, and normalises them into the format that ActiveStorage expects. Note that :styles => :standard has already been replaced with the results of a call to standard_styles. Styles are passed as a hash and arbitrary keys can be passed through from configuration.
96 97 98 99 100 101 102 103 |
# File 'app/models/asset_type.rb', line 96 def active_storage_styles @active_storage_styles ||= if processing_enabled? normalize_style_rules(configured_styles.merge(styles)) else {} end @active_storage_styles end |
#condition ⇒ Object
59 60 61 62 63 64 65 |
# File 'app/models/asset_type.rb', line 59 def condition if @mimes.any? ["asset_content_type IN (#{@mimes.map { '?' }.join(',')})", *@mimes] else self.class.other_condition end end |
#configured_styles ⇒ Object
ActiveStorage 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 variant processor. 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.
140 141 142 143 144 145 146 147 148 149 |
# File 'app/models/asset_type.rb', line 140 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
163 164 165 166 167 168 169 170 171 172 173 |
# File 'app/models/asset_type.rb', line 163 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
47 48 49 50 51 52 53 |
# File 'app/models/asset_type.rb', line 47 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
55 56 57 |
# File 'app/models/asset_type.rb', line 55 def icon_path(style_name = 'icon') Rails.root + "public#{icon(style_name)}" end |
#mime_types ⇒ Object
83 84 85 |
# File 'app/models/asset_type.rb', line 83 def mime_types @mimes end |
#non_condition ⇒ Object
71 72 73 74 75 76 77 |
# File 'app/models/asset_type.rb', line 71 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 #
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'app/models/asset_type.rb', line 107 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 |
#plural ⇒ Object
43 44 45 |
# File 'app/models/asset_type.rb', line 43 def plural name.to_s.pluralize end |
#processing_enabled? ⇒ Boolean
87 88 89 |
# File 'app/models/asset_type.rb', line 87 def processing_enabled? TrustyCms.config["assets.create_#{name}_thumbnails?"] end |
#sanitized_condition ⇒ Object
67 68 69 |
# File 'app/models/asset_type.rb', line 67 def sanitized_condition ActiveRecord::Base.send :sanitize_sql_array, condition end |
#sanitized_non_condition ⇒ Object
79 80 81 |
# File 'app/models/asset_type.rb', line 79 def sanitized_non_condition ActiveRecord::Base.send :sanitize_sql_array, non_condition end |
#standard_styles ⇒ Object
123 124 125 126 127 128 129 |
# File 'app/models/asset_type.rb', line 123 def standard_styles { icon: { geometry: '50x50#', format: :png }, thumbnail: { geometry: '100x100#', format: :png }, original: {}, } end |
#style_dimensions(style_name) ⇒ Object
151 152 153 154 155 |
# File 'app/models/asset_type.rb', line 151 def style_dimensions(style_name) if style = active_storage_styles[style_name.to_sym] style[:geometry] end end |
#style_format(style_name) ⇒ Object
157 158 159 160 161 |
# File 'app/models/asset_type.rb', line 157 def style_format(style_name) if style = active_storage_styles[style_name.to_sym] style[:format] end end |