Skip to content

Instantly share code, notes, and snippets.

@denisoster
Created December 19, 2017 16:41
Show Gist options
  • Save denisoster/9e60cf13d674d32e50b8b58a7684de75 to your computer and use it in GitHub Desktop.
Save denisoster/9e60cf13d674d32e50b8b58a7684de75 to your computer and use it in GitHub Desktop.
class FilterService
def initialize(params)
@products = Product.all
@category = params[:category] || false
@style = params[:style] || false
@scale = params[:scale] || false
@min = params[:min] || false
@max = params[:max] || false
@sort = params[:sort] || ''
@page = params[:page] || 1
@valuta = params[:valuta] || Valuta.first.id
@return_category = []
@return_style = []
@return_scales = []
@return_min = 0
@return_max = 0
@return_page_count = 0
@return_seo_text = ''
end
def call
self.seo
self.processing
self.processing_category
self.processing_style
self.processing_scale
self.price(@min, @max, @valuta)
self.paginate(@page)
return {
products: @products,
products_count: @products_count,
category: @return_category,
style: @return_style,
scale: @return_scales,
min: @return_min,
max: @return_max,
page_count: @return_page_count,
seo_text: @return_seo_text
}
end
def price(min = false, max = false, valuta)
if @products.count > 1
@return_min = ProductsValuta.where(valuta_id: @valuta, product_id: @products).select(:price_current).minimum(:price_current)
@return_max = ProductsValuta.where(valuta_id: @valuta, product_id: @products).select(:price_current).maximum(:price_current)
end
if (not min) || (not max) || min.to_i > max.to_i
return
end
@products = @products.where(id: ProductsValuta.where(valuta_id: @valuta, price_current: min..max).select(:product_id))
end
def seo
if @category.size == 1 && @style.size == 1 && @page.to_i == 1
@return_seo_text_pred = CategoriesStyle.find_by(category_id: @category, style_id: @style)
if @return_seo_text_pred != nil
@return_seo_text = @return_seo_text_pred.seo.text
end
end
end
def processing
if @category && [email protected]?
@products_category = @products.where(id: ProductsCategory.where(category_id: @category).select(:product_id))
if @category.size == 1 && @style.size == 0 && @page.to_i == 1
@return_seo_text = Category.find_by(id: @category).seo.text
end
else
@products_category = @products
end
if @style && [email protected]?
@products_style = @products.where(id: ProductsStyle.where(style_id: @style).select(:product_id))
if @style.size == 1 && @category.size == 0 && @page.to_i == 1
@return_seo_text = Style.find_by(id: @style).seo.text
end
else
@products_style = @products
end
if @scale && [email protected]?
@products_scale = @products.where(scale_id: @scale)
else
@products_scale = @products
end
@products = @products.where(id: @products_category & @products_style & @products_scale)
end
def processing_category
product = @products_style & @products_scale
Category.all.each {|c|
if ProductsCategory.where(category_id: c, product_id: product).select(:product_id).any?
@return_category << c
end
}
end
def processing_style
product = @products_category & @products_scale
Style.all.each {|c|
if ProductsStyle.where(style_id: c, product_id: product).select(:product_id).any?
@return_style << c
end
}
end
def processing_scale
product = @products_category & @products_style
Scale.all.each {|c|
if Product.where(id: product, scale_id: c).any?
@return_scales << c
end
}
end
def paginate(page = 1)
@products_count = @products.count
@return_page_count = (@products.count/12.0).ceil
if @sort == "" || @sort == nil
@products = @products.order(id: :desc).paginate(:page => @page, :per_page => 12)
else
@products = @products.includes(:products_valuta).order("products_valuta.price_current #{@sort}").references(:products_valuta).paginate(:page => @page, :per_page => 12)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment