Class: Asset
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Asset
- Defined in:
- app/models/asset.rb
Class Method Summary collapse
- .approved_content_types ⇒ Object
- .count_with_asset_types(asset_types, *args) ⇒ Object
-
.define_class_method(name, &block) ⇒ Object
called from AssetType to set type_condition? methods on Asset.
-
.eigenclass ⇒ Object
returns the return value of class << self block, which is self (as defined within that block).
-
.find_all_by_asset_types(asset_types, *args) ⇒ Object
searching and pagination moved to the controller.
- .known_types ⇒ Object
- .ransackable_attributes(auth_object = nil) ⇒ Object
- .thumbnail_names ⇒ Object
-
.thumbnail_options ⇒ Object
this is a convenience for image-pickers.
-
.thumbnail_sizes ⇒ Object
for backwards compatibility.
- .with_asset_types(asset_types, &block) ⇒ Object
Instance Method Summary collapse
- #aspect(style_name = 'original') ⇒ Object
- #asset_type ⇒ Object
- #asset_variant(style_name) ⇒ Object
- #asset_within_configured_size ⇒ Object
- #attached_to?(page) ⇒ Boolean
- #basename ⇒ Object
- #byte_size ⇒ Object
- #content_type ⇒ Object
- #dimensions_known? ⇒ Boolean
- #extension(style_name = 'original') ⇒ Object
- #filename ⇒ Object
- #geometry(style_name = 'original') ⇒ Object
- #height(style_name = 'original') ⇒ Object
- #horizontal?(style_name = 'original') ⇒ Boolean
- #orientation(style_name = 'original') ⇒ Object
- #original_extension ⇒ Object
- #original_geometry ⇒ Object
- #public_url(style_name = 'normal') ⇒ Object
- #render_original(_style_name) ⇒ Object
- #square?(style_name = 'original') ⇒ Boolean
- #style?(style_name = 'original') ⇒ Boolean
- #thumbnail(style_name = 'normal') ⇒ Object
- #vertical?(style_name = 'original') ⇒ Boolean
- #width(style_name = 'original') ⇒ Object
Class Method Details
.approved_content_types ⇒ Object
38 39 40 |
# File 'app/models/asset.rb', line 38 def self.approved_content_types AssetType.known_mimetypes end |
.count_with_asset_types(asset_types, *args) ⇒ Object
305 306 307 |
# File 'app/models/asset.rb', line 305 def count_with_asset_types(asset_types, *args) with_asset_types(asset_types) { where(*args).count } end |
.define_class_method(name, &block) ⇒ Object
called from AssetType to set type_condition? methods on Asset
316 317 318 |
# File 'app/models/asset.rb', line 316 def self.define_class_method(name, &block) eigenclass.send :define_method, name, &block end |
.eigenclass ⇒ Object
returns the return value of class << self block, which is self (as defined within that block)
321 322 323 324 325 |
# File 'app/models/asset.rb', line 321 def self.eigenclass class << self self; end end |
.find_all_by_asset_types(asset_types, *args) ⇒ Object
searching and pagination moved to the controller
301 302 303 |
# File 'app/models/asset.rb', line 301 def find_all_by_asset_types(asset_types, *args) with_asset_types(asset_types) { where *args } end |
.known_types ⇒ Object
295 296 297 |
# File 'app/models/asset.rb', line 295 def known_types AssetType.known_types end |
.ransackable_attributes(auth_object = nil) ⇒ Object
109 110 111 |
# File 'app/models/asset.rb', line 109 def self.ransackable_attributes(auth_object = nil) %w[asset_content_type asset_file_name asset_file_size caption created_at created_by_id id original_extension original_height original_width title updated_at updated_by_id uuid] end |
.thumbnail_names ⇒ Object
332 333 334 |
# File 'app/models/asset.rb', line 332 def self.thumbnail_names thumbnail_sizes.keys end |
.thumbnail_options ⇒ Object
this is a convenience for image-pickers
337 338 339 340 341 342 343 344 345 346 |
# File 'app/models/asset.rb', line 337 def self. asset_sizes = thumbnail_sizes.map do |k, v| size_id = k size_description = "#{k}: " size_description << (v.is_a?(Array) ? v.join(' as ') : v) [size_description, size_id] end.sort_by { |pair| pair.last.to_s } asset_sizes.unshift ['Original (as uploaded)', 'original'] asset_sizes end |
.thumbnail_sizes ⇒ Object
for backwards compatibility
328 329 330 |
# File 'app/models/asset.rb', line 328 def self.thumbnail_sizes AssetType.find(:image).active_storage_styles end |
.with_asset_types(asset_types, &block) ⇒ Object
309 310 311 312 |
# File 'app/models/asset.rb', line 309 def with_asset_types(asset_types, &block) w_asset_types = AssetType.conditions_for(asset_types) with_scope(where(conditions: ["#{w_asset_types} = ?", block])) end |
Instance Method Details
#aspect(style_name = 'original') ⇒ Object
184 185 186 |
# File 'app/models/asset.rb', line 184 def aspect(style_name = 'original') geometry(style_name).aspect end |
#asset_type ⇒ Object
64 65 66 |
# File 'app/models/asset.rb', line 64 def asset_type AssetType.for(asset) end |
#asset_variant(style_name) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'app/models/asset.rb', line 120 def asset_variant(style_name) style = active_storage_styles[style_name.to_sym] return unless style transformations = active_storage_transformations(style[:geometry]) transformations[:format] = style[:format] if style[:format] if asset.variable? asset.variant(transformations) elsif asset.previewable? asset.preview(transformations) end end |
#asset_within_configured_size ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'app/models/asset.rb', line 50 def asset_within_configured_size limit_mb = if video_content_type? TrustyCms.config['assets.max_video_size'].to_i else TrustyCms.config['assets.max_asset_size'].to_i end limit_bytes = limit_mb.megabytes return if asset.blob.byte_size.between?(1, limit_bytes) errors.add(:asset, :wrong_size_error, limit_mb: limit_mb) end |
#attached_to?(page) ⇒ Boolean
155 156 157 |
# File 'app/models/asset.rb', line 155 def attached_to?(page) pages.include?(page) end |
#basename ⇒ Object
137 138 139 |
# File 'app/models/asset.rb', line 137 def basename File.basename(filename, '.*') if filename end |
#byte_size ⇒ Object
80 81 82 83 84 |
# File 'app/models/asset.rb', line 80 def byte_size return asset.blob.byte_size if asset.attached? self[:asset_file_size] end |
#content_type ⇒ Object
74 75 76 77 78 |
# File 'app/models/asset.rb', line 74 def content_type return asset.content_type if asset.attached? self[:asset_content_type] end |
#dimensions_known? ⇒ Boolean
221 222 223 |
# File 'app/models/asset.rb', line 221 def dimensions_known? original_dimensions.all?(&:positive?) end |
#extension(style_name = 'original') ⇒ Object
141 142 143 144 145 146 147 148 149 |
# File 'app/models/asset.rb', line 141 def extension(style_name = 'original') if style_name == 'original' original_extension elsif style = active_storage_styles[style_name.to_sym] style[:format] else original_extension end end |
#filename ⇒ Object
68 69 70 71 72 |
# File 'app/models/asset.rb', line 68 def filename return asset.filename.to_s if asset.attached? self[:asset_file_name] end |
#geometry(style_name = 'original') ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'app/models/asset.rb', line 163 def geometry(style_name = 'original') unless style?(style_name) raise TrustyCms::StyleError, "Requested style #{style_name} is not defined for this asset." end @geometry ||= {} begin @geometry[style_name] ||= if style_name.to_s == 'original' original_geometry else style = active_storage_styles[style_name.to_sym] original_geometry.transformed_by(style[:geometry]) # this can return dimensions for fully specified style sizes but not for relative sizes when there are no original dimensions end rescue TrustyCms::TransformationError => e Rails.logger.warn "geometry transformation error: #{e}" original_geometry # returns a blank geometry if the real geometry cannot be calculated end end |
#height(style_name = 'original') ⇒ Object
205 206 207 |
# File 'app/models/asset.rb', line 205 def height(style_name = 'original') geometry(style_name).height.to_i end |
#horizontal?(style_name = 'original') ⇒ Boolean
217 218 219 |
# File 'app/models/asset.rb', line 217 def horizontal?(style_name = 'original') geometry(style_name).horizontal? end |
#orientation(style_name = 'original') ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'app/models/asset.rb', line 188 def orientation(style_name = 'original') a = aspect(style_name) if a == nil? 'unknown' elsif a < 1.0 'vertical' elsif a > 1.0 'horizontal' else 'square' end end |
#original_extension ⇒ Object
151 152 153 |
# File 'app/models/asset.rb', line 151 def original_extension return filename.split('.').last.downcase if filename end |
#original_geometry ⇒ Object
159 160 161 |
# File 'app/models/asset.rb', line 159 def original_geometry @original_geometry ||= TrustyCms::Geometry.new(*original_dimensions) end |
#public_url(style_name = 'normal') ⇒ Object
98 99 100 101 102 103 104 105 106 107 |
# File 'app/models/asset.rb', line 98 def public_url(style_name = 'normal') if style_name.to_s == 'original' || render_original(style_name) return rewrite_cloud_url(asset.url) end variant = asset_variant(style_name.to_s) return rewrite_cloud_url(variant.processed.url) if variant rewrite_cloud_url(asset.url) end |
#render_original(_style_name) ⇒ Object
113 114 115 116 117 118 |
# File 'app/models/asset.rb', line 113 def render_original(_style_name) return false unless asset.attached? prefix = TrustyCms::Config['assets.storage.prefix'].presence prefix ? asset.key.start_with?(prefix) : asset.key.include?('/') end |
#square?(style_name = 'original') ⇒ Boolean
209 210 211 |
# File 'app/models/asset.rb', line 209 def square?(style_name = 'original') geometry(style_name).square? end |
#style?(style_name = 'original') ⇒ Boolean
133 134 135 |
# File 'app/models/asset.rb', line 133 def style?(style_name = 'original') style_name == 'original' || active_storage_styles.keys.include?(style_name.to_sym) end |
#thumbnail(style_name = 'normal') ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'app/models/asset.rb', line 89 def thumbnail(style_name = 'normal') return rewrite_cloud_url(asset.url) if asset.attached? && content_type == 'application/pdf' variant = asset_variant(style_name.to_s) return rewrite_cloud_url(variant.processed.url) if variant asset_type.icon(style_name.to_s) end |
#vertical?(style_name = 'original') ⇒ Boolean
213 214 215 |
# File 'app/models/asset.rb', line 213 def vertical?(style_name = 'original') geometry(style_name).vertical? end |
#width(style_name = 'original') ⇒ Object
201 202 203 |
# File 'app/models/asset.rb', line 201 def width(style_name = 'original') geometry(style_name).width.to_i end |