Commit cb989cc7c8d16497bbacb02ba13220e1061d0c13

Authored by AntonioTerceiro
1 parent c400ca46

ActionItem501: PostgreSQL porting



git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2188 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/models/category_finder.rb
@@ -51,7 +51,7 @@ class CategoryFinder @@ -51,7 +51,7 @@ class CategoryFinder
51 def upcoming_events(options = {}) 51 def upcoming_events(options = {})
52 options = { :page => 1}.merge(options) 52 options = { :page => 1}.merge(options)
53 53
54 - Event.paginate(:all, {:include => :categories, :conditions => [ 'categories.id = ? and start_date >= ?', category_id, Date.today ], :order => :start_date }.merge(options)) 54 + Event.paginate(:all, {:include => :categories, :conditions => [ 'categories.id = ? and start_date >= ?', category_id, Date.today ], :order => 'start_date' }.merge(options))
55 end 55 end
56 56
57 protected 57 protected
app/models/profile.rb
@@ -377,11 +377,9 @@ class Profile < ActiveRecord::Base @@ -377,11 +377,9 @@ class Profile < ActiveRecord::Base
377 377
378 after_save :update_category_from_region 378 after_save :update_category_from_region
379 def update_category_from_region 379 def update_category_from_region
380 - categories.find(:all, :conditions => "type = 'Region'").each do |cat|  
381 - categories.delete(cat)  
382 - end 380 + ProfileCategorization.remove_region(self)
383 if region 381 if region
384 - categories << region 382 + self.add_category(region)
385 end 383 end
386 end 384 end
387 385
app/models/profile_categorization.rb
@@ -18,4 +18,12 @@ class ProfileCategorization &lt; ActiveRecord::Base @@ -18,4 +18,12 @@ class ProfileCategorization &lt; ActiveRecord::Base
18 self.delete_all(:profile_id => profile.id) 18 self.delete_all(:profile_id => profile.id)
19 end 19 end
20 20
  21 + def self.remove_region(profile)
  22 + region = profile.categories.find(:first, :conditions => "type = 'Region'")
  23 + if region
  24 + ids = region.hierarchy.map(&:id)
  25 + self.delete_all(:profile_id => profile.id, :category_id => ids)
  26 + end
  27 + end
  28 +
21 end 29 end
test/unit/profile_test.rb
@@ -516,8 +516,9 @@ class ProfileTest &lt; Test::Unit::TestCase @@ -516,8 +516,9 @@ class ProfileTest &lt; Test::Unit::TestCase
516 end 516 end
517 517
518 should 'index profile identifier for searching' do 518 should 'index profile identifier for searching' do
519 - p = Profile.create!(:identifier => 'testprofile', :name => 'Interesting Profile')  
520 - assert_includes Profile.find_by_contents('testprofile'), p 519 + Profile.destroy_all
  520 + p = Profile.create!(:identifier => 'lalala', :name => 'Interesting Profile')
  521 + assert_includes Profile.find_by_contents('lalala'), p
521 end 522 end
522 523
523 should 'index profile name for searching' do 524 should 'index profile name for searching' do
@@ -638,6 +639,18 @@ class ProfileTest &lt; Test::Unit::TestCase @@ -638,6 +639,18 @@ class ProfileTest &lt; Test::Unit::TestCase
638 assert_includes profile.categories(true), region 639 assert_includes profile.categories(true), region
639 end 640 end
640 641
  642 + should 'be able to update region and not get categories removed' do
  643 + region = Region.create!(:name => "Salvador", :environment => Environment.default)
  644 + category = Category.create!(:name => 'test category', :environment => Environment.default)
  645 + profile = Profile.create!(:name => 'testprofile', :identifier => 'testprofile', :region => region, :category_ids => [category.id])
  646 +
  647 + region2 = Region.create!(:name => "Aracaju", :environment => Environment.default)
  648 +
  649 + profile.update_attributes!(:region => region2)
  650 +
  651 + assert_includes profile.categories(true), category
  652 + end
  653 +
641 should 'not accept product category as category' do 654 should 'not accept product category as category' do
642 assert !Profile.new.accept_category?(ProductCategory.new) 655 assert !Profile.new.accept_category?(ProductCategory.new)
643 end 656 end