-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add concept of Image gallery #625
Closed
Closed
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
10fdae8
Add the Gallery interface
5066766
Create a VariantAssociation Gallery
864cd19
Allow a variant's gallery to be configurable
ba98d93
Add image_or_default
b4c669a
Add spree_image_tag
afa70c5
Mark Variant#display_image as deprecated
894ff88
Pull Spree::Variant#display_image out of frontend
70c39c2
Pull Spree::Variant#display_image out of backend
91093dc
Add a Spree::Gallery::ProductVariantAssociation
cf31b81
Mark Spree::Product#display_image as deprecated
1e915f8
Remove all calls to Spree::Product.display_image
8860499
Add a shared example to test behaves like gallery
90963e4
Use preload_params on product gallery
8b6ea1a
Remove duplication in :includes
2959777
Add preload_params to the variant gallery
52606f7
Switch to :include the variant gallery preload
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -50,7 +50,11 @@ def load_stock_management_data | |
@stock_item_stock_locations = params[:stock_location_id].present? ? @stock_locations.where(id: params[:stock_location_id]) : @stock_locations | ||
@variant_display_attributes = self.class.variant_display_attributes | ||
@variants = Spree::Config.variant_search_class.new(params[:variant_search_term], scope: variant_scope).results | ||
@variants = @variants.includes(:images, stock_items: :stock_location, product: :variant_images) | ||
@variants = @variants.includes( | ||
Spree::Config.variant_gallery_class.preload_params, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer some simpler indentation rather than sticking everything far to the right: @variants = @variants.includes(
Spree::Config.variant_gallery_class.preload_params,
stock_items: :stock_location,
product: Spree::Config.product_gallery_class.preload_params
) |
||
stock_items: :stock_location, | ||
product: Spree::Config.product_gallery_class.preload_params | ||
) | ||
@variants = @variants.includes(option_values: :option_type) | ||
@variants = @variants.order(id: :desc).page(params[:page]).per(params[:per_page] || Spree::Config[:orders_per_page]) | ||
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
2 changes: 1 addition & 1 deletion
2
backend/app/views/spree/admin/orders/_shipment_manifest.html.erb
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
2 changes: 1 addition & 1 deletion
2
backend/app/views/spree/admin/orders/confirm/_shipment_manifest.html.erb
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
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,32 @@ | ||
module Spree | ||
module ImagesHelper | ||
# Return the provided image if it is not nil, | ||
# otherwise return a default {Spree::Image}. | ||
# | ||
# This is useful when displaying images for a resource when it exists, | ||
# or falling back to the Paperclip default image when none is provided. | ||
# | ||
# @param image [Spree::Image, nil] The image to return, or nil if the default | ||
# should be used. | ||
# | ||
# @return [Spree::Image] the provided image if it exists, or a default | ||
# image otherwise | ||
def image_or_default(image) | ||
image || Spree::Image.new | ||
end | ||
|
||
# Display an image_tag for the given spree image, if one exists, for the | ||
# provided style, or a image_tag for the default image if no image is provided. | ||
# | ||
# @param image [Spree::Image, nil] The image to display, if provided, or nil | ||
# if the default image should be displayed. | ||
# | ||
# @param style [symbol] The paperclip {Spree::Image} style to display the image_tag | ||
# for | ||
# | ||
# @return [String] A string of the image_tag built from the provided image | ||
def spree_image_tag(image, style, options={}) | ||
image_tag image_or_default(image).attachment(style), options | ||
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,51 @@ | ||
module Spree | ||
module Gallery | ||
|
||
# A gallery represents a collection of images. | ||
# | ||
# @abstract base class for all galleries. Implement your own | ||
# to define how images should be associated to objects in your | ||
# store | ||
class Base | ||
|
||
# A list of all images associated with this gallery | ||
# | ||
# @return [Enumerable<Spree::Image>] All images associated to the gallery | ||
def images; raise NotImplementedError end | ||
|
||
# The "primary" {Spree::Image} associated with the gallery | ||
# | ||
# Typically the most appropriate single image to display representing | ||
# the gallery, but will not follow the fallback rules of {#best_image} | ||
# | ||
# @return [Spree::Image, nil] if primary image for the gallery exists or | ||
# nil otherwise | ||
# | ||
# @abstract | ||
def primary_image; raise NotImplementedError end | ||
|
||
# The "best" {Spree::Image} associated with this gallery. | ||
# | ||
# Will attempt to fall back to other sources and logic to find a picture | ||
# if the {#primary_image} does not exist | ||
# | ||
# @return [Spree::Image, nil] if an image can be found to represent the gallery, | ||
# following fallback rules, and nli otherwise | ||
# | ||
# @abstract | ||
def best_image; raise NotImplementedError end | ||
|
||
# Return an ActiveRecord-compatible Array of objects to preload | ||
# | ||
# An unfortunate method to allow the application to minimize queries while | ||
# allowing different Gallery implementations to maintain their own | ||
# database structures | ||
# | ||
# @return [Array] an ActiveRecord::QueryMethods#includes compatible array | ||
def self.preload_params | ||
[] | ||
end | ||
|
||
end | ||
end | ||
end |
46 changes: 46 additions & 0 deletions
46
core/app/models/spree/gallery/product_variant_association.rb
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,46 @@ | ||
module Spree | ||
# Uses the ActiveRecord association for a Prodcut's Variant's Images | ||
module Gallery | ||
class ProductVariantAssociation < Gallery::Base | ||
|
||
# Creates a gallery for the given product | ||
# @param [Spree::Product] product The product to create the gallery for | ||
def initialize(product) | ||
@product = product | ||
end | ||
|
||
# (see Spree::Gallery#images) | ||
def images | ||
product.variant_images | ||
end | ||
|
||
# (see Spree::Gallery#primary_image) | ||
def primary_image | ||
product.images.first || product.variant_images.first | ||
end | ||
|
||
# Returns the primary image for the gallery | ||
# | ||
# Does not follow any fallback rules | ||
# | ||
# (see Spree::Gallery#best_image) | ||
def best_image | ||
primary_image | ||
end | ||
|
||
# Return a list of image associations to preload a Spree::Product's images | ||
# | ||
# @return An array compatible with ActiveRecord :includes | ||
# for a Spree::Products images preload | ||
# | ||
# (see Spree::Galery#preload_params) | ||
def self.preload_params | ||
[:variant_images] | ||
end | ||
|
||
private | ||
attr_reader :product | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
module Spree | ||
# Uses the ActiveRecord association between Spree::Variant and | ||
# Spree::Image | ||
module Gallery | ||
class VariantAssociation < Gallery::Base | ||
|
||
# Creates a gallery for the given variant | ||
# @param [Spree::Variant] variant variant to build the gallery for | ||
def initialize(variant) | ||
@variant = variant | ||
end | ||
|
||
# (see Spree::Gallery#images) | ||
def images | ||
variant.images | ||
end | ||
|
||
# (see Spree::Gallery#primary_image) | ||
def primary_image | ||
variant.images.first | ||
end | ||
|
||
# Will fall back to the first image on the variant's product if | ||
# the variant has no associated images. | ||
# @return [Spree::Image, nil] The variant's primary image, if it exists, | ||
# or the variant's products first image, if it exists, nil otherwise | ||
def best_image | ||
primary_image || products_images.first | ||
end | ||
|
||
# Return a list of image associations to preload a Spree::Variant's images | ||
# | ||
# @return An array compatible with ActiveRecord :includes | ||
# for a Spree::Variant images preload | ||
# | ||
# (see Spree::Galery#preload_params) | ||
def self.preload_params | ||
[:images] | ||
end | ||
|
||
private | ||
attr_reader :variant | ||
|
||
def products_images | ||
variant.product.try!(:variant_images) || [] | ||
end | ||
|
||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The readability of this could be improved by putting one thing per line: