Skip to content
This repository has been archived by the owner on Aug 13, 2021. It is now read-only.

Update to image processing for Store logos #156

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions app/controllers/spree/admin/products_controller_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
Spree::Admin::ProductsController.class_eval do
update.before :set_stores
before_action :find_stores, only: [:update]

def update
private

def set_stores
@product.store_ids = nil unless params[:product].key? :store_ids
end

def find_stores
store_ids = params[:product][:store_ids]
if store_ids.present?
params[:product][:store_ids] = store_ids.split(',')
end
super
end

private

def set_stores
@product.store_ids = nil unless params[:product].key? :store_ids
end
end
end
48 changes: 36 additions & 12 deletions app/models/spree/store_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,41 @@ module Spree
has_many :shipping_methods, through: :store_shipping_methods

has_and_belongs_to_many :promotion_rules, class_name: 'Spree::Promotion::Rules::Store', join_table: 'spree_promotion_rules_stores', association_foreign_key: 'promotion_rule_id'

has_attached_file :logo,
styles: { mini: '48x48>', small: '100x100>', medium: '250x250>' },
default_style: :medium,
url: 'stores/:id/:style/:basename.:extension',
path: 'stores/:id/:style/:basename.:extension',
convert_options: { all: '-strip -auto-orient' }

if respond_to? :logo_file_name
validates_attachment_file_name :logo, matches: [/png\Z/i, /jpe?g\Z/i]

if ActiveRecord::Base.connection.table_exists?(:spree_stores) && ActiveRecord::Base.connection.column_exists?(:spree_stores, :logo_content_type)
# save the w,h of the original image (from which others can be calculated)
# we need to look at the write-queue for images which have not been saved yet
before_save :find_dimensions, if: :logo_updated_at_changed?

has_attached_file :logo,
styles: { mini: '48x48>', small: '100x100>', logo: '250x250>', large: '600x600>' },
default_style: :logo,
url: '/spree/logos/:id/:style/:basename.:extension',
path: ':rails_root/public/spree/logos/:id/:style/:basename.:extension',
convert_options: { all: '-strip -auto-orient -colorspace sRGB' }

validate :no_logo_errors
validates_attachment :logo,
content_type: { content_type: %w(image/jpeg image/jpg image/png image/gif) }

def find_dimensions
temporary = logo.queued_for_write[:original]
filename = temporary.path unless temporary.nil?
filename = logo.path if filename.blank?
geometry = Paperclip::Geometry.from_file(filename)
self.logo_width = geometry.width
self.logo_height = geometry.height
end

# if there are errors from the plugin, then add a more meaningful message
def no_logo_errors
unless logo.errors.empty?
# uncomment this to get rid of the less-than-useful interim messages
# errors.clear
errors.add :logo, "Paperclip returned errors for file '#{logo_file_name}' - check ImageMagick installation or image source file."
false
end
end
end

end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AddImageAttributesToSpreeStores < ActiveRecord::Migration
def change
add_column :spree_stores, :logo_content_type, :string
add_column :spree_stores, :logo_file_size, :string
add_column :spree_stores, :logo_width, :string
add_column :spree_stores, :logo_height, :string
add_column :spree_stores, :logo_updated_at, :datetime
end
end