Skip to content

Commit

Permalink
Index friendly where
Browse files Browse the repository at this point in the history
  • Loading branch information
Willianvdv committed Jan 20, 2014
1 parent 7576b85 commit 1f0f34a
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions app/controllers/spree/taxons_controller_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ def initialize value, property, product_collection
end

def number_of_products
# todo: there must be a beter way to do this
value = ActiveRecord::Base::sanitize(@value)
filter = "data -> '#{@property.name}' = #{value}"
@number_of_products ||= @product_collection.limit(nil).where(filter).count
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

Expand All @@ -55,8 +57,8 @@ def filter
filters_per_filterable = []
property = filterable.property
@params[property.name].each do |value|
value = ActiveRecord::Base::sanitize(value)
filters_per_filterable << "data -> '#{property.name}' = #{value}"
hstore_filter = ActiveRecord::Base::sanitize "#{property.name}=>\"#{value}\""
filters_per_filterable << "data @> #{hstore_filter}::hstore"
end

filters << filters_per_filterable.join(' OR ')
Expand Down

0 comments on commit 1f0f34a

Please sign in to comment.