From ea8bbd54bcb98cb24d249c49df6724985ac6cdb8 Mon Sep 17 00:00:00 2001 From: lpichler Date: Fri, 19 Oct 2018 13:50:42 +0200 Subject: [PATCH] Use create method for arrays to create product features --- app/models/miq_product_feature.rb | 17 +++++++----- app/models/tenant.rb | 8 +++--- db/fixtures/miq_product_features.yml | 12 ++++++--- spec/models/miq_product_feature_spec.rb | 36 ++++++++++++++++++------- 4 files changed, 49 insertions(+), 24 deletions(-) diff --git a/app/models/miq_product_feature.rb b/app/models/miq_product_feature.rb index 4ba4a29b781..5be2cf78c6f 100644 --- a/app/models/miq_product_feature.rb +++ b/app/models/miq_product_feature.rb @@ -24,12 +24,13 @@ class MiqProductFeature < ApplicationRecord :feature_type, :hidden, :protected, - :tenant_id + :tenant_id, + :tenant_node ] - FEATURE_TYPE_ORDER = %w(view control admin node tenant tenant_node).freeze + FEATURE_TYPE_ORDER = %w(view control admin node).freeze REQUIRED_ATTRIBUTES = [:identifier].freeze - OPTIONAL_ATTRIBUTES = [:name, :feature_type, :description, :children, :hidden, :protected].freeze + OPTIONAL_ATTRIBUTES = [:name, :feature_type, :description, :children, :hidden, :protected, :tenant_node].freeze ALLOWED_ATTRIBUTES = (REQUIRED_ATTRIBUTES + OPTIONAL_ATTRIBUTES).freeze def name @@ -47,7 +48,7 @@ def self.tenant_identifier(identifier, tenant_id) end def self.current_tenant_identifier(identifier) - identifier && feature_details(identifier) && feature_details(identifier)[:feature_type] == "tenant_node" ? tenant_identifier(identifier, User.current_tenant.id) : identifier + identifier && feature_details(identifier) && feature_details(identifier)[:tenant_node] ? tenant_identifier(identifier, User.current_tenant.id) : identifier end def self.feature_yaml(path = FIXTURE_PATH) @@ -133,13 +134,15 @@ def self.seed end def self.with_tenant_node_features - where(:feature_type => 'tenant_node') + where(:tenant_node => true) end def self.seed_tenant_miq_product_features - with_tenant_node_features.map.each do |tenant_miq_product_feature| - Tenant.all.map { |tenant| tenant.create_miq_product_feature(tenant_miq_product_feature).identifier } + result = with_tenant_node_features.map.each do |tenant_miq_product_feature| + Tenant.all.map { |tenant| tenant.create_miq_product_feature(tenant_miq_product_feature) } end.flatten + + MiqProductFeature.create(result).map(&:identifier) end def self.seed_features(path = FIXTURE_PATH) diff --git a/app/models/tenant.rb b/app/models/tenant.rb index 8186f481624..d3520808a67 100644 --- a/app/models/tenant.rb +++ b/app/models/tenant.rb @@ -293,9 +293,11 @@ def allowed? def create_miq_product_features_for_tenant_nodes result = MiqProductFeature.with_tenant_node_features.map.each do |tenant_miq_product_feature| - create_miq_product_feature(tenant_miq_product_feature).identifier + create_miq_product_feature(tenant_miq_product_feature) end.flatten + result = MiqProductFeature.create(result).map(&:identifier) + MiqProductFeature.invalidate_caches result end @@ -303,14 +305,14 @@ def create_miq_product_features_for_tenant_nodes def create_miq_product_feature(miq_product_feature) attrs = { :name => miq_product_feature.name, :description => miq_product_feature.description, - :feature_type => 'tenant', + :feature_type => 'admin', :hidden => false, :identifier => MiqProductFeature.tenant_identifier(miq_product_feature.identifier, id), :tenant_id => id, :parent_id => miq_product_feature.id } - MiqProductFeature.find_or_create_by(attrs) + attrs end def destroy_tenant_feature_for_tenant_node diff --git a/db/fixtures/miq_product_features.yml b/db/fixtures/miq_product_features.yml index b39b9078ec2..dd41f1ef09b 100644 --- a/db/fixtures/miq_product_features.yml +++ b/db/fixtures/miq_product_features.yml @@ -2451,22 +2451,26 @@ :children: - :name: Add :description: Add Dialog in the Dialog Editor - :feature_type: tenant_node + :feature_type: node + :tenant_node: true :hidden: false :identifier: dialog_new_editor - :name: Edit :description: Edit Dialog in the Dialog Editor - :feature_type: tenant_node + :feature_type: node + :tenant_node: true :hidden: false :identifier: dialog_edit_editor - :name: Copy :description: Copy Dialog - :feature_type: tenant_node + :feature_type: node + :tenant_node: true :hidden: false :identifier: dialog_copy_editor - :name: Delete :description: Delete Dialog - :feature_type: tenant_node + :feature_type: node + :tenant_node: true :hidden: false :identifier: dialog_delete - :name: Provisioning Dialogs diff --git a/spec/models/miq_product_feature_spec.rb b/spec/models/miq_product_feature_spec.rb index 996612b5ddd..3aac0755c52 100644 --- a/spec/models/miq_product_feature_spec.rb +++ b/spec/models/miq_product_feature_spec.rb @@ -2,6 +2,18 @@ require 'pathname' describe MiqProductFeature do + let(:miq_product_feature_class) do + Class.new(described_class) do + def self.with_parent_tenant_nodes + includes(:parent).where(:parents_miq_product_features => {:tenant_node => true } ) + end + + def self.tenant_features_in_hash + with_parent_tenant_nodes.map { |x| x.slice(:name, :description, :identifier, :tenant_id) } + end + end + end + # - container_dashboard # - miq_report_widget_editor # - miq_report_widget_admin @@ -120,7 +132,8 @@ def traverse_product_features(product_feature, &block) :name => "One", :children => [ { - :feature_type => "tenant_node", + :feature_type => "admin", + :tenant_node => true, :identifier => "one_edit", :name => "Edit", :description => "XXX" @@ -143,14 +156,14 @@ def traverse_product_features(product_feature, &block) end it "creates tenant features" do - features = MiqProductFeature.where(:feature_type => 'tenant').map { |x| x.slice(:name, :description, :identifier, :tenant_id) } + features = miq_product_feature_class.tenant_features_in_hash expect(features).to match_array([{ "name" => "Edit (#{root_tenant.name})", "description" => "XXX for tenant #{root_tenant.name}", "identifier" => "one_edit_tenant_#{root_tenant.id}", "tenant_id" => root_tenant.id}, {"name" => "Edit (#{tenant.name})", "description" => "XXX for tenant #{tenant.name}", "identifier" => "one_edit_tenant_#{tenant.id}", "tenant_id" => tenant.id}]) - expect(MiqProductFeature.where(:feature_type => "tenant_node", :identifier => "one_edit", :name => "Edit").count).to eq(1) + expect(MiqProductFeature.where(:tenant_node => true, :identifier => "one_edit", :name => "Edit").count).to eq(1) end context "add tenant node product features" do @@ -165,15 +178,17 @@ def traverse_product_features(product_feature, &block) :name => "One", :children => [ { - :feature_type => "tenant_node", + :feature_type => "admin", :identifier => "one_edit", + :tenant_node => true, :name => "Edit", :description => "XXX" } ] }, { - :feature_type => "tenant_node", + :feature_type => "admin", + :tenant_node => true, :identifier => "two_edit", :name => "Add", :description => "YYY" @@ -183,7 +198,7 @@ def traverse_product_features(product_feature, &block) end it "add new tenant feature" do - features = MiqProductFeature.where(:feature_type => 'tenant').map { |x| x.slice(:name, :description, :identifier, :tenant_id) } + features = miq_product_feature_class.tenant_features_in_hash expect(features).to match_array([{ "name" => "Edit (#{root_tenant.name})", "description" => "XXX for tenant #{root_tenant.name}", "identifier" => "one_edit_tenant_#{root_tenant.id}", "tenant_id" => root_tenant.id}, @@ -194,7 +209,7 @@ def traverse_product_features(product_feature, &block) {"name" => "Add (#{tenant.name})", "description" => "YYY for tenant #{tenant.name}", "identifier" => "two_edit_tenant_#{tenant.id}", "tenant_id" => tenant.id}]) - expect(MiqProductFeature.where(:feature_type => "tenant_node", :identifier => "two_edit", :name => "Add").count).to eq(1) + expect(MiqProductFeature.where(:tenant_node => true, :identifier => "two_edit", :name => "Add").count).to eq(1) end context "remove added tenant feaure" do @@ -209,7 +224,8 @@ def traverse_product_features(product_feature, &block) :name => "One", :children => [ { - :feature_type => "tenant_node", + :feature_type => "admin", + :tenant_node => true, :identifier => "one_edit", :name => "Edit", :description => "XXX" @@ -221,14 +237,14 @@ def traverse_product_features(product_feature, &block) end it "removes tenant features" do - features = MiqProductFeature.where(:feature_type => 'tenant').map { |x| x.slice(:name, :description, :identifier, :tenant_id) } + features = miq_product_feature_class.tenant_features_in_hash expect(features).to match_array([{ "name" => "Edit (#{root_tenant.name})", "description" => "XXX for tenant #{root_tenant.name}", "identifier" => "one_edit_tenant_#{root_tenant.id}", "tenant_id" => root_tenant.id}, {"name" => "Edit (#{tenant.name})", "description" => "XXX for tenant #{tenant.name}", "identifier" => "one_edit_tenant_#{tenant.id}", "tenant_id" => tenant.id}]) - expect(MiqProductFeature.where(:feature_type => "tenant_node", :identifier => "one_edit", :name => "Edit").count).to eq(1) + expect(MiqProductFeature.where(:tenant_node => true, :identifier => "one_edit", :name => "Edit").count).to eq(1) end end end