1
0
Fork 0
forked from OpenNeo/impress

basic translation infrastructure for items - search is still english-only, and probably will have to be replaced with a legit search engine

This commit is contained in:
Emi Matchu 2013-01-11 17:56:33 -06:00
parent d79c225c1e
commit 9c8f7f498f
5 changed files with 27 additions and 14 deletions

View file

@ -188,15 +188,17 @@ class ClosetHangersController < ApplicationController
def find_closet_lists_by_owned(closet_lists) def find_closet_lists_by_owned(closet_lists)
return {} if closet_lists == [] return {} if closet_lists == []
closet_lists.alphabetical.includes(:hangers => :item). closet_lists.alphabetical.includes(:hangers => {:item => :translations}).
group_by(&:hangers_owned) group_by(&:hangers_owned)
end end
def find_unlisted_closet_hangers_by_owned(visible_groups) def find_unlisted_closet_hangers_by_owned(visible_groups)
unless visible_groups.empty? unless visible_groups.empty?
@user.closet_hangers.unlisted. @user.closet_hangers.unlisted.
owned_before_wanted.alphabetical_by_item_name.includes(:item). owned_before_wanted.alphabetical_by_item_name.
where(:owned => [visible_groups]).group_by(&:owned) includes(:item => :translations).
where(:owned => [visible_groups]).
group_by(&:owned)
else else
{} {}
end end

View file

@ -10,7 +10,11 @@ class ItemsController < ApplicationController
else else
per_page = nil per_page = nil
end end
@items = Item.search(@query, current_user).alphabetize.paginate :page => params[:page], :per_page => per_page # Note that we sort by name by hand, since we might have to use
# fallbacks after the fact
@items = Item.search(@query, current_user, I18n.default_locale).
alphabetize_by_translations.
paginate(:page => params[:page], :per_page => per_page)
assign_closeted! assign_closeted!
respond_to do |format| respond_to do |format|
format.html { render } format.html { render }
@ -35,7 +39,7 @@ class ItemsController < ApplicationController
respond_to do |format| respond_to do |format|
format.html { format.html {
unless localized_fragment_exist?('items#index newest_items') unless localized_fragment_exist?('items#index newest_items')
@newest_items = Item.newest.limit(18) @newest_items = Item.newest.with_translations.limit(18)
end end
} }
format.js { render :json => {:error => '$q required'}} format.js { render :json => {:error => '$q required'}}
@ -96,7 +100,8 @@ class ItemsController < ApplicationController
raise ActiveRecord::RecordNotFound, 'Pet type not found' raise ActiveRecord::RecordNotFound, 'Pet type not found'
end end
@items = @pet_type.needed_items.alphabetize @items = @pet_type.needed_items.with_translations(I18n.locale).
alphabetize_by_translations
assign_closeted! assign_closeted!
respond_to do |format| respond_to do |format|

View file

@ -47,7 +47,8 @@ class OutfitsController < ApplicationController
end end
unless localized_fragment_exist?('outfits#new newest_items') unless localized_fragment_exist?('outfits#new newest_items')
@newest_items = Item.newest.select([:id, :name, :thumbnail_url]).limit(9) @newest_items = Item.newest.select([:id, :name, :thumbnail_url]).
with_translations.limit(9)
end end
unless localized_fragment_exist?('outfits#new latest_contribution') unless localized_fragment_exist?('outfits#new latest_contribution')

View file

@ -3,7 +3,7 @@ class SitemapController < ApplicationController
def index def index
respond_to do |format| respond_to do |format|
format.xml { @items = Item.sitemap } format.xml { @items = Item.with_translations.sitemap }
end end
end end
end end

View file

@ -25,7 +25,11 @@ class Item < ActiveRecord::Base
cattr_reader :per_page cattr_reader :per_page
@@per_page = 30 @@per_page = 30
scope :alphabetize, order('name ASC') scope :alphabetize, order(arel_table[:name])
scope :alphabetize_by_translations, lambda {
it = Item::Translation.arel_table
order(it[:name])
}
scope :join_swf_assets, joins(:swf_assets).group(arel_table[:id]) scope :join_swf_assets, joins(:swf_assets).group(arel_table[:id])
@ -36,7 +40,8 @@ class Item < ActiveRecord::Base
scope :sold_in_mall, where(:sold_in_mall => true) scope :sold_in_mall, where(:sold_in_mall => true)
scope :not_sold_in_mall, where(:sold_in_mall => false) scope :not_sold_in_mall, where(:sold_in_mall => false)
scope :sitemap, select([:id, :name]).order(:id).limit(49999) scope :sitemap, select([arel_table[:id], arel_table[:name]]).
order(arel_table[:id]).limit(49999)
scope :with_closet_hangers, joins(:closet_hangers) scope :with_closet_hangers, joins(:closet_hangers)
@ -155,7 +160,7 @@ class Item < ActiveRecord::Base
Species.find(species_ids) Species.find(species_ids)
end end
def self.search(query, user=nil) def self.search(query, user, locale)
raise SearchError, "Please provide a search query" unless query raise SearchError, "Please provide a search query" unless query
query = query.strip query = query.strip
raise SearchError, "Search queries should be at least 3 characters" if query.length < 3 raise SearchError, "Search queries should be at least 3 characters" if query.length < 3
@ -175,7 +180,7 @@ class Item < ActiveRecord::Base
end end
end end
limited_filters_used = [] limited_filters_used = []
query_conditions.inject(self.scoped) do |scope, condition| query_conditions.inject(self.with_translations(locale)) do |scope, condition|
if condition.filter? && LimitedSearchFilters.include?(condition.filter) if condition.filter? && LimitedSearchFilters.include?(condition.filter)
if limited_filters_used.include?(condition.filter) if limited_filters_used.include?(condition.filter)
raise SearchError, "The #{condition.filter} filter is complex; please only use one per search. Thanks!" raise SearchError, "The #{condition.filter} filter is complex; please only use one per search. Thanks!"
@ -706,11 +711,11 @@ class Item < ActiveRecord::Base
end end
search_filter :name do |name| search_filter :name do |name|
arel_table[:name].matches("%#{name}%") Item::Translation.arel_table[:name].matches("%#{name}%")
end end
search_filter :description do |description| search_filter :description do |description|
arel_table[:description].matches("%#{description}%") Item::Translation.arel_table[:description].matches("%#{description}%")
end end
def self.adjective_filters def self.adjective_filters