Sha256: 8162c137771c1eaa42e8b31994967712eb3cdfee4ddd202eb307259d928e1373

Contents?: true

Size: 1.66 KB

Versions: 21

Compression:

Stored size: 1.66 KB

Contents

require 'will_paginate/array'

module Dugway
  module Drops
    class ProductsDrop < BaseDrop
      def all
        sort_and_paginate source
      end

      def current
        sort_and_paginate begin
          if artist.present?
            dropify store.artist_products(artist)
          elsif category.present?
            dropify store.category_products(category)
          elsif search_terms.present?
            dropify store.search_products(search_terms)
          else
            source
          end
        end
      end

      def on_sale
        sort_and_paginate source.select { |p| p['on_sale'] }
      end

      private

      def order
        begin case @context['internal']['order']
          when 'newest', 'date'
            'date'
          # We don't pass these in the API, so fake it
          when 'sales', 'sells', 'views'
            'shuffle'
          else
            'position'
          end
        end
      end

      def sort_and_paginate(array)
        if order == 'shuffle'
          array.shuffle!
        elsif order == 'date'
          array.sort! { |a,b| b['id'] <=> a['id'] }
        else
          array.sort_by! { |p| p[order] }
        end

        array.paginate({
          :page => (@context.registers[:params][:page] || 1).to_i,
          :per_page => @context['internal']['per_page']
        })
      end

      def artist
        @context.registers[:artist]['permalink'] rescue nil
      end

      def category
        @context.registers[:category]['permalink'] rescue nil
      end

      def search_terms
        params[:search]
      end  

      def dropify(products)
        products.map { |p| ProductDrop.new(p) }
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
dugway-0.8.1 lib/dugway/liquid/drops/products_drop.rb
dugway-0.8.0 lib/dugway/liquid/drops/products_drop.rb
dugway-0.7.1 lib/dugway/liquid/drops/products_drop.rb
dugway-0.7.0 lib/dugway/liquid/drops/products_drop.rb
dugway-0.6.7 lib/dugway/liquid/drops/products_drop.rb
dugway-0.6.6 lib/dugway/liquid/drops/products_drop.rb
dugway-0.6.5 lib/dugway/liquid/drops/products_drop.rb
dugway-0.6.4 lib/dugway/liquid/drops/products_drop.rb
dugway-0.6.3 lib/dugway/liquid/drops/products_drop.rb
dugway-0.6.2 lib/dugway/liquid/drops/products_drop.rb
dugway-0.6.1 lib/dugway/liquid/drops/products_drop.rb
dugway-0.6.0 lib/dugway/liquid/drops/products_drop.rb
dugway-0.5.9 lib/dugway/liquid/drops/products_drop.rb
dugway-0.5.8 lib/dugway/liquid/drops/products_drop.rb
dugway-0.5.7 lib/dugway/liquid/drops/products_drop.rb
dugway-0.5.6 lib/dugway/liquid/drops/products_drop.rb
dugway-0.5.4 lib/dugway/liquid/drops/products_drop.rb
dugway-0.5.3 lib/dugway/liquid/drops/products_drop.rb
dugway-0.5.2 lib/dugway/liquid/drops/products_drop.rb
dugway-0.5.1 lib/dugway/liquid/drops/products_drop.rb