Commit b3dc4ef4d288d9120c9d9a95ef276aba0f633555
1 parent
1f1fb02a
Exists in
master
and in
22 other branches
ActionItem133: choosing categories
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1603 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
17 changed files
with
120 additions
and
22 deletions
Show diff stats
app/controllers/my_profile/profile_editor_controller.rb
| @@ -31,5 +31,15 @@ class ProfileEditorController < MyProfileController | @@ -31,5 +31,15 @@ class ProfileEditorController < MyProfileController | ||
| 31 | end | 31 | end |
| 32 | end | 32 | end |
| 33 | end | 33 | end |
| 34 | + | ||
| 35 | + def edit_categories | ||
| 36 | + @profile_object = profile | ||
| 37 | + if request.post? | ||
| 38 | + if profile.update_attributes(params[:profile_object]) | ||
| 39 | + redirect_to :action => 'index' | ||
| 40 | + end | ||
| 41 | + end | ||
| 42 | + end | ||
| 43 | + | ||
| 34 | end | 44 | end |
| 35 | 45 |
app/helpers/application_helper.rb
| @@ -442,4 +442,23 @@ module ApplicationHelper | @@ -442,4 +442,23 @@ module ApplicationHelper | ||
| 442 | url | 442 | url |
| 443 | end | 443 | end |
| 444 | 444 | ||
| 445 | + attr_reader :environment | ||
| 446 | + def select_categories(object_name) | ||
| 447 | + object = instance_variable_get("@#{object_name}") | ||
| 448 | + | ||
| 449 | + result = content_tag('h4', _('Categories')) | ||
| 450 | + environment.top_level_categories.each do |toplevel| | ||
| 451 | + toplevel.map_traversal do |cat| | ||
| 452 | + if cat.top_level? | ||
| 453 | + result << content_tag('h5', toplevel.name) | ||
| 454 | + else | ||
| 455 | + checkbox_id = "#{object_name}_#{cat.full_name.downcase.gsub(/\s+|\//, '_')}" | ||
| 456 | + result << content_tag('label', check_box_tag("#{object_name}[category_ids][]", cat.id, object.category_ids.include?(cat.id), :id => checkbox_id) + cat.full_name_without_leading(1), :for => checkbox_id) | ||
| 457 | + end | ||
| 458 | + end | ||
| 459 | + end | ||
| 460 | + | ||
| 461 | + content_tag('div', result) | ||
| 462 | + end | ||
| 463 | + | ||
| 445 | end | 464 | end |
app/helpers/cms_helper.rb
| @@ -24,22 +24,4 @@ module CmsHelper | @@ -24,22 +24,4 @@ module CmsHelper | ||
| 24 | 24 | ||
| 25 | attr_reader :environment | 25 | attr_reader :environment |
| 26 | 26 | ||
| 27 | - def select_categories(object_name) | ||
| 28 | - object = instance_variable_get("@#{object_name}") | ||
| 29 | - | ||
| 30 | - result = content_tag('h4', _('Categories')) | ||
| 31 | - environment.top_level_categories.each do |toplevel| | ||
| 32 | - toplevel.map_traversal do |cat| | ||
| 33 | - if cat.top_level? | ||
| 34 | - result << content_tag('h5', toplevel.name) | ||
| 35 | - else | ||
| 36 | - checkbox_id = "#{object_name}_#{cat.full_name.downcase.gsub(/\s+|\//, '_')}" | ||
| 37 | - result << content_tag('label', check_box_tag("#{object_name}[category_ids][]", cat.id, object.category_ids.include?(cat.id), :id => checkbox_id) + cat.full_name_without_leading(1), :for => checkbox_id) | ||
| 38 | - end | ||
| 39 | - end | ||
| 40 | - end | ||
| 41 | - | ||
| 42 | - content_tag('div', result) | ||
| 43 | - end | ||
| 44 | - | ||
| 45 | end | 27 | end |
app/models/category.rb
| @@ -25,6 +25,9 @@ class Category < ActiveRecord::Base | @@ -25,6 +25,9 @@ class Category < ActiveRecord::Base | ||
| 25 | has_many :articles, :through => :article_categorizations | 25 | has_many :articles, :through => :article_categorizations |
| 26 | has_many :comments, :through => :articles | 26 | has_many :comments, :through => :articles |
| 27 | 27 | ||
| 28 | + has_many :profile_categorizations | ||
| 29 | + has_many :profiles, :through => :profile_categorizations | ||
| 30 | + | ||
| 28 | def recent_articles(limit = 10) | 31 | def recent_articles(limit = 10) |
| 29 | self.articles.recent(limit) | 32 | self.articles.recent(limit) |
| 30 | end | 33 | end |
app/models/profile.rb
| @@ -73,6 +73,8 @@ class Profile < ActiveRecord::Base | @@ -73,6 +73,8 @@ class Profile < ActiveRecord::Base | ||
| 73 | 73 | ||
| 74 | has_many :tasks, :foreign_key => :target_id | 74 | has_many :tasks, :foreign_key => :target_id |
| 75 | 75 | ||
| 76 | + has_and_belongs_to_many :categories | ||
| 77 | + | ||
| 76 | def top_level_articles(reload = false) | 78 | def top_level_articles(reload = false) |
| 77 | if reload | 79 | if reload |
| 78 | @top_level_articles = nil | 80 | @top_level_articles = nil |
| @@ -0,0 +1,7 @@ | @@ -0,0 +1,7 @@ | ||
| 1 | +<% form_for :profile_object, @profile_object, :url => { :action => 'edit_categories'} do |f| %> | ||
| 2 | + <%= select_categories('profile_object') %> | ||
| 3 | + <% button_bar do %> | ||
| 4 | + <%= submit_button('save', _('Save'), :cancel => {:action => 'edit_categories'}) %> | ||
| 5 | + <%= button(:back, _('Back to control panel'), :controller => 'profile_editor') %> | ||
| 6 | + <% end %> | ||
| 7 | +<% end %> |
app/views/profile_editor/index.rhtml
| @@ -25,6 +25,8 @@ | @@ -25,6 +25,8 @@ | ||
| 25 | 25 | ||
| 26 | <%= file_manager_button(_('Manage Content'), 'icons-app/cms.png', :controller => 'cms') %> | 26 | <%= file_manager_button(_('Manage Content'), 'icons-app/cms.png', :controller => 'cms') %> |
| 27 | 27 | ||
| 28 | + <%= file_manager_button(_('Edit Categories'), 'icons-app/categories.png', :controller => 'profile_editor', :action => 'edit_categories') %> | ||
| 29 | + | ||
| 28 | <%= file_manager_button(_('Change Password'), 'icons-app/change-password.png', :controller => 'account', :action => 'change_password') if profile.person? %> | 30 | <%= file_manager_button(_('Change Password'), 'icons-app/change-password.png', :controller => 'account', :action => 'change_password') if profile.person? %> |
| 29 | 31 | ||
| 30 | <%= file_manager_button(_('Manage friends'), 'icons-app/friends.png', :controller => 'friends', :action => 'index') if profile.person? %> | 32 | <%= file_manager_button(_('Manage friends'), 'icons-app/friends.png', :controller => 'friends', :action => 'index') if profile.person? %> |
| @@ -0,0 +1,12 @@ | @@ -0,0 +1,12 @@ | ||
| 1 | +class CreateCategoriesProfiles < ActiveRecord::Migration | ||
| 2 | + def self.up | ||
| 3 | + create_table :categories_profiles, :id => false do |t| | ||
| 4 | + t.integer :profile_id | ||
| 5 | + t.integer :category_id | ||
| 6 | + end | ||
| 7 | + end | ||
| 8 | + | ||
| 9 | + def self.down | ||
| 10 | + drop_table :categories_profiles | ||
| 11 | + end | ||
| 12 | +end |
public/images/icons-app/README
| @@ -28,6 +28,7 @@ gtk-yes.png dlg-neu | @@ -28,6 +28,7 @@ gtk-yes.png dlg-neu | ||
| 28 | users.png (locally modified) dlg-neu | 28 | users.png (locally modified) dlg-neu |
| 29 | stock_todo.png Nuovo | 29 | stock_todo.png Nuovo |
| 30 | friends.png (modified version of users.png) Nuovo | 30 | friends.png (modified version of users.png) Nuovo |
| 31 | +gtk-folder.png Nuovo | ||
| 31 | 32 | ||
| 32 | Icons rasterization | 33 | Icons rasterization |
| 33 | =================== | 34 | =================== |
3.03 KB
test/functional/profile_editor_controller_test.rb
| @@ -74,12 +74,23 @@ class ProfileEditorControllerTest < Test::Unit::TestCase | @@ -74,12 +74,23 @@ class ProfileEditorControllerTest < Test::Unit::TestCase | ||
| 74 | end | 74 | end |
| 75 | 75 | ||
| 76 | should 'display categories to choose to associate profile' do | 76 | should 'display categories to choose to associate profile' do |
| 77 | - cat = Environment.default.categories.build(:name => 'a category'); cat.save! | 77 | + cat1 = Environment.default.categories.build(:name => 'top category'); cat1.save! |
| 78 | + cat2 = Environment.default.categories.build(:name => 'sub category', :parent => cat1); cat2.save! | ||
| 78 | person = create_user('test_user').person | 79 | person = create_user('test_user').person |
| 79 | - get :edit, :profile => 'test_user' | ||
| 80 | - assert_tag :tag => 'input', :attributes => {:name => 'profile[category_ids][]'} | 80 | + get :edit_categories, :profile => 'test_user' |
| 81 | + assert_response :success | ||
| 82 | + assert_template 'edit_categories' | ||
| 83 | + assert_tag :tag => 'input', :attributes => {:name => 'profile_object[category_ids][]'} | ||
| 81 | end | 84 | end |
| 82 | 85 | ||
| 83 | - should 'save categorization of profile' | 86 | + should 'save categorization of profile' do |
| 87 | + cat1 = Environment.default.categories.build(:name => 'top category'); cat1.save! | ||
| 88 | + cat2 = Environment.default.categories.build(:name => 'sub category', :parent => cat1); cat2.save! | ||
| 89 | + person = create_user('test_user').person | ||
| 90 | + post :edit_categories, :profile => 'test_user', :profile_object => {:category_ids => [cat2.id]} | ||
| 91 | + assert_response :redirect | ||
| 92 | + assert_redirected_to :action => 'index' | ||
| 93 | + assert_includes person.categories, cat2 | ||
| 94 | + end | ||
| 84 | 95 | ||
| 85 | end | 96 | end |
test/unit/category_test.rb
| @@ -276,4 +276,14 @@ class CategoryTest < Test::Unit::TestCase | @@ -276,4 +276,14 @@ class CategoryTest < Test::Unit::TestCase | ||
| 276 | assert_equivalent [c1, c2, c3], c.comments | 276 | assert_equivalent [c1, c2, c3], c.comments |
| 277 | end | 277 | end |
| 278 | 278 | ||
| 279 | + should 'have profiles' do | ||
| 280 | + c = @env.categories.build(:name => 'my category'); c.save! | ||
| 281 | + person1 = create_user('testuser_one').person | ||
| 282 | + person1.categories << c | ||
| 283 | + person2 = create_user('testuser_two').person | ||
| 284 | + person2.categories << c | ||
| 285 | + assert_includes c.profiles, person1 | ||
| 286 | + assert_includes c.profiles, person2 | ||
| 287 | + end | ||
| 288 | + | ||
| 279 | end | 289 | end |
| @@ -0,0 +1,13 @@ | @@ -0,0 +1,13 @@ | ||
| 1 | +require File.dirname(__FILE__) + '/../test_helper' | ||
| 2 | + | ||
| 3 | +class ProfileCategorizationTest < ActiveSupport::TestCase | ||
| 4 | + should 'have profile and category' do | ||
| 5 | + person = create_user('test_user').person | ||
| 6 | + cat = Environment.default.categories.build(:name => 'a category'); cat.save! | ||
| 7 | + person.categories << cat | ||
| 8 | + person.save! | ||
| 9 | + assert_includes person.categories, cat | ||
| 10 | + assert_includes cat.profiles, person | ||
| 11 | + assert_equal [cat.id], person.category_ids | ||
| 12 | + end | ||
| 13 | +end |
test/unit/profile_test.rb
| @@ -377,6 +377,19 @@ class ProfileTest < Test::Unit::TestCase | @@ -377,6 +377,19 @@ class ProfileTest < Test::Unit::TestCase | ||
| 377 | assert_equal [t2, t3], c.tasks.finished | 377 | assert_equal [t2, t3], c.tasks.finished |
| 378 | end | 378 | end |
| 379 | 379 | ||
| 380 | + should 'responds to categories' do | ||
| 381 | + c = Profile.create!(:name => 'my test profile', :identifier => 'mytestprofile') | ||
| 382 | + assert_respond_to c, :categories | ||
| 383 | + end | ||
| 384 | + | ||
| 385 | + should 'have categories' do | ||
| 386 | + c = Profile.create!(:name => 'my test profile', :identifier => 'mytestprofile') | ||
| 387 | + cat = Environment.default.categories.build(:name => 'a category'); cat.save! | ||
| 388 | + c.categories << cat | ||
| 389 | + c.save! | ||
| 390 | + assert_includes c.categories, cat | ||
| 391 | + end | ||
| 392 | + | ||
| 380 | private | 393 | private |
| 381 | 394 | ||
| 382 | def assert_invalid_identifier(id) | 395 | def assert_invalid_identifier(id) |