Commit 59b729e5d546296f4cad7f74e20adaeb39144469
Committed by
Joenio Costa
1 parent
aedeaf61
Exists in
master
and in
23 other branches
The admin user should manage the region and the product category on manage category interface.
(ActionItem1582)
Showing
9 changed files
with
118 additions
and
13 deletions
Show diff stats
app/controllers/admin/categories_controller.rb
| @@ -5,9 +5,9 @@ class CategoriesController < AdminController | @@ -5,9 +5,9 @@ class CategoriesController < AdminController | ||
| 5 | helper :categories | 5 | helper :categories |
| 6 | 6 | ||
| 7 | def index | 7 | def index |
| 8 | - # WORKAROUND: restricting the category trees to display. Region and | ||
| 9 | - # ProductCategory have VERY LARGE trees. | ||
| 10 | @categories = environment.categories.find(:all, :conditions => "parent_id is null AND type is null") | 8 | @categories = environment.categories.find(:all, :conditions => "parent_id is null AND type is null") |
| 9 | + @regions = environment.regions.find(:all, :conditions => {:parent_id => nil}) | ||
| 10 | + @product_categories = environment.product_categories.find(:all, :conditions => {:parent_id => nil}) | ||
| 11 | end | 11 | end |
| 12 | 12 | ||
| 13 | ALLOWED_TYPES = CategoriesHelper::TYPES.map {|item| item[1] } | 13 | ALLOWED_TYPES = CategoriesHelper::TYPES.map {|item| item[1] } |
app/models/environment.rb
| @@ -147,6 +147,7 @@ class Environment < ActiveRecord::Base | @@ -147,6 +147,7 @@ class Environment < ActiveRecord::Base | ||
| 147 | has_many :categories | 147 | has_many :categories |
| 148 | has_many :display_categories, :class_name => 'Category', :conditions => 'display_color is not null and parent_id is null', :order => 'display_color' | 148 | has_many :display_categories, :class_name => 'Category', :conditions => 'display_color is not null and parent_id is null', :order => 'display_color' |
| 149 | 149 | ||
| 150 | + has_many :product_categories, :conditions => { :type => 'ProductCategory'} | ||
| 150 | has_many :regions | 151 | has_many :regions |
| 151 | 152 | ||
| 152 | has_many :roles | 153 | has_many :roles |
app/views/categories/_category.rhtml
| @@ -2,6 +2,12 @@ | @@ -2,6 +2,12 @@ | ||
| 2 | <div class='treeitem'> | 2 | <div class='treeitem'> |
| 3 | <%= display_color_for_category(category) %> | 3 | <%= display_color_for_category(category) %> |
| 4 | <%= category.name %> | 4 | <%= category.name %> |
| 5 | + <div class='button' id="show_button_<%= category.id %>"> | ||
| 6 | + <%= link_to_function(_('Show'), "['category_#{category.id}','hide_button_#{category.id}'].each(Element.show); Element.hide('show_button_#{category.id}');") unless category.children.empty? %> | ||
| 7 | + </div> | ||
| 8 | + <div class='button' id="hide_button_<%= category.id %>" style='display:none;'> | ||
| 9 | + <%= link_to_function(_('Hide'), "['category_#{category.id}','hide_button_#{category.id}'].each(Element.hide); Element.show('show_button_#{category.id}') ") %> | ||
| 10 | + </div> | ||
| 5 | 11 | ||
| 6 | <div> | 12 | <div> |
| 7 | <%= link_to _('Add subcategory'), :action => 'new', :parent_id => category %> | 13 | <%= link_to _('Add subcategory'), :action => 'new', :parent_id => category %> |
| @@ -10,11 +16,12 @@ | @@ -10,11 +16,12 @@ | ||
| 10 | </div> | 16 | </div> |
| 11 | </div> | 17 | </div> |
| 12 | 18 | ||
| 13 | - <% unless category.children.empty? %> | ||
| 14 | - <ul class='tree'> | ||
| 15 | - <%= render :partial => 'category', :collection => category.children %> | ||
| 16 | - </ul> | ||
| 17 | - <% end %> | 19 | + <div id="category_<%= category.id %>" style='display:none;'> |
| 20 | + <% unless category.children.empty? %> | ||
| 21 | + <ul class='tree'> | ||
| 22 | + <%= render :partial => 'category', :collection => category.children %> | ||
| 23 | + </ul> | ||
| 24 | + <% end %> | ||
| 25 | + </div> | ||
| 18 | 26 | ||
| 19 | </li> | 27 | </li> |
| 20 | - |
app/views/categories/index.rhtml
| @@ -3,8 +3,25 @@ | @@ -3,8 +3,25 @@ | ||
| 3 | <ul class='tree'> | 3 | <ul class='tree'> |
| 4 | <%= render :partial => 'category', :collection => @categories %> | 4 | <%= render :partial => 'category', :collection => @categories %> |
| 5 | </ul> | 5 | </ul> |
| 6 | - | ||
| 7 | <div> | 6 | <div> |
| 8 | <%= link_to _('New category'), :action => 'new' %> | 7 | <%= link_to _('New category'), :action => 'new' %> |
| 9 | </div> | 8 | </div> |
| 10 | 9 | ||
| 10 | +<h1><%= _('Product Categories') %></h1> | ||
| 11 | +<ul class='tree'> | ||
| 12 | + <%= render :partial => 'category', :collection => @product_categories %> | ||
| 13 | +</ul> | ||
| 14 | + | ||
| 15 | +<div> | ||
| 16 | + <%= link_to _('New category'), :action => 'new', :type => 'ProductCategory' %> | ||
| 17 | +</div> | ||
| 18 | + | ||
| 19 | +<h1><%= _('Regions') %></h1> | ||
| 20 | +<ul class='tree'> | ||
| 21 | + <%= render :partial => 'category', :collection => @regions %> | ||
| 22 | +</ul> | ||
| 23 | + | ||
| 24 | +<div> | ||
| 25 | + <%= link_to _('New category'), :action => 'new', :type => 'Region' %> | ||
| 26 | +</div> | ||
| 27 | + |
| @@ -0,0 +1,56 @@ | @@ -0,0 +1,56 @@ | ||
| 1 | +Feature: manage categories | ||
| 2 | + As an administrator | ||
| 3 | + I want to manage the categories | ||
| 4 | + | ||
| 5 | + Background: | ||
| 6 | + Given the following categories | ||
| 7 | + | name | display_in_menu | | ||
| 8 | + | Food | true | | ||
| 9 | + | Book | true | | ||
| 10 | + And the following categories | ||
| 11 | + | parent | name | display_in_menu | | ||
| 12 | + | Food | Vegetarian | true | | ||
| 13 | + | Food | Steak | true | | ||
| 14 | + Given I am logged in as admin | ||
| 15 | + | ||
| 16 | + @selenium | ||
| 17 | + Scenario: admin user could create a category | ||
| 18 | + Given I follow "Administration" | ||
| 19 | + And I follow "Manage Categories" | ||
| 20 | + And I follow "New category" | ||
| 21 | + When I fill in "Name" with "Category 1" | ||
| 22 | + And I press "Save" | ||
| 23 | + Then I should see "Categories" | ||
| 24 | + And I should see "Category 1" | ||
| 25 | + | ||
| 26 | + @selenium | ||
| 27 | + Scenario: admin user could see all the category tree | ||
| 28 | + Given I follow "Administration" | ||
| 29 | + And I follow "Manage Categories" | ||
| 30 | + When I follow "Show" | ||
| 31 | + Then I should see "Vegetarian" | ||
| 32 | + And I should see "Steak" | ||
| 33 | + | ||
| 34 | + @selenium | ||
| 35 | + Scenario: admin user could hide the category tree | ||
| 36 | + Given I follow "Administration" | ||
| 37 | + And I follow "Manage Categories" | ||
| 38 | + When I follow "Show" | ||
| 39 | + Then I should see "Vegetarian" | ||
| 40 | + And I should see "Steak" | ||
| 41 | + When I follow "Hide" | ||
| 42 | + Then I should not see "Vegetarian" | ||
| 43 | + And I should not see "Steak" | ||
| 44 | + | ||
| 45 | + @selenium | ||
| 46 | + Scenario: the show link is available just for categories with category tree | ||
| 47 | + Given the following category | ||
| 48 | + | parent | name | display_in_menu | | ||
| 49 | + | Steak | Pig | true | | ||
| 50 | + And I follow "Administration" | ||
| 51 | + And I follow "Manage Categories" | ||
| 52 | + Then I should see "Food Show" | ||
| 53 | + When I follow "Show" | ||
| 54 | + Then I should see "Vegetarian" | ||
| 55 | + And I should not see "Vegetarian Show" | ||
| 56 | + And I should see "Steak Show" |
features/step_definitions/noosfero_steps.rb
| @@ -85,12 +85,12 @@ Given /^the following validation info$/ do |table| | @@ -85,12 +85,12 @@ Given /^the following validation info$/ do |table| | ||
| 85 | end | 85 | end |
| 86 | end | 86 | end |
| 87 | 87 | ||
| 88 | -Given /^the following (product_categories|product_category|category|categories)$/ do |kind,table| | 88 | +Given /^the following (product_categories|product_category|category|categories|regions?)$/ do |kind,table| |
| 89 | klass = kind.singularize.camelize.constantize | 89 | klass = kind.singularize.camelize.constantize |
| 90 | table.hashes.each do |row| | 90 | table.hashes.each do |row| |
| 91 | parent = row.delete("parent") | 91 | parent = row.delete("parent") |
| 92 | if parent | 92 | if parent |
| 93 | - parent = Category.find_by_slug(parent) | 93 | + parent = Category.find_by_slug(parent.to_slug) |
| 94 | row.merge!({:parent_id => parent.id}) | 94 | row.merge!({:parent_id => parent.id}) |
| 95 | end | 95 | end |
| 96 | category = klass.create!({:environment_id => Environment.default.id}.merge(row)) | 96 | category = klass.create!({:environment_id => Environment.default.id}.merge(row)) |
public/stylesheets/application.css
test/functional/categories_controller_test.rb
| @@ -36,6 +36,8 @@ class CategoriesControllerTest < Test::Unit::TestCase | @@ -36,6 +36,8 @@ class CategoriesControllerTest < Test::Unit::TestCase | ||
| 36 | assert_response :success | 36 | assert_response :success |
| 37 | assert_template 'index' | 37 | assert_template 'index' |
| 38 | assert_kind_of Array, assigns(:categories) | 38 | assert_kind_of Array, assigns(:categories) |
| 39 | + assert_kind_of Array, assigns(:product_categories) | ||
| 40 | + assert_kind_of Array, assigns(:regions) | ||
| 39 | assert_tag :tag => 'a', :attributes => { :href => '/admin/categories/new'} | 41 | assert_tag :tag => 'a', :attributes => { :href => '/admin/categories/new'} |
| 40 | end | 42 | end |
| 41 | 43 | ||
| @@ -178,11 +180,13 @@ class CategoriesControllerTest < Test::Unit::TestCase | @@ -178,11 +180,13 @@ class CategoriesControllerTest < Test::Unit::TestCase | ||
| 178 | should 'not list regions and product categories' do | 180 | should 'not list regions and product categories' do |
| 179 | Environment.default.categories.destroy_all | 181 | Environment.default.categories.destroy_all |
| 180 | c = Category.create!(:name => 'Regular category', :environment => Environment.default) | 182 | c = Category.create!(:name => 'Regular category', :environment => Environment.default) |
| 181 | - ProductCategory.create!(:name => 'Product category', :environment => Environment.default) | ||
| 182 | - Region.create!(:name => 'Some region', :environment => Environment.default) | 183 | + p = ProductCategory.create!(:name => 'Product category', :environment => Environment.default) |
| 184 | + r = Region.create!(:name => 'Some region', :environment => Environment.default) | ||
| 183 | 185 | ||
| 184 | get :index | 186 | get :index |
| 185 | assert_equal [c], assigns(:categories) | 187 | assert_equal [c], assigns(:categories) |
| 188 | + assert_equal [p], assigns(:product_categories) | ||
| 189 | + assert_equal [r], assigns(:regions) | ||
| 186 | end | 190 | end |
| 187 | 191 | ||
| 188 | end | 192 | end |
test/unit/environment_test.rb
| @@ -131,6 +131,22 @@ class EnvironmentTest < Test::Unit::TestCase | @@ -131,6 +131,22 @@ class EnvironmentTest < Test::Unit::TestCase | ||
| 131 | assert cats.include?(subcat) | 131 | assert cats.include?(subcat) |
| 132 | end | 132 | end |
| 133 | 133 | ||
| 134 | + def test_should_list_all_product_categories | ||
| 135 | + env = fast_create(Environment) | ||
| 136 | + Category.create!(:name => 'first category', :environment_id => env.id) | ||
| 137 | + cat = Category.create!(:name => 'second category', :environment_id => env.id) | ||
| 138 | + Category.create!(:name => 'child category', :environment_id => env.id, :parent_id => cat.id) | ||
| 139 | + cat1 = ProductCategory.create!(:name => 'first product category', :environment_id => env.id) | ||
| 140 | + cat2 = ProductCategory.create!(:name => 'second product category', :environment_id => env.id) | ||
| 141 | + subcat = ProductCategory.create!(:name => 'child product category', :environment_id => env.id, :parent_id => cat2.id) | ||
| 142 | + | ||
| 143 | + cats = env.product_categories | ||
| 144 | + assert_equal 3, cats.size | ||
| 145 | + assert cats.include?(cat1) | ||
| 146 | + assert cats.include?(cat2) | ||
| 147 | + assert cats.include?(subcat) | ||
| 148 | + end | ||
| 149 | + | ||
| 134 | should 'list displayable categories' do | 150 | should 'list displayable categories' do |
| 135 | env = fast_create(Environment) | 151 | env = fast_create(Environment) |
| 136 | cat1 = env.categories.create(:name => 'category one', :display_color => 1) | 152 | cat1 = env.categories.create(:name => 'category one', :display_color => 1) |