Sha256: 677095447b1af3124c3f44dea379a8d2c52421a79c923c8dc4f001828135abd0

Contents?: true

Size: 1.06 KB

Versions: 50

Compression:

Stored size: 1.06 KB

Contents

module Spree
  module Scopes
    # This module is extended by ProductScope
    module Dynamic
      module_function

      # Sample dynamic scope generating from set of products
      # generates 0 or (2..scope_limit) scopes for prices, based
      # on number of products (uses Math.log, to guess number of scopes)
      def price_scopes_for(products, scope_limit=5)
        scopes = []

        # Price based scopes
        all_prices = products.map(&:price).sort

        ranges = [Math.log(products.length).floor, scope_limit].max

        if ranges >= 2
          l = all_prices.length / ranges
          scopes << ProductScope.new({:name => "master_price_lte", :arguments => [all_prices[l]] })

          (ranges - 2).times do |x|
            scopes << ProductScope.new({:name => "price_between",
                                        :arguments => [ all_prices[l*(x+1)+1], all_prices[l*(x+2)] ] })
          end
          scopes << ProductScope.new({:name => "master_price_gte", :arguments => [all_prices[l*(ranges-1)+1]] })
        end

        scopes
      end
    end
  end
end

Version data entries

50 entries across 50 versions & 3 rubygems

Version Path
spree_frontend-2.0.13 lib/spree/scopes/dynamic.rb
spree_frontend-2.0.12 lib/spree/scopes/dynamic.rb
spree_frontend-2.0.11 lib/spree/scopes/dynamic.rb
spree_frontend-2.0.10 lib/spree/scopes/dynamic.rb
spree_frontend-2.0.9 lib/spree/scopes/dynamic.rb
spree_frontend-2.0.8 lib/spree/scopes/dynamic.rb
spree_frontend-2.0.7 lib/spree/scopes/dynamic.rb
spree_frontend-2.0.6 lib/spree/scopes/dynamic.rb
spree_frontend-2.1.1 lib/spree/scopes/dynamic.rb
spree_frontend-2.0.5 lib/spree/scopes/dynamic.rb
spree_frontend-2.1.0 lib/spree/scopes/dynamic.rb
spree_frontend-2.0.4 lib/spree/scopes/dynamic.rb
spree_frontend-2.0.3 lib/spree/scopes/dynamic.rb
spree_core-1.2.5 lib/spree/scopes/dynamic.rb
spree_frontend-2.0.2 lib/spree/scopes/dynamic.rb
spree_frontend-2.0.1 lib/spree/scopes/dynamic.rb
spree_frontend-2.0.0 lib/spree/scopes/dynamic.rb
spree_frontend-2.0.0.rc1 lib/spree/scopes/dynamic.rb
spree_core-1.1.6 lib/spree/scopes/dynamic.rb
spree_core-1.1.5 lib/spree/scopes/dynamic.rb