better handling of i18n and labels and resource filters and junk

This commit is contained in:
Emi Matchu 2014-02-27 00:21:20 -06:00
parent ed9c828373
commit 1d11cf6edc
5 changed files with 54 additions and 15 deletions

View file

@ -152,7 +152,14 @@ class ItemsController < ApplicationController
def set_query def set_query
q = params[:q] q = params[:q]
if q.is_a?(String) if q.is_a?(String)
begin
@query = Item::Search::Query.from_text(q, current_user) @query = Item::Search::Query.from_text(q, current_user)
rescue
# Set the query string for error handling messages, but let the error
# bubble up.
@query = params[:q]
raise
end
elsif q.is_a?(Hash) elsif q.is_a?(Hash)
@query = Item::Search::Query.from_params(q, current_user) @query = Item::Search::Query.from_params(q, current_user)
end end

View file

@ -15,18 +15,36 @@ class Item
def to_s def to_s
sign = positive? ? '' : '-' sign = positive? ? '' : '-'
key_str = key.to_s key_str = @key.to_s
if key_str.start_with?('is_') if key_str.start_with?('is_')
rest_of_key = key_str[3..-1] is_label = I18n.translate("items.search.flag_keywords.is")
"#{sign}is:#{rest_of_key}" "#{sign}#{is_label}:#{label}"
else else
quoted_value = value.include?(' ') ? value.inspect : value if Query::TEXT_QUERY_RESOURCE_TYPES_BY_KEY.include?(@key)
if key == :name resource_type = Query::TEXT_QUERY_RESOURCE_TYPES_BY_KEY[@key]
reverse_finder = Query::REVERSE_RESOURCE_FINDERS[resource_type]
resource_value = reverse_finder.call(@value)
else
resource_value = @value
end
if resource_value.include?(' ')
quoted_value = resource_value.inspect
else
quoted_value = resource_value
end
if @key == :name
"#{sign}#{quoted_value}" "#{sign}#{quoted_value}"
else else
"#{sign}#{key}:#{quoted_value}"
end "#{sign}#{label}:#{quoted_value}"
end end
end
end
private
def label
I18n.translate("items.search.labels.#{@key}").split(',').first
end end
end end
end end

View file

@ -15,9 +15,10 @@ class Item
} }
FIELD_KEYS = FIELD_CLASSES.keys FIELD_KEYS = FIELD_CLASSES.keys
def initialize(filters, user) def initialize(filters, user, text=nil)
@filters = filters @filters = filters
@user = user @user = user
@text = text
end end
def fields def fields
@ -134,7 +135,7 @@ class Item
end end
def to_s def to_s
@filters.map(&:to_s).join(' ') @text || @filters.map(&:to_s).join(' ')
end end
TEXT_QUERY_RESOURCE_FINDERS = { TEXT_QUERY_RESOURCE_FINDERS = {
@ -161,6 +162,18 @@ class Item
} }
} }
REVERSE_RESOURCE_FINDERS = {
species: lambda { |id|
Species.find(id).name
},
zone: lambda { |id|
Zone.find(id).plain_label
},
ownership: lambda { |owned|
I18n.translate("items.search.labels.user_#{owned}")
}
}
TEXT_QUERY_RESOURCE_TYPES_BY_KEY = { TEXT_QUERY_RESOURCE_TYPES_BY_KEY = {
:species_support_id => :species, :species_support_id => :species,
:occupied_zone_id => :zone, :occupied_zone_id => :zone,
@ -222,7 +235,7 @@ class Item
filters << Filter.new(key, value, is_positive) filters << Filter.new(key, value, is_positive)
end end
self.new(filters, user) self.new(filters, user, text)
end end
def self.from_params(params, user=nil) def self.from_params(params, user=nil)
@ -230,7 +243,8 @@ class Item
if filter_params.has_key?(:key) if filter_params.has_key?(:key)
key = filter_params[:key].to_sym key = filter_params[:key].to_sym
if FIELD_KEYS.include?(key) if FIELD_KEYS.include?(key)
Filter.new(key, filter_params[:value], filter_params[:is_positive] == 'true') is_positive = filter_params[:is_positive] == 'true'
Filter.new(key, filter_params[:value], is_positive)
end end
end end
}.compact }.compact

View file

@ -1,7 +1,7 @@
- if @items - if @items
- title t('.title_with_query', :query => @query) - title t('.title_with_query', :query => @query.to_s)
- if @items.empty? - if @items.empty?
%p= t '.no_results_html', :query_html => content_tag(:strong, @query) %p= t '.no_results_html', :query_html => content_tag(:strong, @query.to_s)
%p= twl '.modeling_request', :modeling_link_url => root_path %p= twl '.modeling_request', :modeling_link_url => root_path
- else - else
= will_paginate @items = will_paginate @items

View file

@ -8,7 +8,7 @@
%span= t 'infinite_closet' %span= t 'infinite_closet'
- content_for :content do - content_for :content do
= form_tag items_path, :method => :get do = form_tag items_path, :method => :get do
= text_field_tag :q, @query = text_field_tag :q, @query.to_s
= submit_tag t('.search'), :name => nil = submit_tag t('.search'), :name => nil
= yield = yield
= render :file => 'layouts/application' = render :file => 'layouts/application'