Commit 6b9d32ebb18b6cf482b1bd7cf42e2c0e25168d7e

Authored by Larissa Reis
1 parent ad416827

api: scope to fetch visible products for person

app/models/product.rb
... ... @@ -51,6 +51,25 @@ class Product < ActiveRecord::Base
51 51 {:joins => :product_category, :conditions => ['categories.path LIKE ?', "%#{category.slug}%"]} if category
52 52 }
53 53  
  54 + scope :visible_for_person, lambda { |person|
  55 + joins('INNER JOIN "profiles" enterprises ON enterprises."id" = "products"."profile_id"')
  56 + .joins('LEFT JOIN "role_assignments" ON ("role_assignments"."resource_id" = enterprises."id"
  57 + AND "role_assignments"."resource_type" = \'Profile\') OR (
  58 + "role_assignments"."resource_id" = enterprises."environment_id" AND
  59 + "role_assignments"."resource_type" = \'Environment\' )')
  60 + .joins('LEFT JOIN "roles" ON "role_assignments"."role_id" = "roles"."id"')
  61 + .where(
  62 + ['( (roles.key = ? OR roles.key = ?) AND role_assignments.accessor_type = \'Profile\' AND role_assignments.accessor_id = ? )
  63 + OR
  64 + ( ( ( role_assignments.accessor_type = \'Profile\' AND
  65 + role_assignments.accessor_id = ? ) OR
  66 + ( enterprises.public_profile = ? AND enterprises.enabled = ? ) ) AND
  67 + ( enterprises.visible = ? ) )',
  68 + 'profile_admin', 'environment_administrator', person.id, person.id,
  69 + true, true, true]
  70 + ).uniq
  71 + }
  72 +
54 73 after_update :save_image
55 74  
56 75 def lat
... ...
test/unit/product_test.rb
... ... @@ -577,4 +577,68 @@ class ProductTest < ActiveSupport::TestCase
577 577 assert_includes products, p3
578 578 end
579 579  
  580 + should 'fetch products from organizations that are visible for a user' do
  581 + person = create_user('some-person').person
  582 + admin = create_user('some-admin').person
  583 + env_admin = create_user('env-admin').person
  584 + env = Environment.default
  585 +
  586 + e1 = fast_create(Enterprise, :public_profile => true , :visible => true)
  587 + p1 = fast_create(Product, :profile_id => e1.id)
  588 + e1.affiliate(admin, Profile::Roles.admin(env.id))
  589 + e1.affiliate(person, Profile::Roles.member(env.id))
  590 +
  591 + e2 = fast_create(Enterprise, :public_profile => true , :visible => true)
  592 + p2 = fast_create(Product, :profile_id => e2.id)
  593 + e3 = fast_create(Enterprise, :public_profile => false, :visible => true)
  594 + p3 = fast_create(Product, :profile_id => e3.id)
  595 +
  596 + e4 = fast_create(Enterprise, :public_profile => false, :visible => true)
  597 + p4 = fast_create(Product, :profile_id => e4.id)
  598 + e4.affiliate(admin, Profile::Roles.admin(env.id))
  599 + e4.affiliate(person, Profile::Roles.member(env.id))
  600 +
  601 + e5 = fast_create(Enterprise, :public_profile => true, :visible => false)
  602 + p5 = fast_create(Product, :profile_id => e5.id)
  603 + e5.affiliate(admin, Profile::Roles.admin(env.id))
  604 + e5.affiliate(person, Profile::Roles.member(env.id))
  605 +
  606 + e6 = fast_create(Enterprise, :enabled => false, :visible => true)
  607 + p6 = fast_create(Product, :profile_id => e6.id)
  608 + e6.affiliate(admin, Profile::Roles.admin(env.id))
  609 +
  610 + e7 = fast_create(Enterprise, :public_profile => false, :visible => false)
  611 + p7 = fast_create(Product, :profile_id => e7.id)
  612 +
  613 + Environment.default.add_admin(env_admin)
  614 +
  615 + products_person = Product.visible_for_person(person)
  616 + products_admin = Product.visible_for_person(admin)
  617 + products_env_admin = Product.visible_for_person(env_admin)
  618 +
  619 + assert_includes products_person, p1
  620 + assert_includes products_admin, p1
  621 + assert_includes products_env_admin, p1
  622 +
  623 + assert_includes products_person, p2
  624 + assert_includes products_env_admin, p2
  625 + assert_not_includes products_person, p3
  626 + assert_includes products_env_admin, p3
  627 +
  628 + assert_includes products_person, p4
  629 + assert_includes products_admin, p4
  630 + assert_includes products_env_admin, p4
  631 +
  632 + assert_not_includes products_person, p5
  633 + assert_includes products_admin, p5
  634 + assert_includes products_env_admin, p5
  635 +
  636 + assert_not_includes products_person, p6
  637 + assert_includes products_admin, p6
  638 + assert_includes products_env_admin, p6
  639 +
  640 + assert_not_includes products_person, p7
  641 + assert_includes products_env_admin, p7
  642 + end
  643 +
580 644 end
... ...