From d3898cc182a035b2851f46b35cba02850b6c5660 Mon Sep 17 00:00:00 2001 From: Larissa Reis Date: Wed, 15 Apr 2015 15:58:16 -0300 Subject: [PATCH] Add scope to search product categories by environment --- app/models/product_category.rb | 3 +++ test/unit/product_category_test.rb | 13 +++++++++++++ 2 files changed, 16 insertions(+), 0 deletions(-) diff --git a/app/models/product_category.rb b/app/models/product_category.rb index 1c05677..db73ae2 100644 --- a/app/models/product_category.rb +++ b/app/models/product_category.rb @@ -10,6 +10,9 @@ class ProductCategory < Category :joins => :products, :conditions => ['products.profile_id = ?', enterprise.id] }} + scope :by_environment, lambda { |environment| { + :conditions => ['environment_id = ?', environment.id] + }} scope :unique_by_level, lambda { |level| { :select => "DISTINCT ON (filtered_category) split_part(path, '/', #{level}) AS filtered_category, categories.*" }} diff --git a/test/unit/product_category_test.rb b/test/unit/product_category_test.rb index e100971..ce5972b 100644 --- a/test/unit/product_category_test.rb +++ b/test/unit/product_category_test.rb @@ -57,6 +57,19 @@ class ProductCategoryTest < ActiveSupport::TestCase assert_equivalent [c1,c2], scope end + should 'provide a scope based on the environment' do + alt_environment = fast_create(Environment) + c1 = ProductCategory.create!(:name => 'test cat 1', :environment => Environment.default) + c2 = ProductCategory.create!(:name => 'test cat 2', :environment => alt_environment) + c3 = ProductCategory.create!(:name => 'test cat 3', :environment => Environment.default) + + scope = ProductCategory.by_environment(alt_environment) + + assert_equal ActiveRecord::Relation, scope.class + assert_equivalent [c2], scope + assert_equivalent [c1,c3], ProductCategory.by_environment(Environment.default) + 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) -- libgit2 0.21.2