Commit 3d572c269bcb5738226582aa70549001b4af8c07
1 parent
c23f40da
Exists in
master
and in
23 other branches
[catalog-categories] Adding base names_scopes on product and category
Showing
4 changed files
with
108 additions
and
0 deletions
Show diff stats
app/models/category.rb
| @@ -13,6 +13,16 @@ class Category < ActiveRecord::Base | @@ -13,6 +13,16 @@ class Category < ActiveRecord::Base | ||
| 13 | {:conditions => ['parent_id is null and environment_id = ?', environment.id ]} | 13 | {:conditions => ['parent_id is null and environment_id = ?', environment.id ]} |
| 14 | } | 14 | } |
| 15 | 15 | ||
| 16 | + named_scope :on_level, lambda { |parent| {:conditions => {:parent_id => parent}} } | ||
| 17 | + | ||
| 18 | + named_scope :sub_categories, lambda { |category| | ||
| 19 | + {:conditions => ['categories.path LIKE ? AND categories.id != ?', "%#{category.slug}%", category.id]} | ||
| 20 | + } | ||
| 21 | + | ||
| 22 | + named_scope :sub_tree, lambda { |category| | ||
| 23 | + {:conditions => ['categories.path LIKE ?', "%#{category.slug}%"]} | ||
| 24 | + } | ||
| 25 | + | ||
| 16 | acts_as_filesystem | 26 | acts_as_filesystem |
| 17 | 27 | ||
| 18 | has_many :article_categorizations, :dependent => :destroy | 28 | has_many :article_categorizations, :dependent => :destroy |
app/models/product.rb
| @@ -23,6 +23,10 @@ class Product < ActiveRecord::Base | @@ -23,6 +23,10 @@ class Product < ActiveRecord::Base | ||
| 23 | 23 | ||
| 24 | named_scope :more_recent, :order => "created_at DESC" | 24 | named_scope :more_recent, :order => "created_at DESC" |
| 25 | 25 | ||
| 26 | + named_scope :from_category, lambda { |category| | ||
| 27 | + {:joins => :product_category, :conditions => ['categories.path LIKE ?', "%#{category.slug}%"]} if category | ||
| 28 | + } | ||
| 29 | + | ||
| 26 | after_update :save_image | 30 | after_update :save_image |
| 27 | 31 | ||
| 28 | def lat | 32 | def lat |
test/unit/category_test.rb
| @@ -552,4 +552,62 @@ class CategoryTest < ActiveSupport::TestCase | @@ -552,4 +552,62 @@ class CategoryTest < ActiveSupport::TestCase | ||
| 552 | cat.save! | 552 | cat.save! |
| 553 | end | 553 | end |
| 554 | 554 | ||
| 555 | + should 'return categories of a level' do | ||
| 556 | + c1 = fast_create(Category) | ||
| 557 | + c2 = fast_create(Category) | ||
| 558 | + c3 = fast_create(Category, :parent_id => c1) | ||
| 559 | + c4 = fast_create(Category, :parent_id => c1) | ||
| 560 | + c5 = fast_create(Category, :parent_id => c2) | ||
| 561 | + c6 = fast_create(Category, :parent_id => c3) | ||
| 562 | + | ||
| 563 | + assert_includes Category.on_level(nil), c1 | ||
| 564 | + assert_includes Category.on_level(nil), c2 | ||
| 565 | + assert_includes Category.on_level(c1), c3 | ||
| 566 | + assert_includes Category.on_level(c1), c4 | ||
| 567 | + assert_includes Category.on_level(c2), c5 | ||
| 568 | + assert_includes Category.on_level(c3), c6 | ||
| 569 | + end | ||
| 570 | + | ||
| 571 | + should 'on level named_scope must be able to receive parent or parent_id' do | ||
| 572 | + parent = fast_create(Category) | ||
| 573 | + category = fast_create(Category, :parent_id => parent) | ||
| 574 | + | ||
| 575 | + assert_includes Category.on_level(parent), category | ||
| 576 | + assert_includes Category.on_level(parent.id), category | ||
| 577 | + end | ||
| 578 | + | ||
| 579 | + should 'list category sub-categories' do | ||
| 580 | + c1 = Category.create!(:name => 'Category 1', :environment => Environment.default) | ||
| 581 | + c2 = Category.create!(:name => 'Category 2', :environment => Environment.default) | ||
| 582 | + c3 = Category.create!(:name => 'Category 3', :environment => Environment.default, :parent_id => c1) | ||
| 583 | + c4 = Category.create!(:name => 'Category 4', :environment => Environment.default, :parent_id => c1) | ||
| 584 | + c5 = Category.create!(:name => 'Category 5', :environment => Environment.default, :parent_id => c3) | ||
| 585 | + | ||
| 586 | + sub_categories = Category.sub_categories(c1) | ||
| 587 | + | ||
| 588 | + assert ActiveRecord::NamedScope::Scope, sub_categories.class | ||
| 589 | + assert_not_includes sub_categories, c1 | ||
| 590 | + assert_not_includes sub_categories, c2 | ||
| 591 | + assert_includes sub_categories, c3 | ||
| 592 | + assert_includes sub_categories, c4 | ||
| 593 | + assert_includes sub_categories, c5 | ||
| 594 | + end | ||
| 595 | + | ||
| 596 | + should 'list category sub-tree' do | ||
| 597 | + c1 = Category.create!(:name => 'Category 1', :environment => Environment.default) | ||
| 598 | + c2 = Category.create!(:name => 'Category 2', :environment => Environment.default) | ||
| 599 | + c3 = Category.create!(:name => 'Category 3', :environment => Environment.default, :parent_id => c1) | ||
| 600 | + c4 = Category.create!(:name => 'Category 4', :environment => Environment.default, :parent_id => c1) | ||
| 601 | + c5 = Category.create!(:name => 'Category 5', :environment => Environment.default, :parent_id => c3) | ||
| 602 | + | ||
| 603 | + sub_tree = Category.sub_tree(c1) | ||
| 604 | + | ||
| 605 | + assert ActiveRecord::NamedScope::Scope, sub_tree.class | ||
| 606 | + assert_includes sub_tree, c1 | ||
| 607 | + assert_not_includes sub_tree, c2 | ||
| 608 | + assert_includes sub_tree, c3 | ||
| 609 | + assert_includes sub_tree, c4 | ||
| 610 | + assert_includes sub_tree, c5 | ||
| 611 | + end | ||
| 612 | + | ||
| 555 | end | 613 | end |
test/unit/product_test.rb
| @@ -772,4 +772,40 @@ class ProductTest < ActiveSupport::TestCase | @@ -772,4 +772,40 @@ class ProductTest < ActiveSupport::TestCase | ||
| 772 | assert_equal [prod3, prod2, prod1], Product.more_recent | 772 | assert_equal [prod3, prod2, prod1], Product.more_recent |
| 773 | end | 773 | end |
| 774 | 774 | ||
| 775 | + should 'return products from a category' do | ||
| 776 | + pc1 = ProductCategory.create!(:name => 'PC1', :environment => Environment.default) | ||
| 777 | + pc2 = ProductCategory.create!(:name => 'PC2', :environment => Environment.default) | ||
| 778 | + pc3 = ProductCategory.create!(:name => 'PC3', :environment => Environment.default, :parent => pc1) | ||
| 779 | + p1 = fast_create(Product, :product_category_id => pc1) | ||
| 780 | + p2 = fast_create(Product, :product_category_id => pc1) | ||
| 781 | + p3 = fast_create(Product, :product_category_id => pc2) | ||
| 782 | + p4 = fast_create(Product, :product_category_id => pc3) | ||
| 783 | + | ||
| 784 | + products = Product.from_category(pc1) | ||
| 785 | + | ||
| 786 | + assert_includes products, p1 | ||
| 787 | + assert_includes products, p2 | ||
| 788 | + assert_not_includes products, p3 | ||
| 789 | + assert_includes products, p4 | ||
| 790 | + end | ||
| 791 | + | ||
| 792 | + should 'not crash if nil is passed to from_category' do | ||
| 793 | + assert_nothing_raised do | ||
| 794 | + Product.from_category(nil) | ||
| 795 | + end | ||
| 796 | + end | ||
| 797 | + | ||
| 798 | + should 'return from_category scope untouched if passed nil' do | ||
| 799 | + enterprise = fast_create(Enterprise) | ||
| 800 | + p1 = fast_create(Product, :enterprise_id => enterprise.id) | ||
| 801 | + p2 = fast_create(Product, :enterprise_id => enterprise.id) | ||
| 802 | + p3 = fast_create(Product, :enterprise_id => enterprise.id) | ||
| 803 | + | ||
| 804 | + products = enterprise.products.from_category(nil) | ||
| 805 | + | ||
| 806 | + assert_includes products, p1 | ||
| 807 | + assert_includes products, p2 | ||
| 808 | + assert_includes products, p3 | ||
| 809 | + end | ||
| 810 | + | ||
| 775 | end | 811 | end |