Class: Spree::Api::V2::Tenant::PreviewProductsController

Inherits:
ProductsController show all
Defined in:
app/controllers/spree/api/v2/tenant/preview_products_controller.rb

Instance Method Summary collapse

Methods inherited from BaseController

#current_vendor, #render_serialized_payload, #require_tenant

Instance Method Details

#scopeObject

override



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/controllers/spree/api/v2/tenant/preview_products_controller.rb', line 9

def scope
  taxon_type = model_class.reflect_on_association(:taxons).klass.polymorphic_name

  # IDs of products that have been explicitly assigned their own PreviewRole.
  products_with_own_roles = SpreeCmCommissioner::PreviewRole
                            .where(previewable_type: model_class.polymorphic_name)
                            .select(:previewable_id)

  # Base: all active preview products from the parent scope.
  base = super.where(status: :active, preview: true)

  # Path A: product has its own PreviewRole → user must hold that role directly.
  # Taxon membership is irrelevant for these products.
  via_product_role = base
                     .where(id: products_with_own_roles)
                     .where(id: spree_current_user.preview_roles
                                                    .where(previewable_type: model_class.polymorphic_name)
                                                    .select(:previewable_id)
                           )

  # Path B: product has no own PreviewRole → inherit access from its taxons.
  # Accessible if the user holds a preview role for any taxon the product belongs to.
  via_taxon = base
              .where.not(id: products_with_own_roles)
              .joins(:taxons)
              .where(spree_taxons: { id: spree_current_user.preview_roles
                                                             .where(previewable_type: taxon_type)
                                                             .select(:previewable_id)
}
                    )

  # via_taxon uses joins(:taxons) which is incompatible with or — wrap as subquery.
  via_product_role.or(base.where(id: via_taxon.select(:id)))
end