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
q = params[:q]
if q.is_a?(String)
begin
@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)
@query = Item::Search::Query.from_params(q, current_user)
end

View file

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

View file

@ -15,9 +15,10 @@ class Item
}
FIELD_KEYS = FIELD_CLASSES.keys
def initialize(filters, user)
def initialize(filters, user, text=nil)
@filters = filters
@user = user
@text = text
end
def fields
@ -134,7 +135,7 @@ class Item
end
def to_s
@filters.map(&:to_s).join(' ')
@text || @filters.map(&:to_s).join(' ')
end
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 = {
:species_support_id => :species,
:occupied_zone_id => :zone,
@ -222,7 +235,7 @@ class Item
filters << Filter.new(key, value, is_positive)
end
self.new(filters, user)
self.new(filters, user, text)
end
def self.from_params(params, user=nil)
@ -230,7 +243,8 @@ class Item
if filter_params.has_key?(:key)
key = filter_params[:key].to_sym
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
}.compact

View file

@ -1,7 +1,7 @@
- if @items
- title t('.title_with_query', :query => @query)
- title t('.title_with_query', :query => @query.to_s)
- 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
- else
= will_paginate @items

View file

@ -8,7 +8,7 @@
%span= t 'infinite_closet'
- content_for :content 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
= yield
= render :file => 'layouts/application'