Skip to content

Instantly share code, notes, and snippets.

@denisoster
Created December 19, 2017 16:38
Show Gist options
  • Save denisoster/20e41f2a488f083ed3eb2c6aa7d264f4 to your computer and use it in GitHub Desktop.
Save denisoster/20e41f2a488f083ed3eb2c6aa7d264f4 to your computer and use it in GitHub Desktop.
class FilterService
def initialize(params)
@products = Good.all
@models = Model.all.order(position: :asc)
@brands = Brand.all.order(position: :asc)
@types = TypesTechnology.all.order(position: :asc)
@type = params[:type] || nil
@brand = params[:brand] || nil
@model = params[:model] || false
@filter = params[:filter] || false
@min = params[:min] || false
@max = params[:max] || false
@sort = params[:sort] || ''
@page = params[:page] || 1
@per_page = params[:per_page] || 24
@return_products = []
@return_products_count = 0
@return_types = []
@return_brands = []
@return_models = []
@return_filters = {}
@return_min = 0
@return_max = 0
@return_page_count = 0
@return_seo_text = false
end
def call
self.seo
self.processing
self.processing_filter
self.processing_model
self.processing_brand
self.processing_type
self.price(@min, @max)
self.paginate(@page, @per_page)
@return_models = @models
@return_types = @types
@return_brands = @brands
return {
products: @products,
products_all: @products_all,
products_count: @return_products_count,
type: @return_types,
brand: @return_brands,
model: @return_models,
filter: @return_filters,
min: @return_min,
max: @return_max,
page_count: @return_page_count,
seo_text: @return_seo_text
}
end
def price(min = false, max = false)
if @products.exists?
@return_min = @products.select(:price).minimum(:price)
@return_max = @products.select(:price).maximum(:price)
end
if (not min) || (not max) || min.to_i > max.to_i || max==0
return
end
@products = @products.where(price: min..max)
end
def processing
@products = @products.where(
id: ModelsGood.where(
model_id: Model.where(
id: @model || Model.all.ids,
types_technology_id: @type || TypesTechnology.all.ids,
brand_id: @brand || Brand.all.ids)
.ids)
.select(:good_id)
)
@products_all = @products
end
def processing_filter
filter_group_hash={}
filter_group_hash_result={}
# Find product for each filter group
GroupFilter.all.ids.each{|group_filter_id|
#get filter for filter group
filters = Filter.where(id: @filter , group_filter_id: group_filter_id )
# hash { group=>[products]}
filter_group_hash[group_filter_id] = filters.empty? ? @products.select(:id) : @products.where( id: FilterGood.where(filter_id: filters).select(:good_id) ).select(:id)
}
# get and products with each without him self
filters={}
GroupFilter.all.ids.each{|group_filter_id|
filter_group_hash.each{|k,v|
filter_group_hash_result[group_filter_id] = (filter_group_hash_result[group_filter_id] || v) & v if group_filter_id != k
}
filters = filters.merge(FilterGood.where(good_id:filter_group_hash_result[group_filter_id]).group(:filter_id).count)
}
Filter.where.not(id: filters.keys).ids.each{|k| filters[k]=0}
@return_filters = filters
@products = @products.where( id: FilterGood.where(filter_id: @filter).select(:good_id)) if @filter
end
def processing_model
if @model
@models = @models.where(id: @model)
else
temp={}
temp[:brand_id] = @brand if @brand
temp[:types_technology_id] = @type if @type
temp[:id] = ModelsGood.where(good_id: @products).select('distinct model_id').collect(&:model_id)
@models = @models.where(temp )
end
end
def processing_brand
if @brand
@brands = @brands.where(id: @brand)
else
@brands = @brands.where(id: @models.select(:brand_id))
end
end
def processing_type
if @type
@types = @types.where(id: @type)
else
@types = @types.where(id: @models.select(:types_technology_id))
end
end
def seo
if (@type || @brand ) && @page.to_i == 1 && !@model
@return_seo_text = TypesTechnologyBrand.find_by(types_technologies_id: @type, brand_id: @brand)
end
end
def paginate(page = 1, per_page = 24)
@return_products_count = @products.count
@return_page_count = (@return_products_count/(@per_page.to_f)).ceil
if @sort == "" || @sort == nil
@products = @products.order(id: :desc).paginate(:page => @page, :per_page => @per_page)
else
@products = @products.order(price: @sort).paginate(:page => @page, :per_page => @per_page)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment