diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index ee64cda..cec19a2 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -405,7 +405,7 @@ module ApplicationHelper Effect.toggle( div, "slide", { link:link, div:div, afterFinish:end } ) }') environment.top_level_categories.each do |toplevel| - next if toplevel.is_a?(ProductCategory) + next unless object.accept_category?(toplevel) # FIXME ([toplevel] + toplevel.children_for_menu).each do |cat| if cat.top_level? diff --git a/app/models/article.rb b/app/models/article.rb index de8d367..f915cfe 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -156,6 +156,10 @@ class Article < ActiveRecord::Base ferret_update end + def accept_category?(cat) + !cat.is_a?(ProductCategory) + end + private def sanitize_tag_list diff --git a/app/models/profile.rb b/app/models/profile.rb index 03a5618..fa8af1f 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -103,6 +103,8 @@ class Profile < ActiveRecord::Base has_many :profile_categorizations, :conditions => [ 'categories_profiles.virtual = ?', false ] has_many :categories, :through => :profile_categorizations + belongs_to :region + def pending_categorizations @pending_categorizations ||= [] end @@ -359,4 +361,19 @@ class Profile < ActiveRecord::Base end end + after_save :update_category_from_region + def update_category_from_region + categories.find(:all, :conditions => "type = 'Region'").each do |cat| + categories.delete(cat) + end + if region + categories << region + end + end + + def accept_category?(cat) + forbidden = [ ProductCategory, Region ] + !forbidden.include?(cat.class) + end + end diff --git a/db/migrate/044_add_region_to_profile.rb b/db/migrate/044_add_region_to_profile.rb new file mode 100644 index 0000000..4d1f080 --- /dev/null +++ b/db/migrate/044_add_region_to_profile.rb @@ -0,0 +1,9 @@ +class AddRegionToProfile < ActiveRecord::Migration + def self.up + add_column :profiles, :region_id, :integer + end + + def self.down + remove_column :profiles, :region_id + end +end diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index f6a5269..04b9147 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -364,4 +364,8 @@ class ArticleTest < Test::Unit::TestCase end + should 'not accept Product category as category' do + assert !Article.new.accept_category?(ProductCategory.new) + end + end diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index dd16276..635f28f 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -580,6 +580,82 @@ class ProfileTest < Test::Unit::TestCase assert_equivalent [c1, c2], profile.categories(true) end + should 'be associated with a region' do + region = Region.create!(:name => "Salvador", :environment => Environment.default) + profile = Profile.create!(:name => 'testprofile', :identifier => 'testprofile', :region => region) + + assert_equal region, profile.region + end + + should 'categorized automatically in its region' do + region = Region.create!(:name => "Salvador", :environment => Environment.default) + profile = Profile.create!(:name => 'testprofile', :identifier => 'testprofile', :region => region) + + assert_equal [region], profile.categories(true) + end + + should 'change categorization when changing region' do + region = Region.create!(:name => "Salvador", :environment => Environment.default) + profile = Profile.create!(:name => 'testprofile', :identifier => 'testprofile', :region => region) + + region2 = Region.create!(:name => "Feira de Santana", :environment => Environment.default) + + profile.region = region2 + profile.save! + + assert_equal [region2], profile.categories(true) + end + + should 'remove categorization when removing region' do + region = Region.create!(:name => "Salvador", :environment => Environment.default) + profile = Profile.create!(:name => 'testprofile', :identifier => 'testprofile', :region => region) + + profile.region = nil + profile.save! + + assert_equal [], profile.categories(true) + end + + should 'not remove region, only dissasociate from it' do + region = Region.create!(:name => "Salvador", :environment => Environment.default) + profile = Profile.create!(:name => 'testprofile', :identifier => 'testprofile', :region => region) + + profile.region = nil + profile.save! + + assert_nothing_raised do + Region.find(region.id) + end + end + + should 'be able to create with categories and region at the same time' 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]) + + assert_equivalent [region, category], profile.categories(true) + end + + should 'be able to update categories and not get regions 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]) + + category2 = Category.create!(:name => 'test category 2', :environment => Environment.default) + + profile.update_attributes!(:category_ids => [category2.id]) + + assert_includes profile.categories(true), region + end + + should 'not accept product category as category' do + assert !Profile.new.accept_category?(ProductCategory.new) + end + + should 'not accept region as a category' do + assert !Profile.new.accept_category?(Region.new) + end + private def assert_invalid_identifier(id) -- libgit2 0.21.2