Skip to content

Commit

Permalink
Implemented scoped search
Browse files Browse the repository at this point in the history
  • Loading branch information
Liborio Cannici committed May 16, 2010
1 parent 1a6ba4b commit 8924538
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/controllers/tolk/searches_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions app/helpers/tolk/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 7 additions & 2 deletions app/models/tolk/locale.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
4 changes: 3 additions & 1 deletion app/views/tolk/searches/_form.html.erb
Original file line number Diff line number Diff line change
@@ -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 %>

0 comments on commit 8924538

Please sign in to comment.