Permalink
Browse files

Merge branch 'search_key' of git://github.com/ZenCocoon/tolk

  • Loading branch information...
2 parents 5b6f6c1 + 19bebb2 commit 330f178c1636f13bd4f715a3797842dcf93eefeb @cover committed Nov 24, 2010
View
2 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
View
7 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
View
4 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
View
2 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 %>
View
4 app/views/tolk/searches/show.html.erb
@@ -36,7 +36,9 @@
<% else -%>
<%= format_i18n_value(phrase.translations.primary.text) -%>
<% end -%>
- <span class="key" title="<%= phrase.key %>"><%= truncate(phrase.key, :length => 100) %></span>
+ <span class="key" title="<%= phrase.key %>"><%= params[:k].present? ?
+ highlight(h(truncate(phrase.key, :length => 100)), params[:k]) :
+ h(truncate(phrase.key, :length => 100)) %></span>
</td>
</tr>
<% end %>

0 comments on commit 330f178

Please sign in to comment.