Permalink
Browse files

Moved search actions into their own controller and fixed display of s…

…earch results.
  • Loading branch information...
1 parent 2e1cd6d commit cd7dc65a2263dca11a4902fa170155bc4581fda2 Craig Davey committed Apr 21, 2010
View
6 app/controllers/tolk/locales_controller.rb
@@ -10,11 +10,7 @@ def index
def show
respond_to do |format|
format.html do
- if params[:q].present?
- @phrases = @locale.search_phrases(params[:q], params[:page])
- else
- @phrases = @locale.phrases_without_translation(params[:page])
- end
+ @phrases = @locale.phrases_without_translation(params[:page])
end
format.atom { @phrases = @locale.phrases_without_translation(params[:page], :per_page => 50) }
format.yml { render :text => @locale.to_hash.ya2yaml(:syck_compatible => true) }
View
15 app/controllers/tolk/searches_controller.rb
@@ -0,0 +1,15 @@
+module Tolk
+ class SearchesController < Tolk::ApplicationController
+ before_filter :find_locale
+
+ def show
+ @phrases = @locale.search_phrases(params[:q], params[:page])
+ end
+
+ private
+
+ def find_locale
+ @locale = Tolk::Locale.find_by_name!(params[:locale])
+ end
+ end
+end
View
12 app/models/tolk/locale.rb
@@ -112,16 +112,12 @@ def phrases_without_translation(page = nil, options = {})
def search_phrases(query, page = nil, options = {})
return [] unless query.present?
-
- phrases = Tolk::Phrase.scoped(:order => 'tolk_phrases.key ASC')
- phrase_ids = Tolk::Locale.primary_locale.translations.all(:conditions => ["tolk_translations.text LIKE ?", "%#{query}%"], :select => 'tolk_translations.phrase_id').map(&:phrase_id).uniq
+ translations = Tolk::Locale.primary_locale.translations.all(:conditions => ["tolk_translations.text LIKE ?", "%#{query}%"])
- phrases = phrases.scoped(:conditions => ['tolk_phrases.id IN(?)', phrase_ids])
-
- result = phrases.paginate({:page => page}.merge(options))
- Tolk::Phrase.send :preload_associations, result, :translations
- result
+ phrases = Tolk::Phrase.scoped(:order => 'tolk_phrases.key ASC')
+ phrases = phrases.scoped(:conditions => ['tolk_phrases.id IN(?)', translations.map(&:phrase_id).uniq])
+ phrases.paginate({:page => page}.merge(options))
end
def search_phrases_without_translation(query, page = nil, options = {})
View
55 app/views/tolk/locales/all.html.erb
@@ -7,6 +7,15 @@
<span class="notice">Some phrases have changed. <%= link_to "Update translations", updated_tolk_locale_path(@locale) %>.</span>
<% end %>
+<div class="search">
+ <% form_tag "/tolk/search", :method => :get do %>
+ <%= hidden_field_tag :locale, @locale.name %>
+ Search for an English phrase:
+ <%= text_field_tag :q, params[:q] %>
+ <%= submit_tag "Search", :name => nil %>
+ <% end %>
+</div>
+
<div class="translations">
<% if @phrases.any? %>
<% form_for @locale do |locale_form| %>
@@ -16,31 +25,33 @@
<th class="key"><%= Tolk::Locale.primary_language_name -%></th>
</tr>
<% @phrases.each do |phrase| %>
- <tr>
- <td class="translation">
- <%= hidden_field_tag :"translations[][id]", phrase.translation.id %>
- <%= hidden_field_tag :"translations[][locale_id]", phrase.translation.locale_id %>
- <%= text_area_tag :"translations[][text]", format_i18n_text_area_value(phrase.translation.text), :class => 'locale',
- :onfocus => "$(this).up('tr').addClassName('active');", :onblur => "$(this).up('tr').removeClassName('active');" %>
- </td>
- <td class="phrase">
+ <% if phrase.translations.primary %>
+ <tr>
+ <td class="translation">
+ <%= hidden_field_tag :"translations[][id]", phrase.translation.id %>
+ <%= hidden_field_tag :"translations[][locale_id]", phrase.translation.locale_id %>
+ <%= text_area_tag :"translations[][text]", format_i18n_text_area_value(phrase.translation.text), :class => 'locale',
+ :onfocus => "$(this).up('tr').addClassName('active');", :onblur => "$(this).up('tr').removeClassName('active');" %>
+ </td>
+ <td class="phrase">
- <% if action_name == 'updated' %>
- <div class="updated">
- <span class="key">Updated</span>
+ <% if action_name == 'updated' %>
+ <div class="updated">
+ <span class="key">Updated</span>
+ <%= format_i18n_value(phrase.translations.primary.text) -%>
+ </div>
+ <div class="original">
+ <span class="key">Original</span>
+ <%= format_i18n_value(phrase.translations.primary.previous_text) -%>
+ </div>
+ <% else %>
<%= format_i18n_value(phrase.translations.primary.text) -%>
- </div>
- <div class="original">
- <span class="key">Original</span>
- <%= format_i18n_value(phrase.translations.primary.previous_text) -%>
- </div>
- <% else %>
- <%= format_i18n_value(phrase.translations.primary.text) -%>
- <% end %>
+ <% end %>
- <span class="key"><%= phrase.key %></span>
- </td>
- </tr>
+ <span class="key"><%= phrase.key %></span>
+ </td>
+ </tr>
+ <% end %>
<% end %>
</table>
<div class="table_submit">
View
7 app/views/tolk/locales/show.html.erb
@@ -12,13 +12,14 @@
<span class="notice">Some phrases have changed. <%= link_to "Update translations", updated_tolk_locale_path(@locale) %>.</span>
<% end %>
-<h4 class="search">
- <% form_tag @locale, :method => :get do %>
+<div class="search">
+ <% form_tag "/tolk/search", :method => :get do %>
+ <%= hidden_field_tag :locale, @locale.name %>
Search for an English phrase:
<%= text_field_tag :q, params[:q] %>
<%= submit_tag "Search", :name => nil %>
<% end %>
-</h4>
+</div>
<div class="translations">
<% if @phrases.any? %>
View
60 app/views/tolk/searches/show.html.erb
@@ -0,0 +1,60 @@
+<% content_for :locale do %>
+ <span class="locale"><%= @locale.language_name %></span>
+<% end %>
+
+<h3 class="search">
+ <% form_tag "/tolk/search", :method => :get do %>
+ <%= hidden_field_tag :locale, @locale.name %>
+ Search for an English phrase:
+ <%= text_field_tag :q, params[:q] %>
+ <%= submit_tag "Search", :name => nil %>
+ <% end %>
+</h3>
+
+<div class="search_exits">
+ <%= link_to "Phrases missing translation", @locale %>
+ &nbsp;
+ <%= link_to "Completed translations", all_tolk_locale_path(@locale) %>
+</div>
+
+<div class="translations">
+ <% if @phrases.any? %>
+ <% form_for @locale do |locale_form| %>
+ <table class="translations">
+ <tr>
+ <th class="translation"><%= @locale.language_name -%></th>
+ <th class="key"><%= Tolk::Locale.primary_language_name -%></th>
+ </tr>
+ <% @phrases.each do |phrase| %>
+
+ <% if translation = phrase.translations.find_by_locale_id(@locale.id) || Tolk::Translation.new(:locale => @locale, :phrase => phrase) %>
+ <tr>
+ <td class="translation">
+ <%= hidden_field_tag :"translations[][id]", translation.id, :id => "#{translation.object_id}_id" %>
+ <%= hidden_field_tag :"translations[][phrase_id]", phrase.id, :id => "#{translation.object_id}_phrase_id" %>
+ <%= hidden_field_tag :"translations[][locale_id]", translation.locale_id, :id => "#{translation.object_id}_locale_id" %>
+ <%= text_area_tag :"translations[][text]", translation.text, :class => "locale", :id => "#{translation.object_id}_text", :onfocus => "$(this).up('tr').addClassName('active');", :onblur => "$(this).up('tr').removeClassName('active');" %>
+ </td>
+ <td class="phrase">
+ <% if params[:q].present? -%>
+ <%= highlight(format_i18n_value(phrase.translations.primary.text), params[:q]) -%>
+ <% else -%>
+ <%= format_i18n_value(phrase.translations.primary.text) -%>
+ <% end -%>
+ <span class="key" title="<%= phrase.key %>"><%= truncate(phrase.key, :length => 100) %></span>
+ </td>
+ </tr>
+ <% end %>
+ <% end %>
+ </table>
+ <div class="table_submit">
+ <p><%= locale_form.submit "Save changes" %></p>
+ </div>
+ <% end %>
+ <div class="paginate">
+ <%= will_paginate @phrases, :renderer => Tolk::LinkRenderer %>
+ </div>
+ <% else %>
+ <p style="text-align: left">No search results.</p>
+ <% end %>
+</div>
View
1 config/routes.rb
@@ -2,5 +2,6 @@
map.namespace('tolk') do |tolk|
tolk.root :controller => 'locales'
tolk.resources :locales, :member => {:all => :get, :updated => :get}
+ tolk.resource :search
end
end

0 comments on commit cd7dc65

Please sign in to comment.