-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3a9e380
commit 91a246b
Showing
4 changed files
with
73 additions
and
72 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module HStoreFilter | ||
class Filter | ||
attr_accessor :property | ||
|
||
def initialize property, product_collection | ||
@property = property | ||
@product_collection = product_collection | ||
end | ||
|
||
def options | ||
values.map do |value| | ||
FilterOption.new value, @property, @product_collection | ||
end | ||
end | ||
|
||
def values | ||
@values ||= @product_collection | ||
.joins(:product_properties) | ||
.where('spree_product_properties.property_id = ?', @property.id) | ||
.select('spree_product_properties.value') | ||
.distinct('spree_product_properties.value') | ||
.limit(nil) | ||
.reorder(nil) | ||
.pluck('spree_product_properties.value') | ||
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,20 @@ | ||
module HStoreFilter | ||
class FilterOption | ||
attr_accessor :value, :property | ||
|
||
def initialize value, property, product_collection | ||
@value = value | ||
@property = property | ||
@product_collection = product_collection | ||
end | ||
|
||
def number_of_products | ||
hstore_filter = ActiveRecord::Base::sanitize "#{@property.name}=>\"#{@value}\"" | ||
filter = "data @> #{hstore_filter}::hstore" | ||
@number_of_products ||= @product_collection | ||
.limit(nil) | ||
.where(filter) | ||
.count | ||
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,26 @@ | ||
module HStoreFilter | ||
class FilterScope | ||
def initialize params, filterables, product_collection | ||
@params = params | ||
@filterables = filterables | ||
@product_collection = product_collection | ||
end | ||
|
||
def filter | ||
filters = [] | ||
@filterables.each do |filterable| | ||
filters_per_filterable = [] | ||
property = filterable.property | ||
@params[property.name].each do |value| | ||
hstore_filter = ActiveRecord::Base::sanitize "#{property.name}=>\"#{value}\"" | ||
filters_per_filterable << "data @> #{hstore_filter}::hstore" | ||
end | ||
|
||
filters << filters_per_filterable.join(' OR ') | ||
end | ||
|
||
filter = filters.join(') AND (') | ||
filters.empty? ? @product_collection : @product_collection.where("(#{filter})") | ||
end | ||
end | ||
end |