Skip to content

Commit

Permalink
Merge branch 'search_key' of git://github.com/ZenCocoon/tolk
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio committed Nov 24, 2010
2 parents 5b6f6c1 + 19bebb2 commit 330f178
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/controllers/tolk/searches_controller.rb
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions app/models/tolk/locale.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions app/models/tolk/phrase.rb
Expand Up @@ -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
2 changes: 2 additions & 0 deletions app/views/tolk/searches/_form.html.erb
Expand Up @@ -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 %>
4 changes: 3 additions & 1 deletion app/views/tolk/searches/show.html.erb
Expand Up @@ -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 %>
Expand Down

0 comments on commit 330f178

Please sign in to comment.