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
- #pdf? ⇒ Boolean
- #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
310 311 312 |
# File 'app/models/asset.rb', line 310 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
321 322 323 |
# File 'app/models/asset.rb', line 321 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)
326 327 328 329 330 |
# File 'app/models/asset.rb', line 326 def self.eigenclass class << self self; end end |
.find_all_by_asset_types(asset_types, *args) ⇒ Object
searching and pagination moved to the controller
306 307 308 |
# File 'app/models/asset.rb', line 306 def find_all_by_asset_types(asset_types, *args) with_asset_types(asset_types) { where *args } end |
.known_types ⇒ Object
300 301 302 |
# File 'app/models/asset.rb', line 300 def known_types AssetType.known_types end |
.ransackable_attributes(auth_object = nil) ⇒ Object
114 115 116 |
# File 'app/models/asset.rb', line 114 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
337 338 339 |
# File 'app/models/asset.rb', line 337 def self.thumbnail_names thumbnail_sizes.keys end |
.thumbnail_options ⇒ Object
this is a convenience for image-pickers
342 343 344 345 346 347 348 349 350 351 |
# File 'app/models/asset.rb', line 342 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
333 334 335 |
# File 'app/models/asset.rb', line 333 def self.thumbnail_sizes AssetType.find(:image).active_storage_styles end |
.with_asset_types(asset_types, &block) ⇒ Object
314 315 316 317 |
# File 'app/models/asset.rb', line 314 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
189 190 191 |
# File 'app/models/asset.rb', line 189 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
125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'app/models/asset.rb', line 125 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
160 161 162 |
# File 'app/models/asset.rb', line 160 def attached_to?(page) pages.include?(page) end |
#basename ⇒ Object
142 143 144 |
# File 'app/models/asset.rb', line 142 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
226 227 228 |
# File 'app/models/asset.rb', line 226 def dimensions_known? original_dimensions.all?(&:positive?) end |
#extension(style_name = 'original') ⇒ Object
146 147 148 149 150 151 152 153 154 |
# File 'app/models/asset.rb', line 146 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
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'app/models/asset.rb', line 168 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
210 211 212 |
# File 'app/models/asset.rb', line 210 def height(style_name = 'original') geometry(style_name).height.to_i end |
#horizontal?(style_name = 'original') ⇒ Boolean
222 223 224 |
# File 'app/models/asset.rb', line 222 def horizontal?(style_name = 'original') geometry(style_name).horizontal? end |
#orientation(style_name = 'original') ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'app/models/asset.rb', line 193 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
156 157 158 |
# File 'app/models/asset.rb', line 156 def original_extension return filename.split('.').last.downcase if filename end |
#original_geometry ⇒ Object
164 165 166 |
# File 'app/models/asset.rb', line 164 def original_geometry @original_geometry ||= TrustyCms::Geometry.new(*original_dimensions) end |
#pdf? ⇒ Boolean
89 90 91 |
# File 'app/models/asset.rb', line 89 def pdf? content_type == 'application/pdf' end |
#public_url(style_name = 'normal') ⇒ Object
103 104 105 106 107 108 109 110 111 112 |
# File 'app/models/asset.rb', line 103 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
118 119 120 121 122 123 |
# File 'app/models/asset.rb', line 118 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
214 215 216 |
# File 'app/models/asset.rb', line 214 def square?(style_name = 'original') geometry(style_name).square? end |
#style?(style_name = 'original') ⇒ Boolean
138 139 140 |
# File 'app/models/asset.rb', line 138 def style?(style_name = 'original') style_name == 'original' || active_storage_styles.keys.include?(style_name.to_sym) end |
#thumbnail(style_name = 'normal') ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'app/models/asset.rb', line 93 def thumbnail(style_name = 'normal') if asset.attached? && pdf? return style_name.to_s == 'thumbnail' ? asset_type.icon('thumbnail') : rewrite_cloud_url(asset.url) end 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
218 219 220 |
# File 'app/models/asset.rb', line 218 def vertical?(style_name = 'original') geometry(style_name).vertical? end |
#width(style_name = 'original') ⇒ Object
206 207 208 |
# File 'app/models/asset.rb', line 206 def width(style_name = 'original') geometry(style_name).width.to_i end |