Skip to content

Commit

Permalink
Merge pull request #6220 from skateman/treenode-override-obj
Browse files Browse the repository at this point in the history
Call TreeBuilder#override on TreeNode objects instead of hashes
  • Loading branch information
martinpovolny authored Sep 24, 2019
2 parents ed49463 + 35425f5 commit 4862957
Show file tree
Hide file tree
Showing 19 changed files with 41 additions and 78 deletions.
13 changes: 3 additions & 10 deletions app/presenters/tree_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,6 @@ def self.tree_add_child_nodes(sandbox:, klass_name:, name:, id:)

private

# Temporary method to append the no-cursor class to an already existing CSS class
# list. Eventually it should be removed after all the code from the override methods
# is moved into the TreeNode as a DSL.
def append_no_cursor(klass)
(klass || '').split(' ').push('no-cursor').join(' ')
end

def build_tree
@tree_nodes = x_build_tree
active_node_set(@tree_nodes)
Expand Down Expand Up @@ -230,10 +223,10 @@ def x_build_node(object, pid)
end

def x_build_single_node(object, pid)
# FIXME: to_h is for backwards compatibility with hash-trees, it needs to be removed in the future
node = TreeNode.new(object, pid, self).to_h
node = TreeNode.new(object, pid, self)
override(node, object) if self.class.method_defined?(:override) || self.class.private_method_defined?(:override)
node
# FIXME: to_h is for backwards compatibility with hash-trees, it needs to be removed in the future
node.to_h
end

# Handle custom tree nodes (object is a Hash)
Expand Down
3 changes: 1 addition & 2 deletions app/presenters/tree_builder_alert_profile_obj.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ def initialize(name, sandbox, build = true, **params)
end

def override(node, object)
node[:state] ||= {}
node[:state][:checked] = @selected.try(:include?, object.id)
node.checked = @selected.try(:include?, object.id)
end

def tree_init_options
Expand Down
5 changes: 1 addition & 4 deletions app/presenters/tree_builder_automate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ def x_get_tree_roots
end

def override(node, object)
if object.kind_of?(MiqAeNamespace) && object.domain?
node[:selectable] = false
node[:class] = append_no_cursor(node[:class])
end
node.selectable = false if object.kind_of?(MiqAeNamespace) && object.domain?
end

def x_get_tree_class_kids(object, count_only)
Expand Down
5 changes: 1 addition & 4 deletions app/presenters/tree_builder_automate_catalog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ def x_get_tree_roots

def override(node, object)
# Only the instance items should be clickable when selecting a catalog item entry point
unless object.kind_of?(MiqAeInstance)
node[:selectable] = false
node[:class] = append_no_cursor(node[:class])
end
node.selectable = false unless object.kind_of?(MiqAeInstance)
end

def x_get_tree_ns_kids(object, count_only)
Expand Down
2 changes: 1 addition & 1 deletion app/presenters/tree_builder_automate_entrypoint.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class TreeBuilderAutomateEntrypoint < TreeBuilderAutomateCatalog
def override(node, _object)
node.delete(:selectable)
node.selectable = nil
end

def root_options
Expand Down
10 changes: 4 additions & 6 deletions app/presenters/tree_builder_belongs_to_hac.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ class TreeBuilderBelongsToHac < TreeBuilder
has_kids_for ResourcePool, [:x_get_resource_pool_kids]

def override(node, object)
node[:state] ||= {}
node[:state][:checked] = @selected_nodes&.include?("#{object.class.name}_#{object[:id]}")
node[:hideCheckbox] = true if object.kind_of?(Host) && object.ems_cluster_id.present?
node[:selectable] = false
node[:class] = append_no_cursor(node[:class])
node[:checkable] = @edit.present?
node.checked = @selected_nodes&.include?("#{object.class.name}_#{object[:id]}")
node.hide_checkbox = true if object.kind_of?(Host) && object.ems_cluster_id.present?
node.selectable = false
node.checkable = @edit.present?
end

def initialize(name, sandbox, build, **params)
Expand Down
12 changes: 5 additions & 7 deletions app/presenters/tree_builder_belongs_to_vat.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
class TreeBuilderBelongsToVat < TreeBuilderBelongsToHac
def override(node, object)
node[:selectable] = false
node[:class] = append_no_cursor(node[:class])
node[:checkable] = @edit.present? || @assign_to.present?
node.selectable = false
node.checkable = @edit.present? || @assign_to.present?

if object.kind_of?(EmsFolder) && object.vm_folder?
node[:icon] = "pficon pficon-folder-close-blue"
node.icon = "pficon pficon-folder-close-blue"
else
node[:hideCheckbox] = true
node.hide_checkbox = true
end
node[:state] ||= {}
node[:state][:checked] = @selected_nodes&.include?("EmsFolder_#{object[:id]}")
node.checked = @selected_nodes&.include?("EmsFolder_#{object[:id]}")
end

def x_get_tree_datacenter_kids(parent, count_only)
Expand Down
3 changes: 1 addition & 2 deletions app/presenters/tree_builder_compliance_history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ class TreeBuilderComplianceHistory < TreeBuilder
has_kids_for ComplianceDetail, %i[x_get_compliance_detail_kids parents]

def override(node, _object)
node[:selectable] = false
node[:class] = append_no_cursor(node[:class])
node.selectable = false
end

def initialize(name, sandbox, build = true, **params)
Expand Down
10 changes: 4 additions & 6 deletions app/presenters/tree_builder_ems_folders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ class TreeBuilderEmsFolders < TreeBuilderAlertProfileAssign
ANCESTRY_TYPE = EmsFolder

def override(node, object)
node[:selectable] = false
node[:class] = append_no_cursor(node[:class])
node.selectable = false
if object.kind_of?(EmsFolder) && object.vm_folder?
node[:icon] = "pficon pficon-folder-close-blue"
node.icon = "pficon pficon-folder-close-blue"
else
node[:hideCheckbox] = true
node.hide_checkbox = true
end
node[:state] ||= {}
node[:state][:checked] = @selected_nodes&.include?("EmsFolder_#{object[:id]}")
node.checked = @selected_nodes&.include?("EmsFolder_#{object[:id]}")
end
end
5 changes: 1 addition & 4 deletions app/presenters/tree_builder_infra_networking.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ class TreeBuilderInfraNetworking < TreeBuilder
has_kids_for EmsFolder, [:x_get_tree_folder_kids]

def override(node, object)
if object.kind_of?(Lan)
node[:selectable] = false
node[:class] = append_no_cursor(node[:class])
end
node.selectable = false if object.kind_of?(Lan)
end

private
Expand Down
5 changes: 1 addition & 4 deletions app/presenters/tree_builder_network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ class TreeBuilderNetwork < TreeBuilder
has_kids_for Switch, [:x_get_tree_switch_kids]

def override(node, object)
unless object.kind_of?(::VmOrTemplate)
node[:selectable] = false
node[:class] = append_no_cursor(node[:class])
end
node.selectable = false unless object.kind_of?(::VmOrTemplate)
end

def initialize(name, sandbox, build = true, **params)
Expand Down
10 changes: 4 additions & 6 deletions app/presenters/tree_builder_resource_pools.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ class TreeBuilderResourcePools < TreeBuilderAlertProfileAssign
ANCESTRY_TYPE = ResourcePool

def override(node, object)
node[:selectable] = false
node[:class] = append_no_cursor(node[:class])
node[:checkable] = true
node[:hideCheckbox] = true unless object.kind_of?(ResourcePool)
node[:state] ||= {}
node[:state][:checked] = @selected_nodes&.include?("ResourcePool_#{object[:id]}")
node.selectable = false
node.checkable = true
node.hide_checkbox = true unless object.kind_of?(ResourcePool)
node.checked = @selected_nodes&.include?("ResourcePool_#{object[:id]}")
end
end
5 changes: 2 additions & 3 deletions app/presenters/tree_builder_roles_by_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ def x_get_tree_roots
end

def override(node, _object)
if @sb[:diag_selected_id] && node[:key] == "#{self.class.get_prefix_for_model(@sb[:diag_selected_model]) || 'svr'}-#{@sb[:diag_selected_id]}"
node[:state] ||= {}
node[:state][:selected] = true
if @sb[:diag_selected_id] && node.key == "#{self.class.get_prefix_for_model(@sb[:diag_selected_model]) || 'svr'}-#{@sb[:diag_selected_id]}"
node.selected = true
end
end

Expand Down
5 changes: 2 additions & 3 deletions app/presenters/tree_builder_servers_by_role.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ def x_get_tree_roots
end

def override(node, _object)
if @sb[:diag_selected_id] && node[:key] == "role-#{@sb[:diag_selected_id]}"
node[:state] ||= {}
node[:state][:selected] = true
if @sb[:diag_selected_id] && node.key == "role-#{@sb[:diag_selected_id]}"
node.selected = true
end
end

Expand Down
3 changes: 1 addition & 2 deletions app/presenters/tree_builder_smartproxy_affinity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ def initialize(name, sandbox, build = true, **params)
private

def override(node, _object)
node[:selectable] = false
node[:class] = append_no_cursor(node[:class])
node.selectable = false
end

def tree_init_options
Expand Down
3 changes: 1 addition & 2 deletions app/presenters/tree_builder_storage_adapters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ def initialize(name, sandbox, build = true, **params)
end

def override(node, _object)
node[:selectable] = false
node[:class] = append_no_cursor(node[:class])
node.selectable = false
end

private
Expand Down
5 changes: 2 additions & 3 deletions app/presenters/tree_builder_tenants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ def x_get_tree_tenant_kids(object, count_only)
end

def override(node, object)
node[:state] ||= {}
node[:state][:checked] = @additional_tenants.try(:include?, object)
node[:checkable] = @selectable
node.checked = @additional_tenants.try(:include?, object)
node.checkable = @selectable
end
end
5 changes: 1 addition & 4 deletions app/presenters/tree_builder_utilization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ def tree_init_options
end

def override(node, _object)
if node[:key].split('-')[1].split('_')[0] == 'folder'
node[:selectable] = false
node[:class] = append_no_cursor(node[:class])
end
node.selectable = false if node.key.split('-')[1].split('_')[0] == 'folder'
end

def root_options
Expand Down
10 changes: 5 additions & 5 deletions spec/presenters/tree_builder_alert_profile_obj_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,20 @@
end

describe '#override' do
let(:node) { {} }
let(:node) { TreeNode.new({}) }

it 'sets node1' do
subject.send(:override, node, tag1a)
expect(node[:hideCheckbox]).to be_falsey
expect(node[:state][:checked]).to be_truthy
expect(node.to_h[:hideCheckbox]).to be_falsey
expect(node.to_h[:state][:checked]).to be_truthy
end
it 'sets node2' do
subject.send(:override, node, tag2a)
expect(node[:state][:checked]).to be_truthy
expect(node.to_h[:state][:checked]).to be_truthy
end
it 'sets node3' do
subject.send(:override, node, tag3a)
expect(node[:state][:checked]).to be_falsey
expect(node.to_h[:state][:checked]).to be_falsey
end
end

Expand Down

0 comments on commit 4862957

Please sign in to comment.