Skip to content

Commit

Permalink
add attributes to stores (content_type, height, width, etc), return u…
Browse files Browse the repository at this point in the history
…seful paperclip error messages, fix for issue spree-contrib#155
  • Loading branch information
W0lfbane committed Sep 6, 2016
1 parent 074d1f4 commit d35675d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
39 changes: 31 additions & 8 deletions app/models/spree/store_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
module Spree
Store.class_eval do
# 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_and_belongs_to_many :products, join_table: 'spree_products_stores'
has_many :taxonomies
has_many :orders
Expand All @@ -11,17 +15,36 @@ 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'

validate :no_logo_errors

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' }
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' }
validates_attachment :logo,
presence: true,
content_type: { content_type: %w(image/jpeg image/jpg image/png image/gif) }

if respond_to? :logo_file_name
validates_attachment_file_name :logo, matches: [/png\Z/i, /jpe?g\Z/i]
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
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

0 comments on commit d35675d

Please sign in to comment.