def index
      
      # If id exists, is an integer and a product exists with the specified id then get the product
      if params[:id] && params[:id].to_i > 0 && Product.exists?(params[:id])
        @product = Product.find(params[:id])
        render 'product/not_available' and return if @product.status == 'Inactive'
        
        @category       = @product.categories.first
        @review         = Review.new
        @reviews        = Review.where(:product_id => @product.id).limit(10).order("id DESC") || nil
        @logged_in_user = logged_in_user
        
        render 'caboose_store/products/details' and return
      end
      
      # Filter params from url
      url_without_params = request.fullpath.split('?').first
      
      # Find the category
      category = Category.where(:url => url_without_params).first
      
      # Set category ID
      params['category_id'] = category.id
      
      # If this is the top-most category, collect all it's immediate children IDs
      params['category_id'] = category.children.collect { |child| child.id } if category.id == 1
      
      # Shove the original category ID into the first position if the param is an array
      params['category_id'].unshift(category.id) if params['category_id'].is_a?(Array)
      
      # Otherwise looking at a category or search parameters
      @pager = Caboose::Pager.new(params, {
        'category_id'    => '',
        'vendor_id'      => '',
        'vendor_name'    => '',
        'vendor_status'  => 'Active',
        'status'         => 'Active',
        'variant_status' => 'Active',
        'price_gte'      => '',
        'price_lte'      => '',
        'alternate_id'   => '',
        'search_like'    => ''
      }, {
        'model'          => 'CabooseStore::Product',
        'sort'           => if params[:sort] then params[:sort] else 'store_products.sort_order' end,
        'base_url'       => url_without_params,
        'items_per_page' => 15,
        'use_url_params' => false,
        
        'abbreviations' => {
          'search_like' => 'title_concat_store_products.alternate_id_concat_vendor_name_concat_category_name_like',
        },
        
        'includes' => {
          'category_id'    => [ 'categories' , 'id'     ],
          'category_name'  => [ 'categories' , 'name'   ],
          'vendor_id'      => [ 'vendor'     , 'id'     ],
          'vendor_name'    => [ 'vendor'     , 'name'   ],
          'vendor_status'  => [ 'vendor'     , 'status' ],
          'price_gte'      => [ 'variants'   , 'price'  ],
          'price_lte'      => [ 'variants'   , 'price'  ],
          'variant_status' => [ 'variants'   , 'status' ]
        }
      })
      
      @sort_options = [
        { :name => 'Default',             :value => 'store_products.sort_order' },
        { :name => 'Price (Low to High)', :value => 'store_variants.price ASC'  },
        { :name => 'Price (High to Low)', :value => 'store_variants.price DESC' },
        { :name => 'Alphabetical (A-Z)',  :value => 'store_products.title ASC'  },
        { :name => 'Alphabetical (Z-A)',  :value => 'store_products.title DESC' },
      ]
      
      SearchFilter.delete_all
      
      @filter   = SearchFilter.find_from_url(request.fullpath, @pager, ['page'])
      @products = @pager.items
      @category = if @filter['category_id'] then Category.find(@filter['category_id'].to_i) else nil end
      
      @pager.set_item_count
    end
def index
      
      # If id exists, is an integer and a product exists with the specified id then get the product
      if params[:id] && params[:id].to_i > 0 && Product.exists?(params[:id])
        @product = Product.find(params[:id])
        render 'product/not_available' and return if @product.status == 'Inactive'
        
        @category       = @product.categories.first
        @review         = Review.new
        @reviews        = Review.where(:product_id => @product.id).limit(10).order("id DESC") || nil
        @logged_in_user = logged_in_user
        
        render 'caboose_store/products/details' and return
      end
      
      # Filter params from url
      url_without_params = request.fullpath.split('?').first
      
      # Find the category
      category = Category.where(:url => url_without_params).first
      
      # Set category ID
      params['category_id'] = category.id
      
      # If this is the top-most category, collect all it's immediate children IDs
      params['category_id'] = category.children.collect { |child| child.id } if category.id == 1
      
      # Shove the original category ID into the first position if the param is an array
      params['category_id'].unshift(category.id) if params['category_id'].is_a?(Array)
      
      # Otherwise looking at a category or search parameters
      @pager = Caboose::Pager.new(params, {
        'category_id'    => '',
        'vendor_id'      => '',
        'vendor_name'    => '',
        'vendor_status'  => 'Active',
        'status'         => 'Active',
        'variant_status' => 'Active',
        'price_gte'      => '',
        'price_lte'      => '',
        'alternate_id'   => '',
        'search_like'    => ''
      }, {
        'model'          => 'CabooseStore::Product',
        'sort'           => if params[:sort] then params[:sort] else 'store_products.sort_order' end,
        'base_url'       => url_without_params,
        'items_per_page' => 15,
        'use_url_params' => false,
        
        'abbreviations' => {
          'search_like' => 'title_concat_store_products.alternate_id_concat_vendor_name_concat_category_name_like',
        },
        
        'includes' => {
          'category_id'    => [ 'categories' , 'id'     ],
          'category_name'  => [ 'categories' , 'name'   ],
          'vendor_id'      => [ 'vendor'     , 'id'     ],
          'vendor_name'    => [ 'vendor'     , 'name'   ],
          'vendor_status'  => [ 'vendor'     , 'status' ],
          'price_gte'      => [ 'variants'   , 'price'  ],
          'price_lte'      => [ 'variants'   , 'price'  ],
          'variant_status' => [ 'variants'   , 'status' ]
        }
      })
      
      @sort_options = [
        { :name => 'Default',             :value => 'store_products.sort_order' },
        { :name => 'Price (Low to High)', :value => 'store_variants.price ASC'  },
        { :name => 'Price (High to Low)', :value => 'store_variants.price DESC' },
        { :name => 'Alphabetical (A-Z)',  :value => 'store_products.title ASC'  },
        { :name => 'Alphabetical (Z-A)',  :value => 'store_products.title DESC' },
      ]
      
      SearchFilter.delete_all
      
      @filter   = SearchFilter.find_from_url(request.fullpath, @pager, ['page'])
      @products = @pager.items
      @category = if @filter['category_id'] then Category.find(@filter['category_id'].to_i) else nil end
      
      @pager.set_item_count
    end
GET /products || GET /products/:id