Skip to content

Commit

Permalink
Merge pull request ManageIQ#22 from MilanPospisil/cat_tree_refactoring
Browse files Browse the repository at this point in the history
Cat tree refactoring
  • Loading branch information
himdel authored Jan 9, 2017
2 parents 01d96f5 + 4811281 commit 07ae769
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 51 deletions.
4 changes: 1 addition & 3 deletions app/assets/javascripts/miq_tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,7 @@ function miqOnCheckSections(_tree_name, key, checked, all_checked) {

// OnClick handler for catgories Tree
function miqOnClickTagCat(id) {
if (id.split('__')[0] == 't') {
miqJqueryRequest(ManageIQ.tree.clickUrl + '?id=' + id, {beforeSend: true, complete: true});
}
miqJqueryRequest(ManageIQ.tree.clickUrl + '?id=' + id, {beforeSend: true, complete: true});
}

// OnClick handler for Genealogy Tree
Expand Down
46 changes: 5 additions & 41 deletions app/controllers/miq_policy_controller/miq_actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@ def action_field_changed
def action_tag_pressed
@edit = session[:edit]
@action = @edit[:action_id] ? MiqAction.find_by_id(@edit[:action_id]) : MiqAction.new
tag_name = params[:id].split('__')[1]
_, id = parse_nodetype_and_id(params[:id])
tag_name = Classification.find(from_cid(id)).tag.name
@tag_selected = Classification.tag2human(tag_name)
@edit[:new][:options][:tags] = {} unless tag_name.nil?
@edit[:new][:options][:tags] = [tag_name] unless tag_name.nil?

send_button_changes
end

Expand All @@ -157,10 +157,7 @@ def process_actions(actions, task)
end

def get_tags_tree
cats = Classification.categories.select(&:show).sort_by(&:name)
unless cats.nil?
action_build_cat_tree(cats)
end
action_build_cat_tree
end

def action_build_snmp_variables
Expand Down Expand Up @@ -295,41 +292,8 @@ def action_build_alert_choices
@edit[:new][:alerts] = {} # Clear out the alerts hash
end

# Build the categories/tags tree node
def action_build_cat_tree(cats)
r_node = TreeNodeBuilder.generic_tree_node(
"r_#{current_tenant.name}",
"#{current_tenant.name} Tags",
"100/tag.png",
"#{current_tenant.name} Tags",
:cfme_no_click => true,
:expand => true
)
if cats.any?
r_kids = []
cats.sort_by { |c| c.description.downcase }.each do |c|
next if c.read_only

c_node = TreeNodeBuilder.generic_tree_node("c_#{c.id}", c.description, "100/tag.png", c.description, :cfme_no_click => true)
if c.entries.any?
c_kids ||= []
c.entries.sort_by { |t| t.description.downcase }.each do |t|
t_node = TreeNodeBuilder.generic_tree_node(
"t__#{t.tag.name}",
t.description,
"100/blank.gif",
t.description
)
c_kids.push(t_node)
end
c_node[:children] = c_kids
end
# don't add categories that do not have any entries
r_kids.push(c_node) unless c_node[:children].nil?
end
r_node[:children] = r_kids
end
@cat_tree = TreeBuilder.convert_bs_tree(r_node).to_json
def action_build_cat_tree
@category_tree = TreeBuilderMiqActionCategory.new(:action_tags_tree, :action_tags, @sb, true, "#{current_tenant.name} Tags")
end

# Set action record variables to new values
Expand Down
49 changes: 49 additions & 0 deletions app/presenters/tree_builder_miq_action_category.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class TreeBuilderMiqActionCategory < TreeBuilder
has_kids_for Classification, [:x_get_tree_classification_kids]

private

def override(node, object, _pid, _options)
leaf = !object.category?
image = leaf ? nil : "100/tag.png"
node[:cfmeNoClick] = !leaf
node[:image] = image && ActionController::Base.helpers.image_path(image)
node
end

def initialize(name, type, sandbox, build = true, tenant_name = nil)
@tenant_name = tenant_name
super(name, type, sandbox, build)
end

def tree_init_options(_tree_name)
{
:expand => true,
:lazy => false
}
end

def set_locals_for_render
locals = super
locals.merge!(
:id_prefix => "cat_tree",
:click_url => "/miq_policy/action_tag_pressed/",
:onclick => "miqOnClickTagCat"
)
end

def root_options
[title = @tenant_name, title, "100/tag.png"]
end

# Get root nodes count/array for explorer tree
def x_get_tree_roots(count_only, _options)
cats = Classification.categories.select(&:show)
cats = cats.select { |c| c.entries.any? && c.read_only == false }
count_only_or_objects(count_only, cats, :description)
end

def x_get_tree_classification_kids(c, count_only)
count_only_or_objects(count_only, c.entries, :description)
end
end
8 changes: 1 addition & 7 deletions app/views/miq_policy/_action_options.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,7 @@
.form-horizontal
.form-group
.col-md-8
#action_tags_treebox.treeview-pf-hover.treeview-pf-select
= render(:partial => "layouts/tree",
:locals => {:tree_id => "action_tags_treebox",
:tree_name => "action_tags_tree",
:bs_tree => @cat_tree,
:click_url => "/miq_policy/action_tag_pressed/",
:onclick => "miqOnClickTagCat"})
= render(:partial => 'shared/tree', :locals => {:tree => @category_tree, :name => @category_tree.name})
- when "create_snapshot"
%h3
= _("Snapshot Settings")
Expand Down
79 changes: 79 additions & 0 deletions spec/presenters/tree_builder_miq_action_category_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
describe TreeBuilderMiqActionCategory do
before do
role = MiqUserRole.find_by_name("EvmRole-operator")
group = FactoryGirl.create(:miq_group, :miq_user_role => role, :description => "Tags Group")
login_as FactoryGirl.create(:user, :userid => 'tags_wilma', :miq_groups => [group])
end

let!(:tag1) { FactoryGirl.create(:classification, :name => 'tag1', :show => false) }
let!(:folder1) do
f1 = FactoryGirl.create(:classification, :name => 'folder1', :show => true)
f1.entries.push(tag1)
f1
end
let!(:tag2) { FactoryGirl.create(:classification, :name => 'tag2', :show => false) }
let!(:folder2) do
f2 = FactoryGirl.create(:classification, :name => 'folder2', :show => true)
f2.entries.push(tag2)
f2
end
let!(:tenant) { "TestTenant Tags" }
let!(:tree_name) { :action_tags }

subject do
described_class.new(:action_tags_tree, :action_tags, {}, true, tenant)
end

describe '#tree_init_options' do
it 'set init options correctly' do
expect(subject.send(:tree_init_options, tree_name)).to eq(:expand => true, :lazy => false)
end
end

describe '#set_locals_for_render' do
it 'set locals for render correctly' do
locals = subject.send(:set_locals_for_render)
expect(locals[:id_prefix]).to eq('cat_tree')
expect(locals[:click_url]).to eq("/miq_policy/action_tag_pressed/")
expect(locals[:onclick]).to eq("miqOnClickTagCat")
end
end

describe '#override' do
it 'set node' do
node = subject.send(:override, {}, tag1, nil, nil)
expect(node[:cfmeNoClick]).to eq(false)

node = subject.send(:override, {}, folder1, nil, nil)
expect(node[:cfmeNoClick]).to eq(true)
end
end

describe '#root_options' do
it 'sets root_options correctly' do
expect(subject.send(:root_options)).to eq([tenant, tenant, "100/tag.png"])
end
end

describe '#x_get_tree_roots' do
it 'sets first level nodes correctly' do
s = subject.send(:x_get_tree_roots, false, nil)
expect(s).to eq([folder1, folder2].sort_by { |c| c.description.downcase })
end
end

describe '#x_get_tree_classification_kids' do
it 'sets second level nodes correctly' do
kid1 = subject.send(:x_get_tree_classification_kids, folder1, false)
kid2 = subject.send(:x_get_tree_classification_kids, folder2, false)

expect(kid1[0].id).to eq(tag1.id)
expect(kid1[0].description).to eq(tag1.description)
expect(kid1[0].parent_id).to eq(folder1.id)

expect(kid2[0].id).to eq(tag2.id)
expect(kid2[0].description).to eq(tag2.description)
expect(kid2[0].parent_id).to eq(folder2.id)
end
end
end

0 comments on commit 07ae769

Please sign in to comment.