From 5ae8ad676f0cd2eee5bf00b41880beeae99c4f54 Mon Sep 17 00:00:00 2001 From: Libor Pichler Date: Mon, 6 May 2019 10:49:54 +0200 Subject: [PATCH] Add scope to list service templates from ancestor tenants --- app/models/service_template.rb | 5 +++++ spec/models/service_template_spec.rb | 28 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/app/models/service_template.rb b/app/models/service_template.rb index 1d7375cb5ed..077cac9ad9b 100644 --- a/app/models/service_template.rb +++ b/app/models/service_template.rb @@ -88,6 +88,11 @@ class ServiceTemplate < ApplicationRecord scope :displayed, -> { where(:display => true) } scope :public_service_templates, -> { where(:internal => [false, nil]) } + def self.with_tenant(tenant_id) + tenant = Tenant.find(tenant_id) + where(:tenant_id => tenant.ancestor_ids + [tenant_id]) + end + def self.with_additional_tenants includes(:service_template_tenants => :tenant) end diff --git a/spec/models/service_template_spec.rb b/spec/models/service_template_spec.rb index 6e8dfd2446b..6dc72618af0 100644 --- a/spec/models/service_template_spec.rb +++ b/spec/models/service_template_spec.rb @@ -1,6 +1,34 @@ describe ServiceTemplate do include_examples "OwnershipMixin" + describe ".with_tenant" do + # tenant_root + # \___ tenant_eye_bee_em (service_template_eye_bee_em) + # \__ subtenant_tenant_eye_bee_em_1 (service_template_1) + # \__ subtenant_tenant_eye_bee_em_1_1 (service_template_1_1, service_template_1_1a) + # \__ subtenant_tenant_eye_bee_em_3 (service_template_3, service_template_3_a) + + let!(:tenant_root) { Tenant.seed } + + let!(:tenant_eye_bee_em) { FactoryBot.create(:tenant, :parent => tenant_root) } + let!(:subtenant_tenant_eye_bee_em_1) { FactoryBot.create(:tenant, :parent => tenant_eye_bee_em) } + let!(:subtenant_tenant_eye_bee_em_3) { FactoryBot.create(:tenant, :parent => tenant_eye_bee_em) } + + let!(:subtenant_tenant_eye_bee_em_1_1) { FactoryBot.create(:tenant, :parent => subtenant_tenant_eye_bee_em_1) } + + let!(:service_template_eye_bee_em) { FactoryBot.create(:service_template, :tenant => tenant_eye_bee_em) } + let!(:service_template_1) { FactoryBot.create(:service_template, :tenant => subtenant_tenant_eye_bee_em_1) } + let!(:service_template_3) { FactoryBot.create(:service_template, :tenant => subtenant_tenant_eye_bee_em_3) } + let!(:service_template_3_a) { FactoryBot.create(:service_template, :tenant => subtenant_tenant_eye_bee_em_3) } + let!(:service_template_1_1) { FactoryBot.create(:service_template, :tenant => subtenant_tenant_eye_bee_em_1_1) } + let!(:service_template_1_1_a) { FactoryBot.create(:service_template, :tenant => subtenant_tenant_eye_bee_em_1_1) } + + it "lists ancestor service templates" do + expect(ServiceTemplate.with_tenant(subtenant_tenant_eye_bee_em_1_1.id).ids).to match_array([service_template_1_1.id, service_template_1_1_a.id, service_template_1.id, service_template_eye_bee_em.id]) + expect(ServiceTemplate.with_tenant(subtenant_tenant_eye_bee_em_3.id).ids).to match_array([service_template_3.id, service_template_3_a.id, service_template_eye_bee_em.id]) + end + end + let(:service_user) { FactoryBot.build(:user) } describe "#custom_actions" do