Skip to content

Commit

Permalink
Improve and refactor methods that determine select state.
Browse files Browse the repository at this point in the history
  • Loading branch information
hayesr committed Jan 16, 2017
1 parent 141775a commit 87fbe37
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 154 deletions.
15 changes: 4 additions & 11 deletions app/presenters/tree_builder_ops_rbac_features.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def set_locals_for_render
locals = {
:checkboxes => true,
:three_checks => true,
:check_url => "/ops/rbac_role_field_changed/"
:check_url => "/ops/rbac_role_field_changed/",
:onclick => nil
}

if editable
Expand Down Expand Up @@ -105,8 +106,8 @@ def root_options

def root_node
@root_node ||= {
:key => root_key,
:title => root_title,
:key => "#{node_id_prefix}__#{root_feature}",
:title => _(root_details[:name]),
:tooltip => root_tooltip,
:expand => true,
:cfmeNoClick => true,
Expand All @@ -123,14 +124,6 @@ def root_tooltip
_(root_details[:description]) || _(root_details[:name])
end

def root_title
_(root_details[:name])
end

def root_key
"#{node_id_prefix}__#{root_feature}"
end

def root_select_state
features.include?(root_feature) || select_state_from_counter
end
Expand Down
60 changes: 15 additions & 45 deletions app/presenters/tree_node/menu/item.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module TreeNode
module Menu
class Item < Node
class Item < TreeNode::Menu::Node
set_attribute(:key) { "#{@options[:node_id_prefix]}__#{@object.feature}" }

set_attribute(:title) { _(details[:name]) }
Expand All @@ -9,60 +9,30 @@ class Item < Node

set_attribute(:image, "100/feature_node")

set_attribute(:checkable) { @options[:editable] }

set_attribute(:selected) do
self_selected? || select_by_kids_selected
def select_state
self_selected? || select_by_children_selected
end

set_attribute(:no_click, true)

def selected?
self_selected? || any_children_selected?
def children
@children ||= begin
if feature
feature.children.flat_map do |child|
TreeNode::MiqProductFeature.new(child, key, @options)
end
else
[]
end
end
end

def self_selected?
@options[:features].include?(@object.feature)
end

def fully_selected?
self_selected?
end

def partially_selected?
any_children_selected?
end

private

def any_children_selected?
feature_children.any? do |feature|
@options[:features].include?(feature)
end
end

def all_children_selected?
feature_children.all? do |feature|
@options[:features].include?(feature)
end
end

def select_by_kids_selected
return if feature_children.none?

if all_children_selected?
return true
end

if any_children_selected?
return 'undefined'
end

false
end

def feature_children
::MiqProductFeature.feature_children(@object.feature)
def feature
@feature ||= ::MiqProductFeature.find_by_identifier(@object.feature)
end

def details
Expand Down
38 changes: 38 additions & 0 deletions app/presenters/tree_node/menu/node.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module TreeNode
module Menu
class Node < TreeNode::Node
set_attribute(:no_click, true)
set_attribute(:checkable) { @options[:editable] }
set_attribute(:selected) { select_state }

def self_and_child_states
child_states.unshift(select_state)
end

private

def child_states
@states ||= children.flat_map(&:self_and_child_states)
end

def select_by_children_selected
return true if all_children_fully_selected?
return 'undefined' if any_children_selected?

false
end

def all_children_fully_selected?
return false if children.empty?

child_states.all? { |s| s == true } # true not just truthy
end

def any_children_selected?
return false if children.empty?

child_states.any?
end
end
end
end
60 changes: 8 additions & 52 deletions app/presenters/tree_node/menu/section.rb
Original file line number Diff line number Diff line change
@@ -1,72 +1,28 @@
module TreeNode
module Menu
class Section < Node
class Section < TreeNode::Menu::Node
set_attribute(:key) { "#{@options[:node_id_prefix]}___tab_#{@object.id}" }

set_attribute(:title) { _(@object.name) }

set_attribute(:image) { "100/feature_node" }

set_attribute(:checkable) { @options[:editable] }

set_attribute(:selected) do
select_state_from_descendents
end

set_attribute(:tooltip) { _("%{title} Main Tab") % {:title => @object.name} }

set_attribute(:no_click, true)

private

def select_state_from_descendents
return false if children.none?

return true if all_selected?

return 'undefined' if any_selected?
end

def all_selected?
return false if children.empty?

children.all? do |child|
child_selected?(child)
end
end

def any_selected?
return false if children.empty?

children.any? do |child|
child_selected?(child)
end
def select_state
select_by_children_selected
end

def children
@children ||= begin
@object.items.flat_map { |i| collect_children(i) }
@object.items.flat_map do |item|
node = TreeNode.new(item, nil, @options)
[node].concat(node.children)
end
end
end

def collect_children(item)
if item.kind_of?(::Menu::Section)
item.items.flat_map { |i| collect_children(i) }.unshift(item)
else
item
end
end

def child_selected?(child)
case child
when ::Menu::Section
child.items.any? { |item| child_selected?(item) }
when ::Menu::Item
TreeNode::Menu::Item.new(child, key, @options).selected?
else
@options[:features].include?(child)
end
end
# End class
end
end
end
59 changes: 13 additions & 46 deletions app/presenters/tree_node/miq_product_feature.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module TreeNode
class MiqProductFeature < Node
# Current assumption is that this builder would only be used in Rbac Features tree
#
class MiqProductFeature < TreeNode::Menu::Node
set_attribute(:key) { "#{@options[:node_id_prefix]}__#{@object.identifier}" }

set_attribute(:title) { _(@object.name) }
Expand All @@ -8,28 +10,24 @@ class MiqProductFeature < Node

set_attribute(:image) { "100/feature_#{@object.feature_type}.png" }

set_attribute(:checkable) { @options[:editable] }
def select_state
self_selected? || parent_selected? || select_by_children_selected
end

set_attribute(:selected) do
self_selected? || parent_selected? || select_by_kids_selected
def children
@children ||= begin
@object.children.map do |child|
TreeNode::MiqProductFeature.new(child, key, @options)
end
end
end

set_attribute(:no_click, true)
private

def self_selected?
@options[:features].include?(identifier_without_accords)
end

def fully_selected?
self_selected? || all_children_selected?
end

def partially_selected?
any_children_selected?
end

private

def identifier_without_accords
@object.identifier.sub(/_accords$/, '')
end
Expand All @@ -38,13 +36,6 @@ def parent
::Menu::Manager.item(@parent_id.split('_').last)
end

def select_by_kids_selected
return true if all_children_selected?
return 'undefined' if any_children_selected?

false
end

def parent_selected?
if parent.nil?
# not a Menu::Item, try something else.
Expand All @@ -55,29 +46,5 @@ def parent_selected?
TreeNode::Menu::Item.new(parent, nil, @options).self_selected?
end
end

def any_children_selected?
return false if @object.children.none?

child_nodes.any? do |node|
node.self_selected? || node.partially_selected?
end
end

def all_children_selected?
return false if @object.children.none?

child_nodes.all? do |node|
node.self_selected? || node.fully_selected?
end
end

def child_nodes
@child_nodes ||= begin
@object.children.map do |child|
TreeNode::MiqProductFeature.new(child, key, @options)
end
end
end
end
end

0 comments on commit 87fbe37

Please sign in to comment.