Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implemented scoped search

  • Loading branch information...
commit 8924538fca450868e233b14596d78c5042f76077 1 parent 1a6ba4b
Liborio Cannici authored
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[:page])
+ @phrases = @locale.search_phrases(params[:q], params[:scope].to_sym, params[:page])
end
private
View
5 app/helpers/tolk/application_helper.rb
@@ -24,5 +24,10 @@ def tolk_locale_selection
pairs = Tolk::Locale::MAPPING.to_a.map(&:reverse).sort_by(&:first)
pairs.reject {|pair| existing_locale_names.include?(pair.last) }
end
+
+ def scope_selector_for(locale)
+ select_tag 'scope', options_for_select([[Tolk::Locale.primary_locale.language_name, "origin"],
+ [locale.language_name, "target"]], params[:scope])
+ end
end
end
View
9 app/models/tolk/locale.rb
@@ -138,10 +138,15 @@ def phrases_without_translation(page = nil, options = {})
result
end
- def search_phrases(query, page = nil, options = {})
+ def search_phrases(query, scope, page = nil, options = {})
return [] unless query.present?
- translations = Tolk::Locale.primary_locale.translations.all(:conditions => ["tolk_translations.text LIKE ?", "%#{query}%"])
+ case scope
+ when :origin
+ translations = Tolk::Locale.primary_locale.translations.containing_text(query)
+ when :target
+ translations = self.translations.containing_text(query)
+ end
phrases = Tolk::Phrase.scoped(:order => 'tolk_phrases.key ASC')
phrases = phrases.scoped(:conditions => ['tolk_phrases.id IN(?)', translations.map(&:phrase_id).uniq])
View
4 app/views/tolk/searches/_form.html.erb
@@ -1,6 +1,8 @@
<% form_tag "/tolk/search", :method => :get do %>
<%= hidden_field_tag :locale, locale.name %>
- Search for an English phrase:
+ Search for
+ <%= scope_selector_for(@locale) %>
+ phrase:
<%= text_field_tag :q, params[:q] %>
<%= submit_tag "Search", :name => nil %>
<% end %>
Please sign in to comment.
Something went wrong with that request. Please try again.