Class: Spree::Asset

Inherits:
Object
  • Object
show all
Includes:
Support::ActiveStorage, ImageMethods, Metadata, Metafields
Defined in:
app/models/spree/asset.rb,
app/models/spree/asset/support/active_storage.rb

Direct Known Subclasses

Image

Defined Under Namespace

Modules: Support

Constant Summary collapse

EXTERNAL_URL_METAFIELD_KEY =
'external.url'
MEDIA_TYPES =
%w[image video external_video].freeze
WEBP_SAVER_OPTIONS =
{
  strip: true,
  quality: 75,
  lossless: false,
  alpha_q: 85,
  reduction_effort: 6,
  smart_subsample: true
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Metadata

#metadata, #metadata=, #public_metadata=

Methods included from ImageMethods

#generate_url, #original_url

Class Method Details

.stylesObject

Deprecated.

Will be removed in Spree 6

Legacy styles support (was in Spree::Image::Configuration::ActiveStorage)



13
14
15
16
17
# File 'app/models/spree/asset.rb', line 13

def self.styles
  @styles ||= Spree::Config.product_image_variant_sizes.transform_values do |dimensions|
    "#{dimensions[0]}x#{dimensions[1]}>"
  end
end

Instance Method Details

#default_styleObject



19
20
21
# File 'app/models/spree/asset.rb', line 19

def default_style
  :small
end

#event_prefixObject



122
123
124
# File 'app/models/spree/asset.rb', line 122

def event_prefix
  'media'
end

#external_urlObject



110
111
112
# File 'app/models/spree/asset.rb', line 110

def external_url
  get_metafield(EXTERNAL_URL_METAFIELD_KEY)&.value
end

#external_url=(url) ⇒ Object



114
115
116
# File 'app/models/spree/asset.rb', line 114

def external_url=(url)
  set_metafield(EXTERNAL_URL_METAFIELD_KEY, url.strip)
end

#focal_pointObject



94
95
96
97
98
# File 'app/models/spree/asset.rb', line 94

def focal_point
  return nil if focal_point_x.nil? || focal_point_y.nil?

  { x: focal_point_x, y: focal_point_y }
end

#focal_point=(point) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'app/models/spree/asset.rb', line 100

def focal_point=(point)
  if point.nil?
    self.focal_point_x = nil
    self.focal_point_y = nil
  else
    self.focal_point_x = point[:x]
    self.focal_point_y = point[:y]
  end
end

#productObject



87
88
89
90
91
92
# File 'app/models/spree/asset.rb', line 87

def product
  @product ||= case viewable_type
               when 'Spree::Variant' then viewable&.product
               when 'Spree::Product' then viewable
               end
end

#skip_import?Boolean

Returns:



118
119
120
# File 'app/models/spree/asset.rb', line 118

def skip_import?
  false
end

#stylesObject

Deprecated.


127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/models/spree/asset.rb', line 127

def styles
  Spree::Deprecation.warn("Asset#styles is deprecated and will be removed in Spree 6.0. Please use active storage variants with cdn_image_url")

  self.class.styles.map do |_, size|
    width, height = size.chop.split('x').map(&:to_i)

    {
      url: generate_url(size: size),
      size: size,
      width: width,
      height: height
    }
  end
end