mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Bring image, asset and images_helper from spree_core
This commit is contained in:
18
app/helpers/spree/admin/images_helper.rb
Normal file
18
app/helpers/spree/admin/images_helper.rb
Normal file
@@ -0,0 +1,18 @@
|
||||
module Spree
|
||||
module Admin
|
||||
module ImagesHelper
|
||||
def options_text_for(image)
|
||||
if image.viewable.is_a?(Spree::Variant)
|
||||
if image.viewable.is_master?
|
||||
Spree.t(:all)
|
||||
else
|
||||
image.viewable.options_text
|
||||
end
|
||||
else
|
||||
Spree.t(:all)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
6
app/models/spree/asset.rb
Normal file
6
app/models/spree/asset.rb
Normal file
@@ -0,0 +1,6 @@
|
||||
module Spree
|
||||
class Asset < ActiveRecord::Base
|
||||
belongs_to :viewable, polymorphic: true, touch: true
|
||||
acts_as_list scope: :viewable
|
||||
end
|
||||
end
|
||||
50
app/models/spree/image.rb
Normal file
50
app/models/spree/image.rb
Normal file
@@ -0,0 +1,50 @@
|
||||
module Spree
|
||||
class Image < Asset
|
||||
validates_attachment_presence :attachment
|
||||
validate :no_attachment_errors
|
||||
|
||||
has_attached_file :attachment,
|
||||
styles: { mini: '48x48>', small: '100x100>', product: '240x240>', large: '600x600>' },
|
||||
default_style: :product,
|
||||
url: '/spree/products/:id/:style/:basename.:extension',
|
||||
path: ':rails_root/public/spree/products/:id/:style/:basename.:extension',
|
||||
convert_options: { all: '-strip -auto-orient -colorspace RGB' }
|
||||
|
||||
# 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
|
||||
after_post_process :find_dimensions
|
||||
|
||||
include Spree::Core::S3Support
|
||||
supports_s3 :attachment
|
||||
|
||||
Spree::Image.attachment_definitions[:attachment][:styles] = ActiveSupport::JSON.decode(Spree::Config[:attachment_styles]).symbolize_keys!
|
||||
Spree::Image.attachment_definitions[:attachment][:path] = Spree::Config[:attachment_path]
|
||||
Spree::Image.attachment_definitions[:attachment][:url] = Spree::Config[:attachment_url]
|
||||
Spree::Image.attachment_definitions[:attachment][:default_url] = Spree::Config[:attachment_default_url]
|
||||
Spree::Image.attachment_definitions[:attachment][:default_style] = Spree::Config[:attachment_default_style]
|
||||
|
||||
#used by admin products autocomplete
|
||||
def mini_url
|
||||
attachment.url(:mini, false)
|
||||
end
|
||||
|
||||
def find_dimensions
|
||||
temporary = attachment.queued_for_write[:original]
|
||||
filename = temporary.path unless temporary.nil?
|
||||
filename = attachment.path if filename.blank?
|
||||
geometry = Paperclip::Geometry.from_file(filename)
|
||||
self.attachment_width = geometry.width
|
||||
self.attachment_height = geometry.height
|
||||
end
|
||||
|
||||
# if there are errors from the plugin, then add a more meaningful message
|
||||
def no_attachment_errors
|
||||
unless attachment.errors.empty?
|
||||
# uncomment this to get rid of the less-than-useful interrim messages
|
||||
# errors.clear
|
||||
errors.add :attachment, "Paperclip returned errors for file '#{attachment_file_name}' - check ImageMagick installation or image source file."
|
||||
false
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
16
spec/models/asset_spec.rb
Normal file
16
spec/models/asset_spec.rb
Normal file
@@ -0,0 +1,16 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe Spree::Asset do
|
||||
describe "#viewable" do
|
||||
it "touches association" do
|
||||
product = create(:custom_product)
|
||||
asset = Spree::Asset.create! { |a| a.viewable = product.master }
|
||||
|
||||
product.update_column(:updated_at, 1.day.ago)
|
||||
|
||||
expect do
|
||||
asset.touch
|
||||
end.to change { product.reload.updated_at }
|
||||
end
|
||||
end
|
||||
end
|
||||
16
spec/models/spree/asset_spec.rb
Normal file
16
spec/models/spree/asset_spec.rb
Normal file
@@ -0,0 +1,16 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe Spree::Asset do
|
||||
describe "#viewable" do
|
||||
it "touches association" do
|
||||
product = create(:custom_product)
|
||||
asset = Spree::Asset.create! { |a| a.viewable = product.master }
|
||||
|
||||
product.update_column(:updated_at, 1.day.ago)
|
||||
|
||||
expect do
|
||||
asset.touch
|
||||
end.to change { product.reload.updated_at }
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user