Skip to content

Commit

Permalink
Merge pull request solidusio#4062 from cpfergus1/image-size-config
Browse files Browse the repository at this point in the history
Add preferences to configure product and taxon images style
  • Loading branch information
kennyadsl authored Jun 10, 2021
2 parents f6ce20f + b1fa1b8 commit cc56661
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 14 deletions.
9 changes: 2 additions & 7 deletions core/app/models/spree/image/active_storage_attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,8 @@ module Spree::Image::ActiveStorageAttachment
validate :supported_content_type

has_attachment :attachment,
styles: {
mini: '48x48>',
small: '400x400>',
product: '680x680>',
large: '1200x1200>'
},
default_style: :product
styles: Spree::Config.product_image_styles,
default_style: Spree::Config.product_image_style_default

def supported_content_type
unless attachment.content_type.in?(Spree::Config.allowed_image_mime_types)
Expand Down
4 changes: 2 additions & 2 deletions core/app/models/spree/image/paperclip_attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ module Spree::Image::PaperclipAttachment
validate :no_attachment_errors

has_attached_file :attachment,
styles: { mini: '48x48>', small: '400x400>', product: '680x680>', large: '1200x1200>' },
default_style: :product,
styles: Spree::Config.product_image_styles,
default_style: Spree::Config.product_image_style_default,
default_url: 'noimage/:style.png',
url: '/spree/products/:id/:style/:basename.:extension',
path: ':rails_root/public/spree/products/:id/:style/:basename.:extension',
Expand Down
4 changes: 2 additions & 2 deletions core/app/models/spree/taxon/active_storage_attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ module Spree::Taxon::ActiveStorageAttachment

included do
has_attachment :icon,
styles: { mini: '32x32>', normal: '128x128>' },
default_style: :mini
styles: Spree::Config.taxon_image_styles,
default_style: Spree::Config.taxon_image_style_default
validate :icon_is_an_image
end
end
6 changes: 3 additions & 3 deletions core/app/models/spree/taxon/paperclip_attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ module Spree::Taxon::PaperclipAttachment

included do
has_attached_file :icon,
styles: { mini: '32x32>', normal: '128x128>' },
default_style: :mini,
styles: Spree::Config.taxon_image_styles,
default_style: Spree::Config.taxon_image_style_default,
url: '/spree/taxons/:id/:style/:basename.:extension',
path: ':rails_root/public/spree/taxons/:id/:style/:basename.:extension',
default_url: '/assets/default_taxon.png'

validates_attachment :icon,
content_type: { content_type: %w[image/jpg image/jpeg image/png image/gif] }
content_type: { content_type: Spree::Config.allowed_image_mime_types }
end

def icon_present?
Expand Down
40 changes: 40 additions & 0 deletions core/lib/spree/app_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,46 @@ def payment_canceller
# @return [Array]
class_name_attribute :allowed_image_mime_types, default: %w(image/jpeg image/jpg image/png image/gif).freeze

# @!attribute [rw] product_image_style_default
#
# Defines which style to default to when style is not provided
# :product is the default.
#
# @return [Symbol]
class_name_attribute :product_image_style_default, default: :product

# @!attribute [rw] product_image_styles
#
# Defines image styles/sizes hash for styles
# `{ mini: '48x48>',
# small: '400x400>',
# product: '680x680>',
# large: '1200x1200>' } is the default.
#
# @return [Hash]
class_name_attribute :product_image_styles, default: { mini: '48x48>',
small: '400x400>',
product: '680x680>',
large: '1200x1200>' }
# @!attribute [rw] taxon_image_style_default
#
# Defines which style to default to when style is not provided
# :mini is the default.
#
# @return [Symbol]
class_name_attribute :taxon_image_style_default, default: :mini

# @!attribute [rw] taxon_styles
#
# Defines taxon styles/sizes hash for styles
# `{ mini: '48x48>',
# small: '400x400>',
# product: '680x680>',
# large: '1200x1200>' } is the default.
#
# @return [Hash]
class_name_attribute :taxon_image_styles, default: { mini: '32x32>', normal: '128x128>' }

# Allows switching attachment library for Taxon
#
# `Spree::Taxon::ActiveStorageAttachment`
Expand Down

0 comments on commit cc56661

Please sign in to comment.