Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open the homonymic tree nodes for dialog editor entrypoint selection #5543

Merged
merged 4 commits into from
May 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ ManageIQ.angular.app.service('DialogEditorHttp', ['$http', 'API', function($http
});
};

this.treeSelectorLoadData = function(fqdn) {
var url = '/tree/automate_entrypoint' + (fqdn ? '?fqdn=' + encodeURIComponent(fqdn) : '');
this.treeSelectorLoadData = function(fqname) {
var url = '/tree/automate_entrypoint' + (fqname ? '?fqname=' + encodeURIComponent(fqname) : '');
return $http.get(url).then(function(response) {
return response.data;
});
Expand Down
37 changes: 35 additions & 2 deletions app/controllers/tree_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,30 @@ class TreeController < ApplicationController
before_action :check_privileges

def automate_entrypoint
json = fetch_tree(TreeBuilderAutomateEntrypoint, :automate_entrypoint_tree, params[:id])
json = fetch_tree(TreeBuilderAutomateEntrypoint, :automate_entrypoint_tree, params[:id]) do |tree|
if params[:fqname].present?
MiqAeInstance.get_homonymic_across_domains_noprefix(current_user, params[:fqname], true).each do |instance|
# Parse the namespace, class and instance from the fqname
namespace, klass, instance, _ = MiqAeEngine::MiqAePath.split(instance.fqname)

begin
# Collect all the db records based on the parsed fqname
open_nodes = namespace.split('/').each_with_object([]) do |ns, items|
items << MiqAeNamespace.find_by!(:name => ns, :parent => items.last)
end
open_nodes << MiqAeClass.find_by!(:name => klass, :namespace_id => open_nodes.last.id)
open_nodes << MiqAeInstance.find_by!(:name => instance, :class_id => open_nodes.last.id)
rescue ActiveRecord::RecordNotFound
# Skip the iteration steop if one of the records is not found
# FIXME: we probably need foreign keys instead of this magic
next
else
# Set the related tree nodes to open if all the records have been found
open_nodes.each { |node| tree.open_node(TreeNode.new(node, {}, {}).key) }
end
end
end
end
render :body => json, :content_type => 'application/json'
end

Expand All @@ -16,8 +39,18 @@ def automate_inline_methods

private

# This method returns with a JSON that can be directly consumed by a frontend
# treeview component. If a node_id is set, it only returns the subtree under
# the node represented by this id. There's also an option to pass a block to
# the method and customize the tree before building it. This way we can easily
# define custom behavior without creating an if/else spaghetti.
def fetch_tree(klass, name, node_id = nil)
tree = klass.new(name, {})
tree = klass.new(name, {}, false)

# FIXME: maybe here we would need instance_exec/eval instead
yield(tree) if block_given?

tree.reload!

if node_id
TreeBuilder.convert_bs_tree(tree.x_get_child_nodes(node_id)).to_json
Expand Down
10 changes: 5 additions & 5 deletions app/presenters/tree_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ def reload!
build_tree
end

def open_node(id)
open_nodes = @tree_state.x_tree(@name)[:open_nodes]
open_nodes.push(id) unless open_nodes.include?(id)
end

# FIXME: temporary conversion, needs to be moved into the generation
def self.convert_bs_tree(nodes)
return [] if nodes.nil?
Expand Down Expand Up @@ -334,11 +339,6 @@ def count_only_or_objects_filtered(count_only, objects, sort_by = nil, options =
count_only_or_objects(count_only, Rbac.filtered(objects, options), sort_by, &block)
end

def open_node(id)
open_nodes = @tree_state.x_tree(@name)[:open_nodes]
open_nodes.push(id) unless open_nodes.include?(id)
end

def prefixed_title(prefix, title)
ViewHelper.capture do
ViewHelper.concat_tag(:strong, "#{prefix}:")
Expand Down
3 changes: 2 additions & 1 deletion spec/controllers/tree_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
subject { controller.send(:fetch_tree, klass, :foo_tree, node_id) }

it 'returns with a tree hash' do
expect(klass).to receive(:new).with(:foo_tree, {}).and_return(tree)
expect(klass).to receive(:new).with(:foo_tree, {}, false).and_return(tree)
expect(tree).to receive(:reload!)
expect(subject).to eq(:foo => :bar)
end
end
Expand Down