From c54ef437ad4a9ff89e8c99e078db34d7306333e3 Mon Sep 17 00:00:00 2001 From: Moises Machado Date: Mon, 10 Aug 2009 17:22:57 -0300 Subject: [PATCH] ActionItem1194: made code handles "0" on check_box values --- app/controllers/my_profile/profile_members_controller.rb | 2 +- app/models/article.rb | 2 +- app/models/profile.rb | 2 +- test/functional/profile_members_controller_test.rb | 12 ++++++++++++ test/unit/article_test.rb | 8 ++++++++ test/unit/category_test.rb | 6 +++--- test/unit/profile_test.rb | 8 ++++++++ 7 files changed, 34 insertions(+), 6 deletions(-) diff --git a/app/controllers/my_profile/profile_members_controller.rb b/app/controllers/my_profile/profile_members_controller.rb index 148c4ee..7af7630 100644 --- a/app/controllers/my_profile/profile_members_controller.rb +++ b/app/controllers/my_profile/profile_members_controller.rb @@ -8,7 +8,7 @@ class ProfileMembersController < MyProfileController end def update_roles - @roles = params[:roles] ? environment.roles.find(params[:roles]) : [] + @roles = params[:roles] ? environment.roles.find(params[:roles].select{|r|!r.to_i.zero?}) : [] @roles = @roles.select{|r| r.has_kind?('Profile') } @person = profile.members.find { |m| m.id == params[:person].to_i } if @person && @person.define_roles(@roles, profile) diff --git a/app/models/article.rb b/app/models/article.rb index 9679923..5ac8c6a 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -54,7 +54,7 @@ class Article < ActiveRecord::Base def category_ids=(ids) ArticleCategorization.remove_all_for(self) ids.uniq.each do |item| - add_category(Category.find(item)) + add_category(Category.find(item)) unless item.to_i.zero? end end diff --git a/app/models/profile.rb b/app/models/profile.rb index bccab2e..badad8a 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -176,7 +176,7 @@ class Profile < ActiveRecord::Base def category_ids=(ids) ProfileCategorization.remove_all_for(self) ids.uniq.each do |item| - add_category(Category.find(item)) + add_category(Category.find(item)) unless item.to_i.zero? end end diff --git a/test/functional/profile_members_controller_test.rb b/test/functional/profile_members_controller_test.rb index b937386..4fc849b 100644 --- a/test/functional/profile_members_controller_test.rb +++ b/test/functional/profile_members_controller_test.rb @@ -253,4 +253,16 @@ class ProfileMembersControllerTest < Test::Unit::TestCase assert_includes assigns(:users_found), daniela end + should 'ignore roles with id zero' do + ent = Enterprise.create!(:name => 'Test Ent', :identifier => 'test_ent') + p = create_user_with_permission('test_user', 'manage_memberships', ent) + login_as :test_user + r = ent.environment.roles.create!(:name => 'test_role', :permissions => ['some_perm']) + get :update_roles, :profile => ent.identifier, :person => p.id, :roles => ["0", r.id, nil] + + p_roles = p.find_roles(ent).map(&:role).uniq + + assert p_roles, [r] + end + end diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index 1f42c05..759dfcb 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -741,4 +741,12 @@ class ArticleTest < Test::Unit::TestCase assert_not_includes as, a1 end + should 'ignore category with zero as id' do + a = profile.articles.create!(:name => 'a test article') + c = Category.create!(:name => 'test category', :environment => profile.environment) + a.category_ids = ['0', c.id, nil] + assert a.save + assert_equal [c], a.categories + end + end diff --git a/test/unit/category_test.rb b/test/unit/category_test.rb index eff8d41..b440238 100644 --- a/test/unit/category_test.rb +++ b/test/unit/category_test.rb @@ -288,9 +288,9 @@ class CategoryTest < Test::Unit::TestCase c = @env.categories.build(:name => 'my category'); c.save! person = create_user('testuser').person - a1 = person.articles.build(:name => 'art1', :category_ids => [c]); a1.save! - a2 = person.articles.build(:name => 'art2', :category_ids => [c]); a2.save! - a3 = person.articles.build(:name => 'art3', :category_ids => [c]); a3.save! + a1 = person.articles.build(:name => 'art1', :category_ids => [c.id]); a1.save! + a2 = person.articles.build(:name => 'art2', :category_ids => [c.id]); a2.save! + a3 = person.articles.build(:name => 'art3', :category_ids => [c.id]); a3.save! c1 = a1.comments.build(:title => 'test', :body => 'asdsa', :author => person); c1.save! c2 = a2.comments.build(:title => 'test', :body => 'asdsa', :author => person); c2.save! diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index b642e6e..132bc71 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -1385,6 +1385,14 @@ class ProfileTest < Test::Unit::TestCase assert_equal({ :profile => 'testprofile', :controller => 'profile', :action => 'join'}, profile.join_url) end + should 'ignore categgory with id zero' do + profile = Profile.create!(:name => "Test Profile", :identifier => 'testprofile', :environment_id => create_environment('mycolivre.net').id) + c = Category.create!(:name => 'test cat', :environment => profile.environment) + profile.category_ids = ['0', c.id, nil] + + assert_equal [c], profile.categories + end + private def assert_invalid_identifier(id) -- libgit2 0.21.2