From cb989cc7c8d16497bbacb02ba13220e1061d0c13 Mon Sep 17 00:00:00 2001 From: AntonioTerceiro Date: Wed, 9 Jul 2008 20:33:08 +0000 Subject: [PATCH] ActionItem501: PostgreSQL porting --- app/models/category_finder.rb | 2 +- app/models/profile.rb | 6 ++---- app/models/profile_categorization.rb | 8 ++++++++ test/unit/profile_test.rb | 17 +++++++++++++++-- 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/app/models/category_finder.rb b/app/models/category_finder.rb index b40ff43..f9b2bab 100644 --- a/app/models/category_finder.rb +++ b/app/models/category_finder.rb @@ -51,7 +51,7 @@ class CategoryFinder def upcoming_events(options = {}) options = { :page => 1}.merge(options) - Event.paginate(:all, {:include => :categories, :conditions => [ 'categories.id = ? and start_date >= ?', category_id, Date.today ], :order => :start_date }.merge(options)) + Event.paginate(:all, {:include => :categories, :conditions => [ 'categories.id = ? and start_date >= ?', category_id, Date.today ], :order => 'start_date' }.merge(options)) end protected diff --git a/app/models/profile.rb b/app/models/profile.rb index 87a1e59..68ba930 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -377,11 +377,9 @@ class Profile < ActiveRecord::Base after_save :update_category_from_region def update_category_from_region - categories.find(:all, :conditions => "type = 'Region'").each do |cat| - categories.delete(cat) - end + ProfileCategorization.remove_region(self) if region - categories << region + self.add_category(region) end end diff --git a/app/models/profile_categorization.rb b/app/models/profile_categorization.rb index 9ba3fa1..6143758 100644 --- a/app/models/profile_categorization.rb +++ b/app/models/profile_categorization.rb @@ -18,4 +18,12 @@ class ProfileCategorization < ActiveRecord::Base self.delete_all(:profile_id => profile.id) end + def self.remove_region(profile) + region = profile.categories.find(:first, :conditions => "type = 'Region'") + if region + ids = region.hierarchy.map(&:id) + self.delete_all(:profile_id => profile.id, :category_id => ids) + end + end + end diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index aa421b0..bf45970 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -516,8 +516,9 @@ class ProfileTest < Test::Unit::TestCase end should 'index profile identifier for searching' do - p = Profile.create!(:identifier => 'testprofile', :name => 'Interesting Profile') - assert_includes Profile.find_by_contents('testprofile'), p + Profile.destroy_all + p = Profile.create!(:identifier => 'lalala', :name => 'Interesting Profile') + assert_includes Profile.find_by_contents('lalala'), p end should 'index profile name for searching' do @@ -638,6 +639,18 @@ class ProfileTest < Test::Unit::TestCase assert_includes profile.categories(true), region end + should 'be able to update region and not get categories removed' do + region = Region.create!(:name => "Salvador", :environment => Environment.default) + category = Category.create!(:name => 'test category', :environment => Environment.default) + profile = Profile.create!(:name => 'testprofile', :identifier => 'testprofile', :region => region, :category_ids => [category.id]) + + region2 = Region.create!(:name => "Aracaju", :environment => Environment.default) + + profile.update_attributes!(:region => region2) + + assert_includes profile.categories(true), category + end + should 'not accept product category as category' do assert !Profile.new.accept_category?(ProductCategory.new) end -- libgit2 0.21.2