Skip to content

Commit

Permalink
Merge pull request #704 from romanblanco/button_order
Browse files Browse the repository at this point in the history
Make method 'button_order?' return boolean
  • Loading branch information
martinpovolny authored Mar 20, 2017
2 parents 7372e99 + 226652f commit adefa56
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 66 deletions.
31 changes: 31 additions & 0 deletions app/presenters/custom_buttons_mixin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module CustomButtonsMixin
extend ActiveSupport::Concern
def get_tree_aset_kids_for_nil_id(object, count_only)
count_only ? get_custom_buttons(object).count : get_custom_buttons(object).sort_by { |a| a.name.downcase }
end

def buttons_ordered?(object)
!!(object[:set_data] && object[:set_data][:button_order])
end

def get_custom_buttons(object)
# FIXME: don't we have a method for the splits?
# FIXME: cannot we ask for the null parent using Arel?
CustomButton.buttons_for(object.name.split('|').last.split('-').last).select do |uri|
uri.parent.nil?
end
end

def x_get_tree_aset_kids(object, count_only)
if object.id.nil?
get_tree_aset_kids_for_nil_id(object, count_only)
elsif count_only
object.members.count
else
button_order = buttons_ordered?(object) ? object[:set_data][:button_order] : nil
Array(button_order).each_with_object([]) do |bidx, arr|
object.members.each { |b| arr.push(b) if bidx == b.id && !arr.include?(b) }
end
end
end
end
33 changes: 1 addition & 32 deletions app/presenters/tree_builder_buttons.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class TreeBuilderButtons < TreeBuilderAeCustomization
include CustomButtonsMixin
has_kids_for CustomButtonSet, [:x_get_tree_aset_kids]

private
Expand Down Expand Up @@ -35,38 +36,6 @@ def x_get_tree_custom_kids(object, count_only, _options)
count_only_or_objects(count_only, objects)
end

def get_custom_buttons(object)
# FIXME: don't we have a method for the splits?
# FIXME: cannot we ask for the null parent using Arel?
CustomButton.buttons_for(object.name.split('|').last.split('-').last).select do |uri|
uri.parent.nil?
end
end

def get_tree_aset_kids_for_nil_id(object, count_only)
count_only ? get_custom_buttons(object).count : get_custom_buttons(object).sort_by { |a| a.name.downcase }
end

def button_order?(object)
object[:set_data] && object[:set_data][:button_order]
end

def x_get_tree_aset_kids(object, count_only)
if object.id.nil?
get_tree_aset_kids_for_nil_id(object, count_only)
elsif count_only
object.members.count
else
# need to show button nodes in button order that they were saved in
button_order = button_order?(object) ? object[:set_data][:button_order] : nil
objects = []
Array(button_order).each do |bidx|
object.members.each { |b| objects.push(b) if bidx == b.id && !objects.include?(b) }
end
objects
end
end

def buttons_node_image(node)
case node
when 'ExtManagementSystem' then {:icon => 'pficon pficon-server'}
Expand Down
42 changes: 8 additions & 34 deletions app/presenters/tree_builder_catalogs_class.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class TreeBuilderCatalogsClass < TreeBuilder
include CustomButtonsMixin
has_kids_for CustomButtonSet, [:x_get_tree_aset_kids]

private
Expand All @@ -9,40 +10,13 @@ def x_get_tree_roots(count_only, options)
when :stcat
return count_only_or_objects(count_only, objects)
when :sandt
return count_only_or_objects(count_only,
objects.unshift(ServiceTemplateCatalog.new(:name => 'Unassigned',
:description => 'Unassigned Catalogs')),
nil)
end
end

# TODO: De-duplicate the following methods from tree_builder_buttons.rb
def get_custom_buttons(object)
# FIXME: don't we have a method for the splits?
# FIXME: cannot we ask for the null parent using Arel?
CustomButton.buttons_for(object.name.split('|').last.split('-').last).select do |uri|
uri.parent.nil?
end
end

def get_tree_aset_kids_for_nil_id(object, count_only)
count_only ? get_custom_buttons(object).count : get_custom_buttons(object).sort_by { |a| a.name.downcase }
end

def button_order?(object)
object[:set_data] && object[:set_data][:button_order]
end

def x_get_tree_aset_kids(object, count_only)
if object.id.nil?
get_tree_aset_kids_for_nil_id(object, count_only)
elsif count_only
object.members.count
else
button_order = button_order?(object) ? object[:set_data][:button_order] : nil
Array(button_order).each_with_object([]) do |bidx, arr|
object.members.each { |b| arr.push(b) if bidx == b.id && !arr.include?(b) }
end
return count_only_or_objects(
count_only,
objects.unshift(ServiceTemplateCatalog.new(
:name => 'Unassigned',
:description => 'Unassigned Catalogs')),
nil
)
end
end
end

0 comments on commit adefa56

Please sign in to comment.