Commit 331957a66eadb2596d746cb3e3fe10f6c4ae910a

Authored by AntonioTerceiro
1 parent 0a4543de

ActionItem135: refactoring: moving partial_for_hit in SearchHelper to partial_fo…

…r_class in ApplicationHelper. Added a test.



git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1123 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/helpers/application_helper.rb
... ... @@ -272,4 +272,14 @@ module ApplicationHelper
272 272 return _('No product') unless product
273 273 link_to product.name, :controller => 'catalog', :action => 'show', :id => product, :profile => product.enterprise.identifier
274 274 end
  275 +
  276 + def partial_for_class(klass)
  277 + name = klass.name.underscore
  278 + if File.exists?(File.join(RAILS_ROOT, 'app', 'views', params[:controller], "_#{name}.rhtml"))
  279 + name
  280 + else
  281 + partial_for_class(klass.superclass)
  282 + end
  283 + end
  284 +
275 285 end
... ...
app/helpers/search_helper.rb
1 1 module SearchHelper
2 2  
3   - def partial_for_hit(klass)
4   - name = klass.name.underscore
5   - if File.exists?(File.join(RAILS_ROOT, 'app', 'views', 'search', "_#{name}.rhtml"))
6   - name
7   - else
8   - partial_for_hit(klass.superclass)
9   - end
10   - end
11   -
12 3 def relevance_for(hit)
13 4 n = (hit.ferret_score if hit.respond_to?(:ferret_score))
14 5 n ||= 1.0
... ...
app/views/search/index.rhtml
1 1 <h2> <%= _('Search results for "%s"') % @query %> </h2>
2 2  
3 3 <% @results.each do |hit| %>
4   - <%= render :partial => partial_for_hit(hit.class), :locals => { :hit => hit } %>
  4 + <%= render :partial => partial_for_class(hit.class), :locals => { :hit => hit } %>
5 5 <div class='search-relevance'>
6 6 <%= _('Relevance: %d%%') % relevance_for(hit) %>
7 7 </div>
... ...
app/views/search/tag.rhtml
... ... @@ -8,7 +8,7 @@
8 8  
9 9 <div class='search-tagged-items'>
10 10 <% @tagged.each do |hit| %>
11   - <%= render :partial => partial_for_hit(hit.class), :locals => { :hit => hit } %>
  11 + <%= render :partial => partial_for_class(hit.class), :locals => { :hit => hit } %>
12 12 <br style='clear: left;'/>
13 13 <% end %>
14 14 </div>
... ...
test/unit/application_helper_test.rb
... ... @@ -4,8 +4,16 @@ class ApplicationHelperTest &lt; Test::Unit::TestCase
4 4  
5 5 include ApplicationHelper
6 6  
7   - should 'generate control panel buttons' do
  7 + should 'calculate correctly partial for object' do
  8 + self.stubs(:params).returns({:controller => 'test'})
8 9  
  10 + File.expects(:exists?).with("#{RAILS_ROOT}/app/views/test/_float.rhtml").returns(false)
  11 + File.expects(:exists?).with("#{RAILS_ROOT}/app/views/test/_numeric.rhtml").returns(true).times(2)
  12 + File.expects(:exists?).with("#{RAILS_ROOT}/app/views/test/_runtime_error.rhtml").returns(true).at_least_once
  13 +
  14 + assert_equal 'numeric', partial_for_class(Float)
  15 + assert_equal 'numeric', partial_for_class(Numeric)
  16 + assert_equal 'runtime_error', partial_for_class(RuntimeError)
9 17 end
10 18  
11 19 end
... ...