2010-05-15 10:47:46 -07:00
|
|
|
class ItemsController < ApplicationController
|
2023-08-02 16:05:02 -07:00
|
|
|
before_action :set_query
|
2013-01-21 11:44:02 -08:00
|
|
|
rescue_from Item::Search::Error, :with => :search_error
|
2011-05-20 17:23:37 -07:00
|
|
|
|
2010-05-15 10:47:46 -07:00
|
|
|
def index
|
2014-02-26 21:55:14 -08:00
|
|
|
if @query
|
2024-02-27 14:47:02 -08: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
|
|
|
|
end
|
2024-02-23 10:44:50 -08:00
|
|
|
|
2024-02-27 14:47:02 -08:00
|
|
|
@items = @query.results.paginate(
|
|
|
|
page: params[:page], per_page: per_page)
|
|
|
|
assign_closeted!
|
2024-02-23 10:44:50 -08:00
|
|
|
|
2024-02-27 14:47:02 -08:00
|
|
|
respond_to do |format|
|
|
|
|
format.html {
|
|
|
|
@campaign = Fundraising::Campaign.current rescue nil
|
|
|
|
if @items.count == 1
|
|
|
|
redirect_to @items.first
|
|
|
|
else
|
|
|
|
render
|
|
|
|
end
|
|
|
|
}
|
|
|
|
format.json {
|
|
|
|
render json: {
|
|
|
|
items: @items.as_json(
|
|
|
|
methods: [:nc?, :pb?, :owned?, :wanted?],
|
|
|
|
),
|
|
|
|
appearances: load_appearances.as_json(
|
|
|
|
include: {
|
|
|
|
swf_assets: {
|
|
|
|
only: [:id, :remote_id, :body_id],
|
|
|
|
include: {
|
|
|
|
zone: {
|
|
|
|
only: [:id, :depth, :label],
|
|
|
|
methods: [:is_commonly_used_by_items],
|
|
|
|
},
|
|
|
|
restricted_zones: {
|
|
|
|
only: [:id, :depth, :label],
|
|
|
|
methods: [:is_commonly_used_by_items],
|
2024-02-25 12:06:20 -08:00
|
|
|
},
|
|
|
|
},
|
2024-02-27 14:47:02 -08:00
|
|
|
methods: [:urls, :known_glitches],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
),
|
|
|
|
total_pages: @items.total_pages,
|
|
|
|
query: @query.to_s,
|
2013-06-26 23:01:12 -07:00
|
|
|
}
|
2024-02-27 14:47:02 -08:00
|
|
|
}
|
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)
|
2023-08-02 11:41:19 -07:00
|
|
|
@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|
|
2013-06-26 23:01:12 -07:00
|
|
|
format.json { render json: @items }
|
2010-10-10 19:18:42 -07:00
|
|
|
end
|
2010-06-26 13:20:51 -07:00
|
|
|
else
|
|
|
|
respond_to do |format|
|
2011-08-04 07:01:44 -07:00
|
|
|
format.html {
|
2024-02-18 20:29:31 -08:00
|
|
|
@campaign = Fundraising::Campaign.current rescue nil
|
2024-02-20 16:04:41 -08:00
|
|
|
@newest_items = Item.newest.limit(18)
|
2011-08-04 07:01:44 -07:00
|
|
|
}
|
2010-06-26 13:20:51 -07:00
|
|
|
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-30 21:19:28 -07:00
|
|
|
|
2011-08-02 17:01:48 -07:00
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
2024-01-21 04:49:06 -08:00
|
|
|
@trades = @item.closet_hangers.trading.user_is_active.to_trades
|
2024-01-19 01:39:25 -08:00
|
|
|
|
2024-01-21 00:39:20 -08:00
|
|
|
@contributors_with_counts = @item.contributors_with_counts
|
2011-08-02 17:01:48 -07:00
|
|
|
|
|
|
|
if user_signed_in?
|
2024-01-21 06:42:24 -08:00
|
|
|
@current_user_lists = current_user.closet_lists.alphabetical.
|
|
|
|
group_by_owned
|
|
|
|
@current_user_quantities = current_user.item_quantities_for(@item)
|
2011-08-02 17:01:48 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
format.gif do
|
|
|
|
expires_in 1.month
|
|
|
|
redirect_to @item.thumbnail_url
|
2011-07-22 12:31:23 -07:00
|
|
|
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
|
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
|
|
|
|
2024-02-20 16:04:41 -08:00
|
|
|
@items = @pet_type.needed_items.order(:name)
|
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-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
|
2024-02-23 10:44:50 -08:00
|
|
|
|
|
|
|
def load_appearances
|
2024-02-25 14:46:27 -08:00
|
|
|
appearance_params = params[:with_appearances_for]
|
|
|
|
return {} if appearance_params.blank?
|
2024-02-27 16:11:06 -08:00
|
|
|
|
|
|
|
if appearance_params[:alt_style_id].present?
|
|
|
|
target = Item::Search::Query.load_alt_style_by_id(
|
|
|
|
appearance_params[:alt_style_id])
|
|
|
|
else
|
|
|
|
target = Item::Search::Query.load_pet_type_by_color_and_species(
|
|
|
|
appearance_params[:color_id], appearance_params[:species_id])
|
|
|
|
end
|
2024-02-23 10:44:50 -08:00
|
|
|
|
2024-02-27 16:11:06 -08:00
|
|
|
target.appearances_for(@items.map(&:id), swf_asset_includes: [:zone])
|
2024-02-23 10:44:50 -08:00
|
|
|
end
|
2013-01-21 11:44:02 -08:00
|
|
|
|
|
|
|
def search_error(e)
|
|
|
|
@items = []
|
2024-02-27 14:47:57 -08:00
|
|
|
@query = params[:q]
|
2013-01-21 11:44:02 -08:00
|
|
|
respond_to do |format|
|
|
|
|
format.html { flash.now[:alert] = e.message; render }
|
2013-06-26 23:01:12 -07:00
|
|
|
format.json { render :json => {error: e.message} }
|
2013-01-21 11:44:02 -08:00
|
|
|
end
|
|
|
|
end
|
2011-05-20 17:23:37 -07:00
|
|
|
|
2010-06-07 17:02:46 -07:00
|
|
|
def set_query
|
2014-02-26 21:55:14 -08:00
|
|
|
q = params[:q]
|
|
|
|
if q.is_a?(String)
|
2024-02-27 14:47:57 -08:00
|
|
|
@query = Item::Search::Query.from_text(q, current_user)
|
2024-02-23 10:44:50 -08:00
|
|
|
elsif q.is_a?(ActionController::Parameters)
|
2014-02-26 21:55:14 -08:00
|
|
|
@query = Item::Search::Query.from_params(q, current_user)
|
|
|
|
end
|
2010-06-07 17:02:46 -07:00
|
|
|
end
|
2010-05-15 10:47:46 -07:00
|
|
|
end
|