Commit 7a857453f63c1aebd689602c132d5cb3f2c40ad4

Authored by AntonioTerceiro
1 parent 69ebcc10

ActionItem512: adding region for profiles

git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2129 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/helpers/application_helper.rb
@@ -405,7 +405,7 @@ module ApplicationHelper @@ -405,7 +405,7 @@ module ApplicationHelper
405 Effect.toggle( div, "slide", { link:link, div:div, afterFinish:end } ) 405 Effect.toggle( div, "slide", { link:link, div:div, afterFinish:end } )
406 }') 406 }')
407 environment.top_level_categories.each do |toplevel| 407 environment.top_level_categories.each do |toplevel|
408 - next if toplevel.is_a?(ProductCategory) 408 + next unless object.accept_category?(toplevel)
409 # FIXME 409 # FIXME
410 ([toplevel] + toplevel.children_for_menu).each do |cat| 410 ([toplevel] + toplevel.children_for_menu).each do |cat|
411 if cat.top_level? 411 if cat.top_level?
app/models/article.rb
@@ -156,6 +156,10 @@ class Article < ActiveRecord::Base @@ -156,6 +156,10 @@ class Article < ActiveRecord::Base
156 ferret_update 156 ferret_update
157 end 157 end
158 158
  159 + def accept_category?(cat)
  160 + !cat.is_a?(ProductCategory)
  161 + end
  162 +
159 private 163 private
160 164
161 def sanitize_tag_list 165 def sanitize_tag_list
app/models/profile.rb
@@ -103,6 +103,8 @@ class Profile < ActiveRecord::Base @@ -103,6 +103,8 @@ class Profile < ActiveRecord::Base
103 has_many :profile_categorizations, :conditions => [ 'categories_profiles.virtual = ?', false ] 103 has_many :profile_categorizations, :conditions => [ 'categories_profiles.virtual = ?', false ]
104 has_many :categories, :through => :profile_categorizations 104 has_many :categories, :through => :profile_categorizations
105 105
  106 + belongs_to :region
  107 +
106 def pending_categorizations 108 def pending_categorizations
107 @pending_categorizations ||= [] 109 @pending_categorizations ||= []
108 end 110 end
@@ -359,4 +361,19 @@ class Profile < ActiveRecord::Base @@ -359,4 +361,19 @@ class Profile < ActiveRecord::Base
359 end 361 end
360 end 362 end
361 363
  364 + after_save :update_category_from_region
  365 + def update_category_from_region
  366 + categories.find(:all, :conditions => "type = 'Region'").each do |cat|
  367 + categories.delete(cat)
  368 + end
  369 + if region
  370 + categories << region
  371 + end
  372 + end
  373 +
  374 + def accept_category?(cat)
  375 + forbidden = [ ProductCategory, Region ]
  376 + !forbidden.include?(cat.class)
  377 + end
  378 +
362 end 379 end
db/migrate/044_add_region_to_profile.rb 0 → 100644
@@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
  1 +class AddRegionToProfile < ActiveRecord::Migration
  2 + def self.up
  3 + add_column :profiles, :region_id, :integer
  4 + end
  5 +
  6 + def self.down
  7 + remove_column :profiles, :region_id
  8 + end
  9 +end
test/unit/article_test.rb
@@ -364,4 +364,8 @@ class ArticleTest &lt; Test::Unit::TestCase @@ -364,4 +364,8 @@ class ArticleTest &lt; Test::Unit::TestCase
364 364
365 end 365 end
366 366
  367 + should 'not accept Product category as category' do
  368 + assert !Article.new.accept_category?(ProductCategory.new)
  369 + end
  370 +
367 end 371 end
test/unit/profile_test.rb
@@ -580,6 +580,82 @@ class ProfileTest &lt; Test::Unit::TestCase @@ -580,6 +580,82 @@ class ProfileTest &lt; Test::Unit::TestCase
580 assert_equivalent [c1, c2], profile.categories(true) 580 assert_equivalent [c1, c2], profile.categories(true)
581 end 581 end
582 582
  583 + should 'be associated with a region' do
  584 + region = Region.create!(:name => "Salvador", :environment => Environment.default)
  585 + profile = Profile.create!(:name => 'testprofile', :identifier => 'testprofile', :region => region)
  586 +
  587 + assert_equal region, profile.region
  588 + end
  589 +
  590 + should 'categorized automatically in its region' do
  591 + region = Region.create!(:name => "Salvador", :environment => Environment.default)
  592 + profile = Profile.create!(:name => 'testprofile', :identifier => 'testprofile', :region => region)
  593 +
  594 + assert_equal [region], profile.categories(true)
  595 + end
  596 +
  597 + should 'change categorization when changing region' do
  598 + region = Region.create!(:name => "Salvador", :environment => Environment.default)
  599 + profile = Profile.create!(:name => 'testprofile', :identifier => 'testprofile', :region => region)
  600 +
  601 + region2 = Region.create!(:name => "Feira de Santana", :environment => Environment.default)
  602 +
  603 + profile.region = region2
  604 + profile.save!
  605 +
  606 + assert_equal [region2], profile.categories(true)
  607 + end
  608 +
  609 + should 'remove categorization when removing region' do
  610 + region = Region.create!(:name => "Salvador", :environment => Environment.default)
  611 + profile = Profile.create!(:name => 'testprofile', :identifier => 'testprofile', :region => region)
  612 +
  613 + profile.region = nil
  614 + profile.save!
  615 +
  616 + assert_equal [], profile.categories(true)
  617 + end
  618 +
  619 + should 'not remove region, only dissasociate from it' do
  620 + region = Region.create!(:name => "Salvador", :environment => Environment.default)
  621 + profile = Profile.create!(:name => 'testprofile', :identifier => 'testprofile', :region => region)
  622 +
  623 + profile.region = nil
  624 + profile.save!
  625 +
  626 + assert_nothing_raised do
  627 + Region.find(region.id)
  628 + end
  629 + end
  630 +
  631 + should 'be able to create with categories and region at the same time' do
  632 + region = Region.create!(:name => "Salvador", :environment => Environment.default)
  633 + category = Category.create!(:name => 'test category', :environment => Environment.default)
  634 + profile = Profile.create!(:name => 'testprofile', :identifier => 'testprofile', :region => region, :category_ids => [category.id])
  635 +
  636 + assert_equivalent [region, category], profile.categories(true)
  637 + end
  638 +
  639 + should 'be able to update categories and not get regions removed' do
  640 + region = Region.create!(:name => "Salvador", :environment => Environment.default)
  641 + category = Category.create!(:name => 'test category', :environment => Environment.default)
  642 + profile = Profile.create!(:name => 'testprofile', :identifier => 'testprofile', :region => region, :category_ids => [category.id])
  643 +
  644 + category2 = Category.create!(:name => 'test category 2', :environment => Environment.default)
  645 +
  646 + profile.update_attributes!(:category_ids => [category2.id])
  647 +
  648 + assert_includes profile.categories(true), region
  649 + end
  650 +
  651 + should 'not accept product category as category' do
  652 + assert !Profile.new.accept_category?(ProductCategory.new)
  653 + end
  654 +
  655 + should 'not accept region as a category' do
  656 + assert !Profile.new.accept_category?(Region.new)
  657 + end
  658 +
583 private 659 private
584 660
585 def assert_invalid_identifier(id) 661 def assert_invalid_identifier(id)