Commit d3898cc182a035b2851f46b35cba02850b6c5660

Authored by Larissa Reis
1 parent b4c42da1

Add scope to search product categories by environment

app/models/product_category.rb
@@ -10,6 +10,9 @@ class ProductCategory < Category @@ -10,6 +10,9 @@ class ProductCategory < Category
10 :joins => :products, 10 :joins => :products,
11 :conditions => ['products.profile_id = ?', enterprise.id] 11 :conditions => ['products.profile_id = ?', enterprise.id]
12 }} 12 }}
  13 + scope :by_environment, lambda { |environment| {
  14 + :conditions => ['environment_id = ?', environment.id]
  15 + }}
13 scope :unique_by_level, lambda { |level| { 16 scope :unique_by_level, lambda { |level| {
14 :select => "DISTINCT ON (filtered_category) split_part(path, '/', #{level}) AS filtered_category, categories.*" 17 :select => "DISTINCT ON (filtered_category) split_part(path, '/', #{level}) AS filtered_category, categories.*"
15 }} 18 }}
test/unit/product_category_test.rb
@@ -57,6 +57,19 @@ class ProductCategoryTest < ActiveSupport::TestCase @@ -57,6 +57,19 @@ class ProductCategoryTest < ActiveSupport::TestCase
57 assert_equivalent [c1,c2], scope 57 assert_equivalent [c1,c2], scope
58 end 58 end
59 59
  60 + should 'provide a scope based on the environment' do
  61 + alt_environment = fast_create(Environment)
  62 + c1 = ProductCategory.create!(:name => 'test cat 1', :environment => Environment.default)
  63 + c2 = ProductCategory.create!(:name => 'test cat 2', :environment => alt_environment)
  64 + c3 = ProductCategory.create!(:name => 'test cat 3', :environment => Environment.default)
  65 +
  66 + scope = ProductCategory.by_environment(alt_environment)
  67 +
  68 + assert_equal ActiveRecord::Relation, scope.class
  69 + assert_equivalent [c2], scope
  70 + assert_equivalent [c1,c3], ProductCategory.by_environment(Environment.default)
  71 + end
  72 +
60 should 'fetch unique categories by level' do 73 should 'fetch unique categories by level' do
61 c1 = ProductCategory.create!(:name => 'test cat 1', :environment => Environment.default) 74 c1 = ProductCategory.create!(:name => 'test cat 1', :environment => Environment.default)
62 c11 = ProductCategory.create!(:name => 'test cat 11', :environment => Environment.default, :parent => c1) 75 c11 = ProductCategory.create!(:name => 'test cat 11', :environment => Environment.default, :parent => c1)