forked from solidusio/solidus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request solidusio#3237 from nebulab/elia/attachment-adapters
Attachment adapters
- Loading branch information
Showing
8 changed files
with
106 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# frozen_string_literal: true | ||
|
||
module Spree::Image::PaperclipAttachment | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
validate :no_attachment_errors | ||
|
||
has_attached_file :attachment, | ||
styles: { mini: '48x48>', small: '100x100>', product: '240x240>', large: '600x600>' }, | ||
default_style: :product, | ||
default_url: 'noimage/:style.png', | ||
url: '/spree/products/:id/:style/:basename.:extension', | ||
path: ':rails_root/public/spree/products/:id/:style/:basename.:extension', | ||
convert_options: { all: '-strip -auto-orient -colorspace sRGB' } | ||
validates_attachment :attachment, | ||
presence: true, | ||
content_type: { content_type: %w[image/jpeg image/jpg image/png image/gif] } | ||
|
||
# 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, if: :valid? | ||
end | ||
|
||
def url(size) | ||
attachment.url(size) | ||
end | ||
|
||
def filename | ||
attachment_file_name | ||
end | ||
|
||
def attachment_present? | ||
attachment.present? | ||
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 interim 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
module Spree::Taxon::PaperclipAttachment | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
has_attached_file :icon, | ||
styles: { mini: '32x32>', normal: '128x128>' }, | ||
default_style: :mini, | ||
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] } | ||
end | ||
|
||
def icon_present? | ||
icon.present? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters