Commit 331957a66eadb2596d746cb3e3fe10f6c4ae910a
1 parent
0a4543de
Exists in
master
and in
29 other branches
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
Showing
5 changed files
with
21 additions
and
12 deletions
Show diff stats
app/helpers/application_helper.rb
@@ -272,4 +272,14 @@ module ApplicationHelper | @@ -272,4 +272,14 @@ module ApplicationHelper | ||
272 | return _('No product') unless product | 272 | return _('No product') unless product |
273 | link_to product.name, :controller => 'catalog', :action => 'show', :id => product, :profile => product.enterprise.identifier | 273 | link_to product.name, :controller => 'catalog', :action => 'show', :id => product, :profile => product.enterprise.identifier |
274 | end | 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 | end | 285 | end |
app/helpers/search_helper.rb
1 | module SearchHelper | 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 | def relevance_for(hit) | 3 | def relevance_for(hit) |
13 | n = (hit.ferret_score if hit.respond_to?(:ferret_score)) | 4 | n = (hit.ferret_score if hit.respond_to?(:ferret_score)) |
14 | n ||= 1.0 | 5 | n ||= 1.0 |
app/views/search/index.rhtml
1 | <h2> <%= _('Search results for "%s"') % @query %> </h2> | 1 | <h2> <%= _('Search results for "%s"') % @query %> </h2> |
2 | 2 | ||
3 | <% @results.each do |hit| %> | 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 | <div class='search-relevance'> | 5 | <div class='search-relevance'> |
6 | <%= _('Relevance: %d%%') % relevance_for(hit) %> | 6 | <%= _('Relevance: %d%%') % relevance_for(hit) %> |
7 | </div> | 7 | </div> |
app/views/search/tag.rhtml
@@ -8,7 +8,7 @@ | @@ -8,7 +8,7 @@ | ||
8 | 8 | ||
9 | <div class='search-tagged-items'> | 9 | <div class='search-tagged-items'> |
10 | <% @tagged.each do |hit| %> | 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 | <br style='clear: left;'/> | 12 | <br style='clear: left;'/> |
13 | <% end %> | 13 | <% end %> |
14 | </div> | 14 | </div> |
test/unit/application_helper_test.rb
@@ -4,8 +4,16 @@ class ApplicationHelperTest < Test::Unit::TestCase | @@ -4,8 +4,16 @@ class ApplicationHelperTest < Test::Unit::TestCase | ||
4 | 4 | ||
5 | include ApplicationHelper | 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 | end | 17 | end |
10 | 18 | ||
11 | end | 19 | end |