2010-05-15 10:47:46 -07:00
|
|
|
class ItemsController < ApplicationController
|
2010-06-07 17:02:46 -07:00
|
|
|
before_filter :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
|
|
|
|
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
|
globalized search first draft
Confirmed features:
* Output (retrieval, sorting, etc.)
* Name (positive and negative, but new behavior)
* Flags (positive and negative)
Planned features:
* users:owns, user:wants
Known issues:
* Sets are broken
* Don't render properly
* Shouldn't actually be done as joined sets, anyway, since
we actually want (set1_zone1 OR set1_zone2) AND
(set2_zone1 OR set2_zone2), which will require breaking
it into multiple terms queries.
* Name has regressed: ignores phrases, doesn't require *all*
words. While we're breaking sets into multiple queries,
maybe we'll do something similar for name. In fact, we
really kinda have to if we're gonna keep sorting by name,
since "straw hat" returns all hats. Eww.
2013-01-18 21:23:37 -08:00
|
|
|
per_page = 30
|
2010-06-22 10:00:55 -07:00
|
|
|
end
|
2013-01-11 15:56:33 -08:00
|
|
|
# Note that we sort by name by hand, since we might have to use
|
|
|
|
# fallbacks after the fact
|
globalized search first draft
Confirmed features:
* Output (retrieval, sorting, etc.)
* Name (positive and negative, but new behavior)
* Flags (positive and negative)
Planned features:
* users:owns, user:wants
Known issues:
* Sets are broken
* Don't render properly
* Shouldn't actually be done as joined sets, anyway, since
we actually want (set1_zone1 OR set1_zone2) AND
(set2_zone1 OR set2_zone2), which will require breaking
it into multiple terms queries.
* Name has regressed: ignores phrases, doesn't require *all*
words. While we're breaking sets into multiple queries,
maybe we'll do something similar for name. In fact, we
really kinda have to if we're gonna keep sorting by name,
since "straw hat" returns all hats. Eww.
2013-01-18 21:23:37 -08:00
|
|
|
@items = Item::Search::Query.from_text(@query, current_user).
|
use proxies for item html, too
Some lame benchmarking on my box, dev, cache classes, many items:
No proxies:
Fresh JSON: 175, 90, 90, 93, 82, 88, 158, 150, 85, 167 = 117.8
Cached JSON: (none)
Fresh HTML: 371, 327, 355, 328, 322, 346 = 341.5
Cached HTML: 173, 123, 175, 187, 171, 179 = 168
Proxies:
Fresh JSON: 175, 183, 269, 219, 195, 178 = 203.17
Cached JSON: 88, 70, 89, 162, 80, 77 = 94.3
Fresh HTML: 494, 381, 350, 334, 451, 372 = 397
Cached HTML: 176, 170, 104, 101, 111, 116 = 129.7
So, overhead is significant, but the gains when cached (and that should be
all the time, since we currently have 0 evictions) are definitely worth
it. Worth pushing, and probably putting some future effort into reducing
overhead.
On production (again, lame), items#index was consistently averaging
73-74ms when super healthy, and 82ms when pets#index was being louder
than usual. For reference is all. This will probably perform
significantly worse at first (in JSON, anyway, since HTML is already
mostly cached), so it might be worth briefly warming the cache after
pushing.
2013-06-26 23:39:04 -07:00
|
|
|
paginate(page: params[:page], per_page: per_page, as: :proxies)
|
2011-07-12 22:21:48 -07:00
|
|
|
assign_closeted!
|
2010-06-22 10:00:55 -07:00
|
|
|
respond_to do |format|
|
use proxies for item html, too
Some lame benchmarking on my box, dev, cache classes, many items:
No proxies:
Fresh JSON: 175, 90, 90, 93, 82, 88, 158, 150, 85, 167 = 117.8
Cached JSON: (none)
Fresh HTML: 371, 327, 355, 328, 322, 346 = 341.5
Cached HTML: 173, 123, 175, 187, 171, 179 = 168
Proxies:
Fresh JSON: 175, 183, 269, 219, 195, 178 = 203.17
Cached JSON: 88, 70, 89, 162, 80, 77 = 94.3
Fresh HTML: 494, 381, 350, 334, 451, 372 = 397
Cached HTML: 176, 170, 104, 101, 111, 116 = 129.7
So, overhead is significant, but the gains when cached (and that should be
all the time, since we currently have 0 evictions) are definitely worth
it. Worth pushing, and probably putting some future effort into reducing
overhead.
On production (again, lame), items#index was consistently averaging
73-74ms when super healthy, and 82ms when pets#index was being louder
than usual. For reference is all. This will probably perform
significantly worse at first (in JSON, anyway, since HTML is already
mostly cached), so it might be worth briefly warming the cache after
pushing.
2013-06-26 23:39:04 -07:00
|
|
|
format.html {
|
|
|
|
@items.prepare_partial(:item_link_partial)
|
|
|
|
render
|
|
|
|
}
|
2013-06-26 23:01:12 -07:00
|
|
|
format.json {
|
|
|
|
@items.prepare_method(:as_json)
|
|
|
|
render json: {items: @items, total_pages: @items.total_pages}
|
|
|
|
}
|
|
|
|
format.js {
|
|
|
|
@items.prepare_method(:as_json)
|
|
|
|
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)
|
2013-06-27 00:15:20 -07:00
|
|
|
@items = Item.build_proxies(params[:ids])
|
2011-07-12 22:21:48 -07:00
|
|
|
assign_closeted!
|
2013-06-27 00:15:20 -07:00
|
|
|
@items.prepare_method(:as_json)
|
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 {
|
2012-12-29 23:40:30 -08:00
|
|
|
unless localized_fragment_exist?('items#index newest_items')
|
2013-01-21 17:52:16 -08:00
|
|
|
@newest_items = Item.newest.includes(:translations).limit(18)
|
2012-08-09 19:35:30 -07:00
|
|
|
end
|
2011-08-04 07:01:44 -07:00
|
|
|
}
|
2013-06-26 23:01:12 -07:00
|
|
|
format.js { render json: {error: '$q required'}}
|
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
|
2013-01-21 17:34:39 -08:00
|
|
|
unless localized_fragment_exist?("items/#{@item.id} info")
|
|
|
|
@occupied_zones = @item.occupied_zones(
|
2013-06-26 23:01:12 -07:00
|
|
|
scope: Zone.includes_translations.alphabetical
|
2013-01-21 17:34:39 -08:00
|
|
|
)
|
|
|
|
@restricted_zones = @item.restricted_zones(
|
2013-06-26 23:01:12 -07:00
|
|
|
scope: Zone.includes_translations.alphabetical
|
2013-01-21 17:34:39 -08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2012-12-30 11:15:55 -08:00
|
|
|
unless localized_fragment_exist?("items/#{@item.id} contributors")
|
2012-10-24 20:09:05 -07:00
|
|
|
@contributors_with_counts = @item.contributors_with_counts
|
|
|
|
end
|
2011-07-30 21:19:28 -07:00
|
|
|
|
2011-08-02 17:01:48 -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?
|
2011-10-10 19:43:46 -07:00
|
|
|
# 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
|
2013-06-26 23:01:12 -07:00
|
|
|
hangers = current_user.closet_hangers.where(item_id: @item.id).
|
2011-10-10 19:43:46 -07:00
|
|
|
select([:owned, :list_id, :quantity])
|
|
|
|
|
|
|
|
hangers.each do |hanger|
|
|
|
|
key = hanger.list_id || hanger.owned
|
|
|
|
@current_user_quantities[key] = hanger.quantity
|
2011-08-02 17:01:48 -07:00
|
|
|
end
|
|
|
|
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
|
|
|
|
2013-01-21 17:52:16 -08:00
|
|
|
@items = @pet_type.needed_items.includes(:translations).
|
2013-01-11 15:56:33 -08:00
|
|
|
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-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
|
2013-01-21 11:44:02 -08:00
|
|
|
|
|
|
|
def search_error(e)
|
|
|
|
@items = []
|
|
|
|
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} }
|
|
|
|
format.js { render :json => {error: e.message},
|
2013-01-21 11:44:02 -08:00
|
|
|
:callback => params[:callback] }
|
|
|
|
end
|
|
|
|
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
|
|
|
|