diff --git a/app/controllers/tolk/searches_controller.rb b/app/controllers/tolk/searches_controller.rb index 49a6d2e..a07dd7d 100644 --- a/app/controllers/tolk/searches_controller.rb +++ b/app/controllers/tolk/searches_controller.rb @@ -3,7 +3,7 @@ class SearchesController < Tolk::ApplicationController before_filter :find_locale def show - @phrases = @locale.search_phrases(params[:q], params[:scope].to_sym, params[:page]) + @phrases = @locale.search_phrases(params[:q], params[:scope].to_sym, params[:k], params[:page]) end private diff --git a/app/models/tolk/locale.rb b/app/models/tolk/locale.rb index 10e87ef..b72801e 100644 --- a/app/models/tolk/locale.rb +++ b/app/models/tolk/locale.rb @@ -139,8 +139,8 @@ def phrases_without_translation(page = nil, options = {}) result end - def search_phrases(query, scope, page = nil, options = {}) - return [] unless query.present? + def search_phrases(query, scope, key_query, page = nil, options = {}) + return [] unless query.present? || key_query.present? translations = case scope when :origin @@ -149,7 +149,8 @@ def search_phrases(query, scope, page = nil, options = {}) self.translations.containing_text(query) end - phrases = Tolk::Phrase.scoped(:order => 'tolk_phrases.key ASC') + phrases = Tolk::Phrase.scoped(:order => 'tolk_phrases.key ASC') + phrases = phrases.containing_text(key_query) phrases = phrases.scoped(:conditions => ['tolk_phrases.id IN(?)', translations.map(&:phrase_id).uniq]) phrases.paginate({:page => page}.merge(options)) end diff --git a/app/models/tolk/phrase.rb b/app/models/tolk/phrase.rb index 9646ef5..e39af24 100644 --- a/app/models/tolk/phrase.rb +++ b/app/models/tolk/phrase.rb @@ -18,5 +18,9 @@ def for(locale) end attr_accessor :translation + + named_scope :containing_text, lambda { |query| + { :conditions => ["tolk_phrases.key LIKE ?", "%#{query}%"] } + } end end diff --git a/app/views/tolk/searches/_form.html.erb b/app/views/tolk/searches/_form.html.erb index f7e13a8..b5677cc 100644 --- a/app/views/tolk/searches/_form.html.erb +++ b/app/views/tolk/searches/_form.html.erb @@ -4,5 +4,7 @@ <%= scope_selector_for(@locale) %> phrase: <%= text_field_tag :q, params[:q] %> + within key: + <%= text_field_tag :k, params[:k] %> <%= submit_tag "Search", :name => nil %> <% end %> diff --git a/app/views/tolk/searches/show.html.erb b/app/views/tolk/searches/show.html.erb index 383da35..69d448e 100644 --- a/app/views/tolk/searches/show.html.erb +++ b/app/views/tolk/searches/show.html.erb @@ -36,7 +36,9 @@ <% else -%> <%= format_i18n_value(phrase.translations.primary.text) -%> <% end -%> - <%= truncate(phrase.key, :length => 100) %> + <%= params[:k].present? ? + highlight(h(truncate(phrase.key, :length => 100)), params[:k]) : + h(truncate(phrase.key, :length => 100)) %> <% end %>