2010-05-15 10:47:46 -07:00
|
|
|
class ItemsController < ApplicationController
|
2010-06-07 17:02:46 -07:00
|
|
|
before_filter :set_query
|
2011-05-20 17:23:37 -07:00
|
|
|
|
2010-05-15 10:47:46 -07:00
|
|
|
def index
|
|
|
|
if params.has_key?(:q)
|
|
|
|
begin
|
2010-06-22 10:00:55 -07:00
|
|
|
if params[:per_page]
|
|
|
|
per_page = params[:per_page].to_i
|
|
|
|
per_page = 50 if per_page && per_page > 50
|
|
|
|
else
|
|
|
|
per_page = nil
|
|
|
|
end
|
2011-07-17 14:24:29 -07:00
|
|
|
@items = Item.search(@query, current_user).alphabetize.paginate :page => params[:page], :per_page => per_page
|
2011-07-12 22:21:48 -07:00
|
|
|
assign_closeted!
|
2010-06-22 10:00:55 -07:00
|
|
|
respond_to do |format|
|
|
|
|
format.html { render }
|
2010-10-11 15:37:20 -07:00
|
|
|
format.json { render :json => {:items => @items, :total_pages => @items.total_pages} }
|
2010-10-10 19:18:42 -07:00
|
|
|
format.js { render :json => {:items => @items, :total_pages => @items.total_pages}, :callback => params[:callback] }
|
2010-06-22 10:00:55 -07:00
|
|
|
end
|
2010-11-16 20:32:53 -08:00
|
|
|
rescue Item::SearchError
|
2010-06-22 10:00:55 -07:00
|
|
|
respond_to do |format|
|
2010-11-05 15:45:05 -07:00
|
|
|
format.html { flash.now[:alert] = $!.message }
|
2010-06-26 13:20:51 -07:00
|
|
|
format.js { render :json => {:error => $!.message}, :callback => params[:callback] }
|
2010-06-22 10:00:55 -07:00
|
|
|
end
|
2010-05-15 10:47:46 -07:00
|
|
|
end
|
2010-10-10 19:18:42 -07:00
|
|
|
elsif params.has_key?(:ids) && params[:ids].is_a?(Array)
|
|
|
|
@items = Item.find(params[:ids])
|
2011-07-12 22:21:48 -07:00
|
|
|
assign_closeted!
|
2010-10-10 19:18:42 -07:00
|
|
|
respond_to do |format|
|
|
|
|
format.json { render :json => @items }
|
|
|
|
end
|
2010-06-26 13:20:51 -07:00
|
|
|
else
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { render }
|
|
|
|
format.js { render :json => {:error => '$q required'}}
|
|
|
|
end
|
2010-05-15 10:47:46 -07:00
|
|
|
end
|
|
|
|
end
|
2011-05-20 17:23:37 -07:00
|
|
|
|
2010-05-15 17:46:41 -07:00
|
|
|
def show
|
|
|
|
@item = Item.find params[:id]
|
2011-07-14 09:50:24 -07:00
|
|
|
if user_signed_in?
|
2011-07-22 12:31:23 -07:00
|
|
|
@hangers = [true, false].map do |owned|
|
|
|
|
hanger = current_user.closet_hangers.find_or_initialize_by_item_id_and_owned(@item.id, owned)
|
|
|
|
hanger.quantity ||= 1
|
|
|
|
hanger
|
|
|
|
end
|
2011-07-14 09:50:24 -07:00
|
|
|
end
|
2010-05-15 17:46:41 -07:00
|
|
|
end
|
2011-05-20 17:23:37 -07:00
|
|
|
|
2010-11-06 10:07:12 -07:00
|
|
|
def needed
|
|
|
|
if params[:color] && params[:species]
|
|
|
|
@pet_type = PetType.find_by_color_id_and_species_id(
|
|
|
|
params[:color],
|
|
|
|
params[:species]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
unless @pet_type
|
|
|
|
raise ActiveRecord::RecordNotFound, 'Pet type not found'
|
|
|
|
end
|
|
|
|
@items = @pet_type.needed_items.alphabetize
|
2011-07-12 22:21:48 -07:00
|
|
|
assign_closeted!
|
2010-11-06 10:07:12 -07:00
|
|
|
@pet_name = params[:name]
|
|
|
|
render :layout => 'application'
|
|
|
|
end
|
2011-05-20 17:23:37 -07:00
|
|
|
|
2011-07-12 22:21:48 -07:00
|
|
|
protected
|
|
|
|
|
|
|
|
def assign_closeted!
|
|
|
|
current_user.assign_closeted_to_items!(@items) if user_signed_in?
|
|
|
|
end
|
2011-05-20 17:23:37 -07:00
|
|
|
|
2010-06-07 17:02:46 -07:00
|
|
|
def set_query
|
|
|
|
@query = params[:q]
|
|
|
|
end
|
2010-05-15 10:47:46 -07:00
|
|
|
end
|
2011-05-20 17:23:37 -07:00
|
|
|
|