Skip to content

Commit

Permalink
Merge pull request #2842 from martinpovolny/search_fixes
Browse files Browse the repository at this point in the history
Search fixes for VmInfra
  • Loading branch information
himdel committed Dec 4, 2017
2 parents 414def5 + d9835ed commit e4e87c6
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 16 deletions.
35 changes: 23 additions & 12 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ def process_params_options(params)
options = from_additional_options(params[:additional_options] || {})
if params[:explorer]
params[:action] = "explorer"
@explorer = params[:explorer] == "true"
@explorer = params[:explorer].to_s == "true"
end

# if params[:active_tree] && defined? get_node_info
Expand Down Expand Up @@ -1456,11 +1456,21 @@ def get_view_process_search_text(view)
@search_text = params[:search_text].blank? ? nil : params[:search_text].strip
end

return nil unless @search_text

# Don't apply sub_filter when viewing sub-list view of a CI.
# This applies when search is active and you go Vm -->
# {Processes,Users,...} in that case, search shoult NOT be applied.
# FIXME: This needs to be changed to apply search in some explicit way.
return nil if @display

# If we came in through Chart pop-up menu click we don't filter records.
return nil if session[:menu_click]

# Build sub_filter where clause from search text
if @search_text && (
(!@parent && @lastaction == "show_list" && !session[:menu_click]) ||
(@explorer && !session[:menu_click]) ||
%w(miq_policy vm_infra).include?(@layout)) # Added to handle search text from list views in control explorer
if (!@parent && @lastaction == "show_list") || # This part is for the Hosts screen.
@explorer # In explorer screens we have search (that includes vm_infra and
# Control/Explorer/Policies)

stxt = @search_text.gsub("_", "`_") # Escape underscores
stxt.gsub!("%", "`%") # and percents
Expand All @@ -1475,14 +1485,15 @@ def get_view_process_search_text(view)
"%#{stxt}%"
end

if ::Settings.server.case_sensitive_name_search
sub_filter = ["#{view.db_class.table_name}.#{view.col_order.first} like ? escape '`'", stxt]
else
# don't apply sub_filter when viewing sub-list view of a CI
sub_filter = ["lower(#{view.db_class.table_name}.#{view.col_order.first}) like ? escape '`'", stxt.downcase] unless @display
end
return (
if ::Settings.server.case_sensitive_name_search
["#{view.db_class.table_name}.#{view.col_order.first} like ? escape '`'", stxt]
else
["lower(#{view.db_class.table_name}.#{view.col_order.first}) like ? escape '`'", stxt.downcase]
end
)
end
sub_filter
nil
end

def perpage_key(dbname)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class ApplicationController
:embedded,
:showlinks,
:policy_sim,
:lastaction
:lastaction,
:display
) do
def self.from_options(options)
additional_options = new
Expand All @@ -40,6 +41,7 @@ def with_quadicon_options(options)
self.showlinks = options[:showlinks]
self.policy_sim = options[:policy_sim]
self.lastaction = options[:lastaction]
self.display = options[:display]
end

def with_row_button(row_button)
Expand Down
7 changes: 6 additions & 1 deletion app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1634,7 +1634,8 @@ def process_show_list_options(options, curr_model = nil)
:embedded => @embedded,
:showlinks => @showlinks,
:policy_sim => @policy_sim,
:lastaction => @lastaction
:lastaction => @lastaction,
:display => @display
)
@report_data_additional_options.with_row_button(@row_button) if @row_button
@report_data_additional_options.with_menu_click(params[:menu_click]) if params[:menu_click]
Expand Down Expand Up @@ -1670,6 +1671,10 @@ def restore_quadicon_options(quadicon_options)
# @view.db
# @parent
@lastaction = quadicon_options[:lastaction]

# we also need to pass the @display because @display passed throught the
# session does not persist the null value
@display = quadicon_options[:display]
end

# Wrapper around jquery-rjs' remote_function which adds an extra .html_safe()
Expand Down
47 changes: 45 additions & 2 deletions spec/controllers/host_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
describe HostController do
let(:h1) { FactoryGirl.create(:host, :name => 'foobar') }
let(:h2) { FactoryGirl.create(:host, :name => 'bar') }

context "#button" do
render_views

Expand All @@ -10,8 +13,6 @@
end

it 'edit renders GTL grid with selected Host records' do
h1 = FactoryGirl.create(:host)
h2 = FactoryGirl.create(:host)
session[:host_items] = [h1.id, h2.id]
session[:settings] = {:views => {:host => 'grid'},
:display => {:quad_truncate => 'f'},
Expand Down Expand Up @@ -353,6 +354,48 @@
expect(response.status).to eq(200)
end
end

context 'called with search text' do
it 'render GTL with and saves search_text in the session' do
expect_any_instance_of(GtlHelper).to receive(:render_gtl).with match_gtl_options(
:model_name => 'Host',
:parent_id => nil,
:report_data_additional_options => {
:lastaction => 'show_list',
},
)
get :show_list, :params => {'search[text]' => 'foobar'}
expect(session.fetch_path(:sandboxes, 'host', :search_text)).to eq('foobar')
expect(response.status).to eq(200)
end
end
end

describe '#report_data' do
before(:each) do
stub_user(:features => :all)
EvmSpecHelper.create_guid_miq_server_zone
end

context 'called with search text' do
it 'returns hosts filtered by the search text' do
h1
h2

session[:sandboxes] = {}
session.store_path(:sandboxes, 'host', :search_text, 'foobar')
report_data_request(
:model => 'Host',
:parent_id => nil,
:report_data_additional_options => {
:lastaction => 'show_list',
}
)
results = assert_report_data_response
expect(results['data']['rows'].length).to eq(1)
expect(results['data']['rows'][0]['long_id']).to eq(h1.id.to_s)
end
end
end

it_behaves_like "controller with custom buttons"
Expand Down
38 changes: 38 additions & 0 deletions spec/controllers/vm_infra_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,44 @@
expect(assigns(:flash_array).first[:message]).to include("Create Snapshot")
end

context 'simple searching' do
let(:vm1) { FactoryGirl.create(:vm_vmware, :name => 'foobar') }
let(:vm2) { FactoryGirl.create(:vm_vmware, :name => 'barbar') }

describe '#x_search_by_name' do
it 'render GTL with and saves search_text in the session' do
seed_session_trees('vm_infra', 'vandt_tree', 'root')
expect_any_instance_of(GtlHelper).to receive(:render_gtl).with match_gtl_options(
:model_name => 'VmOrTemplate',
:parent_id => nil,
:explorer => true,
)

post :x_search_by_name, :params => {:search_text => 'foobar'}
expect(session.fetch_path(:sandboxes, 'vm_infra', :search_text)).to eq('foobar')
expect(response.status).to eq(200)
end
end

describe '#report_data' do
it 'returns VMs filtered by the search text' do
vm1
vm2

session[:sandboxes] = {}
session.store_path(:sandboxes, 'vm_infra', :search_text, 'foobar')
report_data_request(
:model => 'VmOrTemplate',
:parent_id => nil,
:explorer => true,
)
results = assert_report_data_response
expect(results['data']['rows'].length).to eq(1)
expect(results['data']['rows'][0]['long_id']).to eq(vm1.id.to_s)
end
end
end

include_examples '#download_summary_pdf', :vm_vmware
include_examples '#download_summary_pdf', :template_vmware

Expand Down

0 comments on commit e4e87c6

Please sign in to comment.