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 >> _process_finishing end def _possible_query_search lambda do |product| if _query.present? _cached_elasticsearch_product_query product elsif _user.present? _user.products 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_finishing if _user.blank? lambda(&:still_unfinished) else -> (product) { product } end end def _process_taxons lambda do |product| if _taxon_ids.present? product.intersection_of_taxon_ids(_taxon_ids) else product end end end def _unify_type -> (t) { t.respond_to?(:records) ? t.records.load : t } end def _user @user ||= Admin::User.find_by_id params[:user_id] end def _query params[:query] end def _taxon_ids return params[:taxons].flatten if params[:taxons].respond_to? :flatten params[:taxons].to_s.split(/\s*,\s*/) end def _page params[:page] || 0 end def _per_page params[:per] || 15 end def _order params[:order] end end