diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 9e4d387..7a46635 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -514,24 +514,25 @@ module ApplicationHelper def profile_cat_icons( profile ) if profile.class == Enterprise - icons = profile.product_categories.map{ |c| c.size > 1 ? c[1] : nil }. - compact.uniq.map do |c| - cat_name = c.gsub( /[-_\s,.;'"]+/, '_' ) - cat_icon = "/images/icons-cat/#{cat_name}.png" - if ! File.exists? RAILS_ROOT.to_s() + '/public/' + cat_icon - cat_icon = '/images/icons-cat/undefined.png' - end - content_tag('span', - content_tag( 'span', c ), - :title => c, - :class => 'product-cat-icon cat_icon_' + cat_name, - :style => "background-image:url(#{cat_icon})" - ) - end.join("\n").html_safe - content_tag('div', - content_tag( 'span', _('Principal Product Categories'), :class => 'header' ) +"\n"+ icons, - :class => 'product-category-icons' + icons = profile.product_categories.unique_by_level(2).limit(3).map do |c| + filtered_category = c.filtered_category.blank? ? c.path.split('/').last : c.filtered_category + category_title = filtered_category.split(/[-_\s,.;'"]+/).map(&:capitalize).join(' ') + category_name = category_title.gsub(' ', '_' ) + category_icon = "/images/icons-cat/#{category_name}.png" + if ! File.exists? RAILS_ROOT.to_s() + '/public/' + category_icon + category_icon = '/images/icons-cat/undefined.png' + end + content_tag('span', + content_tag( 'span', category_title ), + :title => category_title, + :class => 'product-cat-icon cat_icon_' + category_name, + :style => "background-image:url(#{category_icon})" ) + end.join("\n").html_safe + content_tag('div', + content_tag( 'span', _('Principal Product Categories'), :class => 'header' ) +"\n"+ icons, + :class => 'product-category-icons' + ) else '' end diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 1d045fb..805c5c1 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -17,7 +17,7 @@ class Enterprise < Organization has_and_belongs_to_many :fans, :class_name => 'Person', :join_table => 'favorite_enteprises_people' def product_categories - products.includes(:product_category).map{|p| p.category_full_name}.compact + ProductCategory.by_enterprise(self) end N_('Organization website'); N_('Historic and current context'); N_('Activities short description'); N_('City'); N_('State'); N_('Country'); N_('ZIP code') diff --git a/app/models/product.rb b/app/models/product.rb index 02960d6..aba052d 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -88,10 +88,6 @@ class Product < ActiveRecord::Base image ? image.public_filename(size) : '/images/icons-app/product-default-pic-%s.png' % size end - def category_full_name - product_category ? product_category.full_name.split('/') : nil - end - acts_as_having_image def save_image diff --git a/app/models/product_category.rb b/app/models/product_category.rb index c3c1553..5293bfc 100644 --- a/app/models/product_category.rb +++ b/app/models/product_category.rb @@ -3,6 +3,15 @@ class ProductCategory < Category has_many :products has_many :inputs + named_scope :unique, :select => 'DISTINCT ON (path) categories.*' + named_scope :by_enterprise, lambda { |enterprise| { + :joins => :products, + :conditions => ['products.profile_id = ?', enterprise.id] + }} + named_scope :unique_by_level, lambda { |level| { + :select => "DISTINCT ON (filtered_category) split_part(path, '/', #{level}) AS filtered_category, categories.*" + }} + def all_products Product.find(:all, :conditions => { :product_category_id => (all_children << self).map(&:id) }) end diff --git a/test/unit/enterprise_test.rb b/test/unit/enterprise_test.rb index 2744323..6df50f5 100644 --- a/test/unit/enterprise_test.rb +++ b/test/unit/enterprise_test.rb @@ -234,12 +234,12 @@ class EnterpriseTest < ActiveSupport::TestCase assert enterprise.enable(person) end - should 'list product categories full name' do + should 'list product categories' do subcategory = fast_create(ProductCategory, :name => 'Products subcategory', :parent_id => @product_category.id) ent = fast_create(Enterprise, :name => 'test ent', :identifier => 'test_ent') p = ent.products.create!(:name => 'test prod', :product_category => subcategory) - assert_equal [p.category_full_name], ent.product_categories + assert_equivalent [subcategory], ent.product_categories end should 'not create a products block for enterprise if environment do not let' do diff --git a/test/unit/product_category_test.rb b/test/unit/product_category_test.rb index d5f8557..e176094 100644 --- a/test/unit/product_category_test.rb +++ b/test/unit/product_category_test.rb @@ -36,4 +36,29 @@ class ProductCategoryTest < ActiveSupport::TestCase assert_equal [c11], ProductCategory.menu_categories(c1, nil) end + should 'provide a scope based on the enterprise' do + enterprise = fast_create(Enterprise) + c1 = ProductCategory.create!(:name => 'test cat 1', :environment => Environment.default) + c2 = ProductCategory.create!(:name => 'test cat 2', :environment => Environment.default) + c3 = ProductCategory.create!(:name => 'test cat 3', :environment => Environment.default) + p1 = Product.create(:name => 'product1', :product_category => c1, :profile_id => enterprise.id) + p2 = Product.create(:name => 'product2', :product_category => c1, :profile_id => enterprise.id) + p3 = Product.create(:name => 'product3', :product_category => c2, :profile_id => enterprise.id) + + scope = ProductCategory.by_enterprise(enterprise) + + assert_equal ActiveRecord::NamedScope::Scope, scope.class + assert_equivalent [c1,c2], scope + end + + should 'fetch unique categories by level' do + c1 = ProductCategory.create!(:name => 'test cat 1', :environment => Environment.default) + c11 = ProductCategory.create!(:name => 'test cat 11', :environment => Environment.default, :parent => c1) + c12 = ProductCategory.create!(:name => 'test cat 12', :environment => Environment.default, :parent => c1) + c111 = ProductCategory.create!(:name => 'test cat 111', :environment => Environment.default, :parent => c11) + c112 = ProductCategory.create!(:name => 'test cat 112', :environment => Environment.default, :parent => c11) + + assert_equivalent ['', 'test-cat-11', 'test-cat-12'], ProductCategory.unique_by_level(2).map(&:filtered_category) + end + end diff --git a/test/unit/product_test.rb b/test/unit/product_test.rb index 838784e..643e086 100644 --- a/test/unit/product_test.rb +++ b/test/unit/product_test.rb @@ -89,21 +89,6 @@ class ProductTest < ActiveSupport::TestCase end end - should 'calculate category full name' do - cat = mock - cat.expects(:full_name).returns('A/B/C') - - p = Product.new - p.stubs(:product_category).returns(cat) - assert_equal ['A','B','C'], p.category_full_name - end - - should 'return a nil cateory full name when not categorized' do - p = Product.new - p.stubs(:product_category).returns(nil) - assert_equal nil, p.category_full_name - end - should 'have same lat and lng of its enterprise' do ent = fast_create(Enterprise, :name => 'test enterprise', :identifier => 'test_enterprise', :lat => 30.0, :lng => 30.0) prod = ent.products.create!(:name => 'test product', :product_category => @product_category) -- libgit2 0.21.2