impress/app/controllers/items_controller.rb

127 lines
3.7 KiB
Ruby
Raw Normal View History

2010-05-15 10:47:46 -07:00
class ItemsController < ApplicationController
2010-06-07 17:02:46 -07:00
before_filter :set_query
rescue_from Item::Search::Error, :with => :search_error
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 = 30
2010-06-22 10:00:55 -07:00
end
# Note that we sort by name by hand, since we might have to use
# fallbacks after the fact
@items = Item::Search::Query.from_text(@query, current_user).
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 }
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-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
else
respond_to do |format|
2011-08-04 07:01:44 -07:00
format.html {
unless localized_fragment_exist?('items#index newest_items')
@newest_items = Item.newest.with_translations.limit(18)
end
2011-08-04 07:01:44 -07:00
}
format.js { render :json => {:error => '$q required'}}
end
2010-05-15 10:47:46 -07:00
end
end
2010-05-15 17:46:41 -07:00
def show
@item = Item.find params[:id]
2011-07-30 21:19:28 -07:00
respond_to do |format|
format.html do
unless localized_fragment_exist?("items/#{@item.id} contributors")
@contributors_with_counts = @item.contributors_with_counts
end
2011-07-30 21:19:28 -07:00
@trading_closet_hangers_by_owned = {
true => @item.closet_hangers.owned_trading.newest.includes(:user),
false => @item.closet_hangers.wanted_trading.newest.includes(:user)
}
if user_signed_in?
# Empty arrays are important so that we can loop over this and still
# show the generic no-list case
@current_user_lists = {true => [], false => []}
current_user.closet_lists.alphabetical.each do |list|
@current_user_lists[list.hangers_owned] << list
end
@current_user_quantities = Hash.new(0) # default is zero
hangers = current_user.closet_hangers.where(:item_id => @item.id).
select([:owned, :list_id, :quantity])
hangers.each do |hanger|
key = hanger.list_id || hanger.owned
@current_user_quantities[key] = hanger.quantity
end
end
end
format.gif do
expires_in 1.month
redirect_to @item.thumbnail_url
end
2011-07-14 09:50:24 -07:00
end
2010-05-15 17:46:41 -07:00
end
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
2012-08-06 18:15:31 -07:00
2010-11-06 10:07:12 -07:00
unless @pet_type
raise ActiveRecord::RecordNotFound, 'Pet type not found'
end
2012-08-06 18:15:31 -07:00
@items = @pet_type.needed_items.with_translations.
alphabetize_by_translations
2011-07-12 22:21:48 -07:00
assign_closeted!
2012-08-06 18:15:31 -07:00
respond_to do |format|
format.html { @pet_name = params[:name] ; render :layout => 'application' }
format.json { render :json => @items }
end
2010-11-06 10:07:12 -07:00
end
2011-07-12 22:21:48 -07:00
protected
def assign_closeted!
current_user.assign_closeted_to_items!(@items) if user_signed_in?
end
def search_error(e)
@items = []
respond_to do |format|
format.html { flash.now[:alert] = e.message; render }
format.json { render :json => {:error => e.message} }
format.js { render :json => {:error => e.message},
:callback => params[:callback] }
end
end
2010-06-07 17:02:46 -07:00
def set_query
@query = params[:q]
end
2010-05-15 10:47:46 -07:00
end