Skip to content

Instantly share code, notes, and snippets.

@michiels
Last active December 10, 2015 07:08
Show Gist options
  • Select an option

  • Save michiels/4399159 to your computer and use it in GitHub Desktop.

Select an option

Save michiels/4399159 to your computer and use it in GitHub Desktop.
class OrganizationsController < ApplicationController
include ProductSortableListing
def show
@notification = notification_for_current_user
@categories = Category.top_level
@category = nil
if params[:category_id]
@category = Category.visible.find(params[:category_id])
end
@categories = Category.top_level.visible.orderd_by_name.find(:all, :include => :visible_children)
category_translations = CategoryTranslation.find(:all, :conditions => {:category_id => @categories})
sub_categories = Category.visible.orderd_by_name.find(:all, :conditions => {:category_id => @categories})
@sub_categories_per_category = {}
sub_categories.each do |sub_category|
@sub_categories_per_category[sub_category.category_id] ||= []
@sub_categories_per_category[sub_category.category_id] << sub_category
end
translations_by_category = {}
category_translations.each do |category_translation|
translations_by_category[category_translation.category_id] = category_translation
end
@categories.each do |category|
category.set_translation translations_by_category[category.id]
end
@products = Product.upcoming.active.includes(:product_prices).where('product_prices.active = 1')
# Find this in app/controllers/concerns/product_sortable_listing
@products = sort_products_from_params(@products)
@products = @products.paginate :per_page => 50, :page => params[:page]
respond_to do |format|
format.html do
if ENV["SITE_ALIAS"]
render :layout => ENV["SITE_ALIAS"]+ "/order_system"
else
render :layout => "order_system"
end
end
format.json do
json_products = []
@products.each do |product|
json_products << {:name => product.name, :url => product_url(product), :thumbnail_url => product.image.url(:medium) }
end
render :json => json_products
end
end
end
end
class ProductsController < ApplicationController
include ProductSortableListing
def index
@organization = current_user.organization || Organization.new
if params[:q]
page_number = params[:page] || 1
per_page = 50
@products = Product.search(params[:q]).active.includes(:product_prices).where('product_prices.active = 1', true)
if current_user.searches_are_not_logged == false
SearchResult.create(:keyword => params[:q], :user_id => current_user.id, :number_of_results => @products.size)
end
else # We are displaying products in a category.
raise ActiveRecord::RecordNotFound if @category.nil?
@sub_categories = @category.visible_children
category_ids = [@category] + @sub_categories
@products = Product.active.includes(:product_prices).where('product_prices.active = 1', true).joins(:category_labels).where(["categories_products.category_id IN (?)", category_ids])
end
# Find this in app/controllers/concerns/product_sortable_listing
@products = sort_products_from_params(@products)
@products = @products.paginate :per_page => 50, :page => params[:page]
if request.xhr?
render :partial => "load_more", :layout => false
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment