From 4ae1f84f642f4c0309cdb144c28deadc95abe3c2 Mon Sep 17 00:00:00 2001 From: AntonioTerceiro Date: Tue, 1 Apr 2008 20:46:16 +0000 Subject: [PATCH] ActionItem155: refactoring: moving functionality from category_controller to search_controller, and removing the former. --- app/controllers/public/category_controller.rb | 27 --------------------------- app/controllers/public/search_controller.rb | 19 +++++++++++++++++++ app/helpers/application_helper.rb | 2 +- app/views/category/_article.rhtml | 9 --------- app/views/category/_category.rhtml | 24 ------------------------ app/views/category/_product_category.rhtml | 29 ----------------------------- app/views/category/_region.rhtml | 1 - app/views/category/view.rhtml | 29 ----------------------------- app/views/search/_article.rhtml | 19 ++++++++++--------- app/views/search/_category.rhtml | 24 ++++++++++++++++++++++++ app/views/search/_comment.rhtml | 16 +++++++--------- app/views/search/_product.rhtml | 2 ++ app/views/search/_product_category.rhtml | 29 +++++++++++++++++++++++++++++ app/views/search/_profile.rhtml | 15 ++++++++------- app/views/search/_region.rhtml | 1 + app/views/search/category_index.rhtml | 29 +++++++++++++++++++++++++++++ app/views/search/index.rhtml | 2 ++ app/views/shared/categories_menu.rhtml | 2 +- config/routes.rb | 7 +++---- test/functional/category_controller_test.rb | 61 ------------------------------------------------------------- test/functional/search_controller_test.rb | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++------------ test/integration/routing_test.rb | 7 +++++-- test/unit/application_helper_test.rb | 2 +- 23 files changed, 195 insertions(+), 226 deletions(-) delete mode 100644 app/controllers/public/category_controller.rb delete mode 100644 app/views/category/_article.rhtml delete mode 100644 app/views/category/_category.rhtml delete mode 100644 app/views/category/_product_category.rhtml delete mode 120000 app/views/category/_region.rhtml delete mode 100644 app/views/category/view.rhtml create mode 100644 app/views/search/_category.rhtml create mode 100644 app/views/search/_product_category.rhtml create mode 120000 app/views/search/_region.rhtml create mode 100644 app/views/search/category_index.rhtml delete mode 100644 test/functional/category_controller_test.rb diff --git a/app/controllers/public/category_controller.rb b/app/controllers/public/category_controller.rb deleted file mode 100644 index 7126539..0000000 --- a/app/controllers/public/category_controller.rb +++ /dev/null @@ -1,27 +0,0 @@ -class CategoryController < PublicController - - # view the summary of one category - def view - send('action_' + @category.class.name.underscore) - end - - attr_reader :category - - before_filter :load_category, :only => [ :view ] - private - - def action_product_category - @products = category.products - @enterprises = category.products.map{|p| p.enterprise}.flatten.uniq - @users = category.consumers - end - - def action_category - # TODO: load articles, documents, etc so the view can list them. - @recent_articles = category.recent_articles - @recent_comments = category.recent_comments - @most_commented_articles = category.most_commented_articles - end - alias :action_region :action_category - -end diff --git a/app/controllers/public/search_controller.rb b/app/controllers/public/search_controller.rb index 95ceb69..cd20415 100644 --- a/app/controllers/public/search_controller.rb +++ b/app/controllers/public/search_controller.rb @@ -24,6 +24,19 @@ class SearchController < ApplicationController end end + def action_product_category + @products = category.products + @enterprises = category.products.map{|p| p.enterprise}.flatten.uniq + @users = category.consumers + end + + def action_category + @recent_articles = category.recent_articles + @recent_comments = category.recent_comments + @most_commented_articles = category.most_commented_articles + end + alias :action_region :action_category + public include SearchHelper @@ -53,6 +66,12 @@ class SearchController < ApplicationController ####################################################### + # view the summary of one category + def category_index + send('action_' + @category.class.name.underscore) + end + attr_reader :category + def tags @tags = Tag.find(:all).inject({}) do |memo,tag| memo[tag.name] = tag.taggings.count diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 7a5629d..8520dc0 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -309,7 +309,7 @@ module ApplicationHelper def link_to_category(category, full = true) return _('Uncategorized product') unless category name = full ? category.full_name : category.name - link_to name, :controller => 'category', :action => 'view', :category_path => category.path.split('/') + link_to name, :controller => 'search', :action => 'category_index', :category_path => category.path.split('/') end def link_to_product(product) diff --git a/app/views/category/_article.rhtml b/app/views/category/_article.rhtml deleted file mode 100644 index 6f7c9e0..0000000 --- a/app/views/category/_article.rhtml +++ /dev/null @@ -1,9 +0,0 @@ -
  • - <%= link_to(article.title, article.url) %> -
    - <%= - _('by %s') % link_to(article.last_changed_by.name, article.last_changed_by.url) - %> - <%= _('Last update: %s.') % show_date(article.updated_on) %> -
    -
  • diff --git a/app/views/category/_category.rhtml b/app/views/category/_category.rhtml deleted file mode 100644 index ae00189..0000000 --- a/app/views/category/_category.rhtml +++ /dev/null @@ -1,24 +0,0 @@ -

    <%= _('Recent articles') %>

    - - -

    <%= _('Recent Comments') %>

    - - -

    <%= _('Most commented articles') %>

    - diff --git a/app/views/category/_product_category.rhtml b/app/views/category/_product_category.rhtml deleted file mode 100644 index 1d9d887..0000000 --- a/app/views/category/_product_category.rhtml +++ /dev/null @@ -1,29 +0,0 @@ -<%= link_to(_('I consume this'), {:profile => current_user.login, :controller => 'consumed_products', :action => 'new', 'consumption[product_category_id]' => @category.id }, :method => :post) if logged_in? %> - - -

    <%= _('Products under this category:') %>

    - - -

    <%= _('Users that consumes products in this category:') %>

    - -<% @users.each do |u| %> -

    - <%= image_tag u.image.public_filename(:thumb) if u.image %>
    - <%= link_to_homepage(u.name, u.identifier) %> -

    -<% end %> diff --git a/app/views/category/_region.rhtml b/app/views/category/_region.rhtml deleted file mode 120000 index 859d4d2..0000000 --- a/app/views/category/_region.rhtml +++ /dev/null @@ -1 +0,0 @@ -_category.rhtml \ No newline at end of file diff --git a/app/views/category/view.rhtml b/app/views/category/view.rhtml deleted file mode 100644 index 25465d1..0000000 --- a/app/views/category/view.rhtml +++ /dev/null @@ -1,29 +0,0 @@ -
    - -
    - <%= @category.ancestors.reverse.map { |a| link_to_category(a, false) }.join(' → ') %> -
    - -

    <%= _('Category: %s') % @category.name %>

    - -
    - <%= render :partial => @category.class.name.underscore %> -
    - -
    -

    <%= _('Sub-categories') %>

    - - <% if @category.children.empty? %> - <%= _('No sub-categories') %> - <% else %> -
      - <% @category.children.each do |c| %> -
    • <%= link_to_category(c) %>
    • - <% end %> -
    - <% end %> - -
    - -
    - diff --git a/app/views/search/_article.rhtml b/app/views/search/_article.rhtml index 92dc933..c6c9799 100644 --- a/app/views/search/_article.rhtml +++ b/app/views/search/_article.rhtml @@ -1,10 +1,11 @@ -
    -
    - - <%= link_to_document(hit) %> - -
    -
    - <%= strip_tags(hit.abstract) %> -
    +
  • + <%= link_to(hit.title, hit.url) %> +
    + <% if hit.last_changed_by %> + + <%= _('by %s') % link_to(hit.last_changed_by.name, hit.last_changed_by.url) %> + + <% end %> + <%= _('Last update: %s.') % show_date(hit.updated_on) %>
    +
  • diff --git a/app/views/search/_category.rhtml b/app/views/search/_category.rhtml new file mode 100644 index 0000000..ae00189 --- /dev/null +++ b/app/views/search/_category.rhtml @@ -0,0 +1,24 @@ +

    <%= _('Recent articles') %>

    + + +

    <%= _('Recent Comments') %>

    + + +

    <%= _('Most commented articles') %>

    + diff --git a/app/views/search/_comment.rhtml b/app/views/search/_comment.rhtml index f8563f2..ebc500c 100644 --- a/app/views/search/_comment.rhtml +++ b/app/views/search/_comment.rhtml @@ -1,10 +1,8 @@ -
    -
    - - <%= link_to(hit.title, hit.url) %> - -
    -
    - <%= strip_tags(hit.body) %> -
    +
  • + <%= link_to(hit.title, hit.url)%> +
    + <%= _('by %s, on "%s"') % [hit.author_name, link_to(hit.article.name, hit.article.url)] %>
    +
  • + + diff --git a/app/views/search/_product.rhtml b/app/views/search/_product.rhtml index 1dc4678..aa1a226 100644 --- a/app/views/search/_product.rhtml +++ b/app/views/search/_product.rhtml @@ -1,5 +1,6 @@ <%# FIXME add more information %> +
  • <%= image_tag(hit.image.public_filename(:minor)) if hit.image %> @@ -13,3 +14,4 @@
  • <%= _('Category: %s') % link_to_category(hit.product_category) %>
  • + diff --git a/app/views/search/_product_category.rhtml b/app/views/search/_product_category.rhtml new file mode 100644 index 0000000..1d9d887 --- /dev/null +++ b/app/views/search/_product_category.rhtml @@ -0,0 +1,29 @@ +<%= link_to(_('I consume this'), {:profile => current_user.login, :controller => 'consumed_products', :action => 'new', 'consumption[product_category_id]' => @category.id }, :method => :post) if logged_in? %> +
      +
    • <%= n_("One product in this category.", "%{num} products in this category.", @products.size) % { :num => @products.size } %>
    • +
    • <%= n_("One enterprise sells products in this category.", "%{num} enterprises sells products in this category.", @enterprises.size) % { :num => @enterprises.size} %>
    • +
    • <%= n_("One user consumes this product.", "%{num} users consumes this product", @users.size) % {:num => @users.size} %>
    • +
    + +

    <%= _('Products under this category:') %>

    +
      + <% @products.each do |p| %> +
    • + <%= image_tag p.image.public_filename(:thumb) if p.image%> + <%= link_to_product(p) %>
      + <% if p.price %> + <%= _('Price: %d') % p.price %>
      + <% end %> + <%= _('Enterprise:') %> <%= link_to_homepage p.enterprise.name, p.enterprise.identifier %>

      +
    • + <% end %> +
    + +

    <%= _('Users that consumes products in this category:') %>

    + +<% @users.each do |u| %> +

    + <%= image_tag u.image.public_filename(:thumb) if u.image %>
    + <%= link_to_homepage(u.name, u.identifier) %> +

    +<% end %> diff --git a/app/views/search/_profile.rhtml b/app/views/search/_profile.rhtml index 0d21f7f..fca8153 100644 --- a/app/views/search/_profile.rhtml +++ b/app/views/search/_profile.rhtml @@ -1,8 +1,9 @@ <%# FIXME add more information %> - -
    - <%= image_tag(hit.image.public_filename(:minor)) if hit.image %> - - <%= link_to_homepage(hit.name, hit.identifier) %> - -
    +
  • +
    + <%= image_tag(hit.image.public_filename(:minor)) if hit.image %> + + <%= link_to_homepage(hit.name, hit.identifier) %> + +
    +
  • diff --git a/app/views/search/_region.rhtml b/app/views/search/_region.rhtml new file mode 120000 index 0000000..859d4d2 --- /dev/null +++ b/app/views/search/_region.rhtml @@ -0,0 +1 @@ +_category.rhtml \ No newline at end of file diff --git a/app/views/search/category_index.rhtml b/app/views/search/category_index.rhtml new file mode 100644 index 0000000..25465d1 --- /dev/null +++ b/app/views/search/category_index.rhtml @@ -0,0 +1,29 @@ +
    + +
    + <%= @category.ancestors.reverse.map { |a| link_to_category(a, false) }.join(' → ') %> +
    + +

    <%= _('Category: %s') % @category.name %>

    + +
    + <%= render :partial => @category.class.name.underscore %> +
    + +
    +

    <%= _('Sub-categories') %>

    + + <% if @category.children.empty? %> + <%= _('No sub-categories') %> + <% else %> +
      + <% @category.children.each do |c| %> +
    • <%= link_to_category(c) %>
    • + <% end %> +
    + <% end %> + +
    + +
    + diff --git a/app/views/search/index.rhtml b/app/views/search/index.rhtml index 3b5e892..7f2fee1 100644 --- a/app/views/search/index.rhtml +++ b/app/views/search/index.rhtml @@ -4,9 +4,11 @@ <% if !results.nil? and !results.empty? %>

    <%= @names[name] %>

    +
      <% results.each do |hit| %> <%= render :partial => partial_for_class(hit.class), :locals => { :hit => hit } %> <% end %> +
    <% end %> <% end %> diff --git a/app/views/shared/categories_menu.rhtml b/app/views/shared/categories_menu.rhtml index 7a34bc7..7b1c596 100644 --- a/app/views/shared/categories_menu.rhtml +++ b/app/views/shared/categories_menu.rhtml @@ -5,7 +5,7 @@ <%= item.name %>
      <% item.children.each do |child| %> -
    • <%= link_to(content_tag('span', child.name), :controller => 'category', :action => 'view', :category_path => child.explode_path) %>
    • +
    • <%= link_to(content_tag('span', child.name), :controller => 'search', :action => 'category_index', :category_path => child.explode_path) %>
    • <% end %>
    diff --git a/config/routes.rb b/config/routes.rb index 1cfe8f6..a10d0e9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -29,13 +29,12 @@ ActionController::Routing::Routes.draw do |map| # tags map.tag 'tag', :controller => 'search', :action => 'tags' map.tag 'tag/:tag', :controller => 'search', :action => 'tag' - + # categories index + map.category 'cat/*category_path', :controller => 'search', :action => 'category_index' + map.assets 'assets/:asset/*category_path', :controller => 'search', :action => 'assets' # search map.connect 'search/:action/*category_path', :controller => 'search' - # categories controller - map.connect 'cat', :controller => 'category', :action => 'index' - map.category 'cat/*category_path', :controller => 'category', :action => 'view' # public profile information map.profile 'profile/:profile/:action/:id', :controller => 'profile', :action => 'index' diff --git a/test/functional/category_controller_test.rb b/test/functional/category_controller_test.rb deleted file mode 100644 index b14212a..0000000 --- a/test/functional/category_controller_test.rb +++ /dev/null @@ -1,61 +0,0 @@ -require File.dirname(__FILE__) + '/../test_helper' -require 'category_controller' - -# Re-raise errors caught by the controller. -class CategoryController; def rescue_action(e) raise e end; end - -class CategoryControllerTest < Test::Unit::TestCase - def setup - @controller = CategoryController.new - @request = ActionController::TestRequest.new - @response = ActionController::TestResponse.new - - @category = Category.create!(:name => 'my category', :environment => Environment.default) - end - - def test_should_display_a_given_category - get :view, :category_path => [ 'my-category' ] - assert_equal @category, assigns(:category) - end - - should 'expose category in a method' do - get :view, :category_path => [ 'my-category' ] - assert_same assigns(:category), @controller.category - end - - should 'list recent articles in the category' do - @controller.expects(:category).returns(@category).at_least_once - recent = [] - @category.expects(:recent_articles).returns(recent) - - get :view, :category_path => [ 'my-category' ] - assert_same recent, assigns(:recent_articles) - end - - should 'list recent comments in the category' do - @controller.expects(:category).returns(@category).at_least_once - recent = [] - @category.expects(:recent_comments).returns(recent) - - get :view, :category_path => [ 'my-category' ] - assert_same recent, assigns(:recent_comments) - end - - should 'list most commented articles in the category' do - @controller.expects(:category).returns(@category).at_least_once - most_commented = [] - @category.expects(:most_commented_articles).returns(most_commented) - - get :view, :category_path => [ 'my-category' ] - assert_same most_commented, assigns(:most_commented_articles) - end - - should 'display category of products' do - cat = ProductCategory.create!(:name => 'Food', :environment => Environment.default) - ent = Enterprise.create!(:name => 'Enterprise test', :identifier => 'enterprise_test') - p = cat.products.create!(:name => 'product test', :enterprise => ent) - get :view, :category_path => cat.path.split('/') - assert_includes assigns(:products), p - end - -end diff --git a/test/functional/search_controller_test.rb b/test/functional/search_controller_test.rb index ec64992..7f2cc27 100644 --- a/test/functional/search_controller_test.rb +++ b/test/functional/search_controller_test.rb @@ -9,6 +9,8 @@ class SearchControllerTest < Test::Unit::TestCase @controller = SearchController.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new + + @category = Category.create!(:name => 'my category', :environment => Environment.default) end should 'filter stop words' do @@ -52,11 +54,10 @@ class SearchControllerTest < Test::Unit::TestCase should 'search for articles in a specific category' do person = create_user('teste').person - category = Category.create!(:name => 'my category', :environment => Environment.default) # in category art1 = person.articles.build(:name => 'an article to be found') - art1.categories << category + art1.categories << @category art1.save! # not in category @@ -83,11 +84,10 @@ class SearchControllerTest < Test::Unit::TestCase should 'search in comments in a specific category' do person = create_user('teste').person - category = Category.create!(:name => 'my category', :environment => Environment.default) # in category art1 = person.articles.build(:name => 'an article to be found') - art1.categories << category + art1.categories << @category art1.save! comment1 = art1.comments.build(:title => 'comment to be found', :body => 'hfyfyh', :author => person); comment1.save! @@ -108,10 +108,9 @@ class SearchControllerTest < Test::Unit::TestCase end should 'find enterprises in a specified category' do - category = Category.create!(:name => 'my category', :environment => Environment.default) # in category - ent1 = Enterprise.create!(:name => 'testing enterprise 1', :identifier => 'test1', :categories => [category]) + ent1 = Enterprise.create!(:name => 'testing enterprise 1', :identifier => 'test1', :categories => [@category]) # not in category ent2 = Enterprise.create!(:name => 'testing enterprise 2', :identifier => 'test2') @@ -132,8 +131,7 @@ class SearchControllerTest < Test::Unit::TestCase end should 'find people in a specific category' do - c = Category.create!(:name => 'my category', :environment => Environment.default) - p1 = create_user('people_1').person; p1.name = 'a beautiful person'; p1.categories << c; p1.save! + p1 = create_user('people_1').person; p1.name = 'a beautiful person'; p1.categories << @category; p1.save! p2 = create_user('people_2').person; p2.name = 'another beautiful person'; p2.save! get :index, :category_path => [ 'my-category' ], :query => 'beautiful', :find_in => [ 'people' ] assert_includes assigns(:results)[:people], p1 @@ -150,10 +148,9 @@ class SearchControllerTest < Test::Unit::TestCase end should 'find communities in a specified category' do - c = Category.create!(:name => 'my category', :environment => Environment.default) c1 = Community.create!(:name => 'a beautiful community', :identifier => 'bea_comm', :environment => Environment.default) c2 = Community.create!(:name => 'another beautiful community', :identifier => 'an_bea_comm', :environment => Environment.default) - c1.categories << c; c1.save! + c1.categories << @category; c1.save! get :index, :category_path => [ 'my-category' ], :query => 'beautiful', :find_in => [ 'communities' ] assert_includes assigns(:results)[:communities], c1 assert_not_includes assigns(:results)[:communities], c2 @@ -169,8 +166,7 @@ class SearchControllerTest < Test::Unit::TestCase end should 'find products in a specific category' do - c = Category.create!(:name => 'my category', :environment => Environment.default) - ent1 = Enterprise.create!(:name => 'teste1', :identifier => 'teste1'); ent1.categories << c + ent1 = Enterprise.create!(:name => 'teste1', :identifier => 'teste1'); ent1.categories << @category ent2 = Enterprise.create!(:name => 'teste2', :identifier => 'teste2') prod1 = ent1.products.create!(:name => 'a beautiful product') prod2 = ent2.products.create!(:name => 'another beautiful product') @@ -261,4 +257,49 @@ class SearchControllerTest < Test::Unit::TestCase assert_tag :tag => 'form' , :attributes => { :method => 'get' } end + def test_should_display_a_given_category + get :category_index, :category_path => [ 'my-category' ] + assert_equal @category, assigns(:category) + end + + should 'expose category in a method' do + get :category_index, :category_path => [ 'my-category' ] + assert_same assigns(:category), @controller.category + end + + should 'list recent articles in the category' do + @controller.expects(:category).returns(@category).at_least_once + recent = [] + @category.expects(:recent_articles).returns(recent) + + get :category_index, :category_path => [ 'my-category' ] + assert_same recent, assigns(:recent_articles) + end + + should 'list recent comments in the category' do + @controller.expects(:category).returns(@category).at_least_once + recent = [] + @category.expects(:recent_comments).returns(recent) + + get :category_index, :category_path => [ 'my-category' ] + assert_same recent, assigns(:recent_comments) + end + + should 'list most commented articles in the category' do + @controller.expects(:category).returns(@category).at_least_once + most_commented = [] + @category.expects(:most_commented_articles).returns(most_commented) + + get :category_index, :category_path => [ 'my-category' ] + assert_same most_commented, assigns(:most_commented_articles) + end + + should 'display category of products' do + cat = ProductCategory.create!(:name => 'Food', :environment => Environment.default) + ent = Enterprise.create!(:name => 'Enterprise test', :identifier => 'enterprise_test') + p = cat.products.create!(:name => 'product test', :enterprise => ent) + get :category_index, :category_path => cat.path.split('/') + assert_includes assigns(:products), p + end + end diff --git a/test/integration/routing_test.rb b/test/integration/routing_test.rb index d31be90..70aa7d6 100644 --- a/test/integration/routing_test.rb +++ b/test/integration/routing_test.rb @@ -69,8 +69,7 @@ class RoutingTest < ActionController::IntegrationTest end def test_category_browser - assert_routing('/cat/products/eletronics', :controller => 'category', :action => 'view', :category_path => [ 'products', 'eletronics']) - assert_routing('/cat', :controller => 'category', :action => 'index') + assert_routing('/cat/products/eletronics', :controller => 'search', :action => 'category_index', :category_path => [ 'products', 'eletronics']) end #FIXME remove this if design_blocks is not going to be used; or uncomment otherwise; @@ -99,4 +98,8 @@ class RoutingTest < ActionController::IntegrationTest assert_routing('/search/filter/a/b', :controller => 'search', :action => 'filter', :category_path => ['a','b']) end + def test_assets_routing + assert_routing('/assets/my-asset/a/b/c', :controller => 'search', :action => 'assets', :asset => 'my-asset', :category_path => ['a', 'b', 'c']) + end + end diff --git a/test/unit/application_helper_test.rb b/test/unit/application_helper_test.rb index 26cee55..542098f 100644 --- a/test/unit/application_helper_test.rb +++ b/test/unit/application_helper_test.rb @@ -67,7 +67,7 @@ class ApplicationHelperTest < Test::Unit::TestCase cat.expects(:full_name).returns('category name') result = "/cat/my-category/my-subcatagory" - expects(:link_to).with('category name', :controller => 'category', :action => 'view', :category_path => ['my-category', 'my-subcatagory']).returns(result) + expects(:link_to).with('category name', :controller => 'search', :action => 'category_index', :category_path => ['my-category', 'my-subcatagory']).returns(result) assert_same result, link_to_category(cat) end -- libgit2 0.21.2