Skip to content

Commit

Permalink
Use create method for arrays to create product features
Browse files Browse the repository at this point in the history
  • Loading branch information
lpichler committed Oct 23, 2018
1 parent c9b563a commit ea8bbd5
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 24 deletions.
17 changes: 10 additions & 7 deletions app/models/miq_product_feature.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions app/models/tenant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -293,24 +293,26 @@ 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

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
Expand Down
12 changes: 8 additions & 4 deletions db/fixtures/miq_product_features.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 26 additions & 10 deletions spec/models/miq_product_feature_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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"
Expand All @@ -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},
Expand All @@ -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
Expand All @@ -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"
Expand All @@ -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
Expand Down

0 comments on commit ea8bbd5

Please sign in to comment.