Sha256: 3fbd198d410a85a110fc2909ae69f52984d3f3704652202145442e76484ff1a4

Contents?: true

Size: 1.48 KB

Versions: 18

Compression:

Stored size: 1.48 KB

Contents

class Apiv1::ProductsMachine
  attr_accessor :params
  def initialize(params)
    @params = params
  end
  def products
    @products ||= _filter_pipeline.call Apiv1::Product
  end
  def meta_hash
    {
      page: _page,
      per: _per_page,
      count: _product_total
    }
  end
  private
  def _product_total
    @elastic_query.try(:count) || Apiv1::Product.count
  end
  def _filter_pipeline
    _possible_query_search >> _paginate >> _unify_type >> _process_taxons >> _process_ordering
  end
  def _possible_query_search
    lambda do |product|
      if _query.present?
        _cached_elasticsearch_product_query product
      else
        product
      end
    end
  end
  def _cached_elasticsearch_product_query(product)
    @elastic_query ||= product.search _query
  end
  def _paginate
    -> (t) { t.page(_page).per(_per_page) }
  end
  def _process_ordering
    case _order
    when "showcase"
      -> (product) { product.order_by_showcase }
    else
      -> (product) { product.order_by_created_at }
    end
  end
  def _process_taxons
    lambda do |product|
      if _taxon_ids.present?
        @taxon_query ||= product.involving_taxon_ids(_taxon_ids)
      else
        product
      end
    end
  end
  def _unify_type
    -> (t) { t.respond_to?(:records) ? t.records.load : t }
  end
  def _query
    params[:query]
  end
  def _taxon_ids
    params[:taxons]
  end
  def _page
    params[:page] || 0
  end
  def _per_page
    params[:per] || 15
  end
  def _order
    params[:order]
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
cloudrider-0.3.0 generica/app/models/apiv1/products_machine.rb
cloudrider-0.2.18 generica/app/models/apiv1/products_machine.rb
cloudrider-0.2.15 generica/app/models/apiv1/products_machine.rb
cloudrider-0.2.14 generica/app/models/apiv1/products_machine.rb
cloudrider-0.2.12 generica/app/models/apiv1/products_machine.rb
cloudrider-0.2.11 generica/app/models/apiv1/products_machine.rb
cloudrider-0.2.10 generica/app/models/apiv1/products_machine.rb
cloudrider-0.2.9 generica/app/models/apiv1/products_machine.rb
cloudrider-0.2.8 generica/app/models/apiv1/products_machine.rb
cloudrider-0.2.7 generica/app/models/apiv1/products_machine.rb
cloudrider-0.2.6 generica/app/models/apiv1/products_machine.rb
cloudrider-0.2.5 generica/app/models/apiv1/products_machine.rb
cloudrider-0.2.4 generica/app/models/apiv1/products_machine.rb
cloudrider-0.2.3 generica/app/models/apiv1/products_machine.rb
cloudrider-0.2.2 generica/app/models/apiv1/products_machine.rb
cloudrider-0.2.1 generica/app/models/apiv1/products_machine.rb
cloudrider-0.2.0 generica/app/models/apiv1/products_machine.rb
cloudrider-0.1.3 generica/app/models/apiv1/products_machine.rb