Skip to content

Commit

Permalink
Merge pull request #4658 from mzazrivec/fix_rubocop_warnings_in_appli…
Browse files Browse the repository at this point in the history
…cation_controller

ApplicationController: fix rubocop warnings
  • Loading branch information
himdel authored Sep 17, 2018
2 parents 248d491 + 63b6e72 commit 07dbfda
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 82 deletions.
145 changes: 65 additions & 80 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def pp_choices

before_action :reset_toolbar
before_action :set_session_tenant
before_action :get_global_session_data, :except => [:resize_layout, :authenticate]
before_action :get_global_session_data, :except => %i(resize_layout authenticate)
before_action :set_user_time_zone
before_action :set_gettext_locale
before_action :allow_websocket
Expand Down Expand Up @@ -257,7 +257,7 @@ def self.handle_exceptions=(v)
def error_handler(e)
raise e unless ApplicationController.handle_exceptions?

logger.fatal "Error caught: [#{e.class.name}] #{e.message}\n#{e.backtrace.join("\n")}"
logger.fatal("Error caught: [#{e.class.name}] #{e.message}\n#{e.backtrace.join("\n")}")

msg = case e
when ::ActionController::RoutingError
Expand Down Expand Up @@ -515,15 +515,16 @@ def show_statistics
end

# moved this method here so it can be accessed from pxe_server controller as well
def log_depot_validate # this is a terrible name, it doesn't validate log_depots
# this is a terrible name, it doesn't validate log_depots
def log_depot_validate
@schedule = nil # setting to nil, since we are using same view for both db_back and log_depot edit
# if zone is selected in tree replace tab#3
pfx = if x_active_tree == :diagnostics_tree
if @sb[:active_tab] == "diagnostics_database"
# coming from diagnostics/database tab
"dbbackup"
end
elsif session[:edit] && session[:edit].key?(:pxe_id)
elsif session[:edit]&.key?(:pxe_id)
# add/edit pxe server
"pxe"
else
Expand Down Expand Up @@ -568,7 +569,7 @@ def reload

def filesystem_download
fs = identify_record(params[:id], Filesystem)
send_data fs.contents, :filename => fs.name
send_data(fs.contents, :filename => fs.name)
end

def adv_search_text_clear
Expand All @@ -594,7 +595,7 @@ def move_cols_left_right(direction)
flds = direction == "right" ? "available_fields" : "selected_fields"
edit_fields = direction == "right" ? "available_fields" : "fields"
sort_fields = direction == "right" ? "fields" : "available_fields"
if !params[flds.to_sym] || params[flds.to_sym].empty? || params[flds.to_sym][0] == ""
if params[flds.to_sym].blank? || params[flds.to_sym][0] == ""
lr_messages = {
"left" => _("No fields were selected to move left"),
"right" => _("No fields were selected to move right")
Expand Down Expand Up @@ -648,18 +649,6 @@ def build_ae_tree(type = :ae, name = :ae_tree)
end
end

# moved this method here so it can be accessed from pxe_server controller as well
def log_depot_set_verify_status
if (@edit[:new][:log_password] == @edit[:new][:log_verify]) && @edit[:new][:uri_prefix] != "nfs" &&
(!@edit[:new][:uri].blank? && !@edit[:new][:log_userid].blank? && !@edit[:new][:log_password].blank? && !@edit[:new][:log_verify].blank?)
@edit[:log_verify_status] = true
elsif @edit[:new][:uri_prefix] == "nfs" && !@edit[:new][:uri].blank?
@edit[:log_verify_status] = true
else
@edit[:log_verify_status] = false
end
end

# Build an audit object when configuration is changed in configuration and ops controllers
def build_config_audit(new, current)
if controller_name == "ops" && @sb[:active_tab] == "settings_server"
Expand All @@ -676,7 +665,7 @@ def build_config_audit(new, current)
}
end

PASSWORD_FIELDS = [:password, :_pwd, :amazon_secret, :token].freeze
PASSWORD_FIELDS = %i(password _pwd amazon_secret token).freeze

def filter_config(data)
@parameter_filter ||=
Expand All @@ -703,20 +692,16 @@ def build_audit_msg(new, current, msg_in)
current[k] = {} if !current.nil? && current[k].nil?
# process keys of the current and new hashes
(new[k].keys | (current.nil? ? [] : current[k].keys)).each do |hk|
if current.nil? || (new[k][hk] != current[k][hk])
if password_field?(hk) # Asterisk out password fields
msg_arr << "#{hk}:[*]#{' to [*]' unless current.nil?}"
else
msg_arr << "#{hk}:[" +
(current.nil? ? "" : "#{filter_config(current[k][hk])}] to [") +
"#{filter_config(new[k][hk])}]"
end
end
next if current.present? && (new[k][hk] == current[k][hk])
msg_arr << if password_field?(hk) # Asterisk out password fields
"#{hk}:[*]#{' to [*]' unless current.nil?}"
else
"#{hk}:[" + (current.nil? ? "" : "#{filter_config(current[k][hk])}] to [") +
"#{filter_config(new[k][hk])}]"
end
end
else
msg_arr << "#{k}:[" +
(current.nil? ? "" : "#{filter_config(current[k])}] to [") +
"#{filter_config(new[k])}]"
msg_arr << "#{k}:[" + (current.nil? ? "" : "#{filter_config(current[k])}] to [") + "#{filter_config(new[k])}]"
end
end
end
Expand All @@ -739,7 +724,7 @@ def disable_client_cache

# Common method enable/disable schedules
def schedule_enable_disable(schedules, enabled)
schedules.select { |schedule| schedule.enabled != enabled }
schedules.reject { |schedule| schedule.enabled == enabled }
.sort_by { |e| e.name.downcase }.each do |schedule|
schedule.enabled = enabled
schedule.save!
Expand Down Expand Up @@ -811,7 +796,7 @@ def report_edit_aborted(lastaction)
}
javascript_redirect(javascript_process_redirect_args(js_args))
else
redirect_to :action => lastaction, :id => params[:id], :escape => false
redirect_to(:action => lastaction, :id => params[:id], :escape => false)
end
end

Expand Down Expand Up @@ -839,7 +824,7 @@ def selected_time_profile_for_pull_down
tp = TimeProfile.profile_for_user_tz(session[:userid], session[:user_tz])
tp = TimeProfile.default_time_profile if tp.nil?

if tp.nil? && !session[:time_profiles].blank?
if tp.nil? && session[:time_profiles].present?
first_id_in_hash = Array(session[:time_profiles].invert).min_by(&:first).last
tp = TimeProfile.find_by(:id => first_id_in_hash)
end
Expand Down Expand Up @@ -867,7 +852,7 @@ def set_user_time_zone

def populate_reports_menu(hide_custom = false)
# checking to see if group (used to be role) was selected in menu editor tree, or came in from reports/timeline tree calls
group = !session[:role_choice].blank? ? MiqGroup.find_by(:description => session[:role_choice]) : current_group
group = session[:role_choice].present? ? MiqGroup.find_by(:description => session[:role_choice]) : current_group
@sb[:rpt_menu] = get_reports_menu(hide_custom, group)
end

Expand Down Expand Up @@ -920,7 +905,7 @@ def view_to_hash_controller_from_job_target_class(target_class)
# Render the view data to a Hash structure for the list view
def view_to_hash(view, fetch_data = false)
# Get the time zone in effect for this view
tz = (view.db.downcase == 'miqschedule') ? server_timezone : Time.zone
tz = view.db.downcase == 'miqschedule' ? server_timezone : Time.zone

root = {:head => [], :rows => []}

Expand All @@ -938,7 +923,7 @@ def view_to_hash(view, fetch_data = false)
col = view.col_order[i]
next if view.column_is_hidden?(col)

align = [:fixnum, :integer, :Fixnum, :float].include?(column_type(view.db, view.col_order[i])) ? 'right' : 'left'
align = %i(fixnum integer Fixnum float).include?(column_type(view.db, view.col_order[i])) ? 'right' : 'left'

root[:head] << {:text => h,
:sort => 'str',
Expand Down Expand Up @@ -1101,12 +1086,13 @@ def flash_errors?
helper_method(:flash_errors?)

# Handle the breadcrumb array by either adding, or resetting to, the passed in breadcrumb
def drop_breadcrumb(new_bc, onlyreplace = false) # if replace = true, only add this bc if it was already there
# if replace = true, only add this bc if it was already there
def drop_breadcrumb(new_bc, onlyreplace = false)
# if the breadcrumb is in the array, remove it and all below by counting how many to pop
return if skip_breadcrumb?
remove = 0
@breadcrumbs.each do |bc|
if remove > 0 # already found a match,
if remove.positive? # already found a match,
remove += 1 # increment pop counter

# Check for a name match BEFORE the first left paren "(" or a url match BEFORE the last slash "/"
Expand All @@ -1117,16 +1103,16 @@ def drop_breadcrumb(new_bc, onlyreplace = false) # if replace = true, only add t
end
remove.times { @breadcrumbs.pop } # remove found element and any lower elements
if onlyreplace
@breadcrumbs.push(new_bc) if remove > 0 # only add it if something was removed
@breadcrumbs.push(new_bc) if remove.positive? # only add it if something was removed
else
@breadcrumbs.push(new_bc)
end
@breadcrumbs.push(new_bc) if onlyreplace && @breadcrumbs.empty?
if (@lastaction == "registry_items" || @lastaction == "filesystems" || @lastaction == "files") && new_bc[:name].length > 50
@title = new_bc [:name].slice(0..50) + "..." # Set the title to be the new breadcrumb
else
@title = new_bc [:name] # Set the title to be the new breadcrumb
end
@title = if (@lastaction == "registry_items" || @lastaction == "filesystems" || @lastaction == "files") && new_bc[:name].length > 50
new_bc [:name].slice(0..50) + "..." # Set the title to be the new breadcrumb
else
new_bc [:name] # Set the title to be the new breadcrumb
end

# add @search_text to title for gtl screens only
if @search_text.present? && @display.nil?
Expand Down Expand Up @@ -1192,7 +1178,7 @@ def check_generic_rbac
def handle_generic_rbac(pass)
unless pass
if request.xml_http_request?
javascript_redirect :controller => 'dashboard', :action => 'auth_error'
javascript_redirect(:controller => 'dashboard', :action => 'auth_error')
else
redirect_to(:controller => 'dashboard', :action => 'auth_error')
end
Expand Down Expand Up @@ -1221,11 +1207,11 @@ def cleanup_action
# get the sort column that was clicked on, else use the current one
def get_sort_col
unless params[:sortby].nil?
if @sortcol == params[:sortby].to_i # if same column was selected
@sortdir = flip_sort_direction(@sortdir)
else
@sortdir = "ASC"
end
@sortdir = if @sortcol == params[:sortby].to_i # if same column was selected
flip_sort_direction(@sortdir)
else
"ASC"
end
@sortcol = params[:sortby].to_i
end
# in case sort column is not set, set the defaults
Expand Down Expand Up @@ -1261,11 +1247,11 @@ def process_saved_reports(saved_reports, task)
end
end
end
if success_count > 0
if success_count.positive?
add_flash(n_("Successfully deleted Saved Report from the %{product} Database",
"Successfully deleted Saved Reports from the %{product} Database", success_count) % {:product => Vmdb::Appliance.PRODUCT_NAME})
end
if failure_count > 0
if failure_count.positive?
add_flash(n_("Error during Saved Report delete from the %{product} Database",
"Error during Saved Reports delete from the %{product} Database", failure_count) % {:product => Vmdb::Appliance.PRODUCT_NAME})
end
Expand Down Expand Up @@ -1330,14 +1316,12 @@ def get_view_process_search_text(view)
return nil if session[:menu_click]

# Build sub_filter where clause from search text
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
# This part is for the Hosts screen. In explorer screens we have search (that includes vm_infra and Control/Explorer/Policies)
if (!@parent && @lastaction == "show_list") || @explorer
stxt = @search_text.gsub("_", "`_") # Escape underscores
stxt.gsub!("%", "`%") # and percents

stxt = if stxt.starts_with?("*") && stxt.ends_with?("*") # Replace beginning/ending * chars with % for SQL
stxt = if stxt.starts_with?("*") && stxt.ends_with?("*") # Replace beginning/ending * chars with % for SQL
"%#{stxt[1..-2]}%"
elsif stxt.starts_with?("*")
"%#{stxt[1..-1]}"
Expand Down Expand Up @@ -1388,7 +1372,7 @@ def get_view(db, options = {}, fetch_data = false)
refresh_view = true
# Creating a new view, remember if came from a menu_click
session[:menu_click] = params[:menu_click] || options[:menu_click]
session[:bc] = params[:bc] # Remember incoming breadcrumb as well
session[:bc] = params[:bc] # Remember incoming breadcrumb as well
end

# Build the advanced search @edit hash
Expand Down Expand Up @@ -1444,7 +1428,7 @@ def get_view(db, options = {}, fetch_data = false)
view.ascending = @sortdir.to_s.downcase != "desc"

@items_per_page = controller_name.downcase == "miq_policy" ? ONE_MILLION : get_view_pages_perpage(dbname)
@items_per_page = ONE_MILLION if 'vm' == db_sym.to_s && controller_name == 'service'
@items_per_page = ONE_MILLION if db_sym.to_s == 'vm' && controller_name == 'service'

@current_page = options[:page] || (params[:page].to_i < 1 ? 1 : params[:page].to_i)

Expand Down Expand Up @@ -1567,29 +1551,29 @@ def render_or_redirect_partial(pfx)
if flash_errors?
javascript_flash
else
javascript_redirect :controller => @redirect_controller,
javascript_redirect(:controller => @redirect_controller,
:action => @refresh_partial,
:id => @redirect_id,
:prov_type => @prov_type,
:prov_id => @prov_id
:prov_id => @prov_id)
end
else
javascript_redirect(:controller => @redirect_controller, :action => @refresh_partial, :id => @redirect_id, :template_klass => @template_klass_type)
end
elsif params[:pressed] == "ems_cloud_edit" && params[:id]
javascript_redirect edit_ems_cloud_path(params[:id])
javascript_redirect(edit_ems_cloud_path(params[:id]))
elsif params[:pressed] == "ems_infra_edit" && params[:id]
javascript_redirect edit_ems_infra_path(params[:id])
javascript_redirect(edit_ems_infra_path(params[:id]))
elsif params[:pressed] == "ems_container_edit" && params[:id]
javascript_redirect edit_ems_container_path(params[:id])
javascript_redirect(edit_ems_container_path(params[:id]))
elsif params[:pressed] == "ems_middleware_edit" && params[:id]
javascript_redirect edit_ems_middleware_path(params[:id])
javascript_redirect(edit_ems_middleware_path(params[:id]))
elsif params[:pressed] == "ems_network_edit" && params[:id]
javascript_redirect edit_ems_network_path(params[:id])
javascript_redirect(edit_ems_network_path(params[:id]))
elsif params[:pressed] == "ems_physical_infra_edit" && params[:id]
javascript_redirect edit_ems_physical_infra_path(params[:id])
javascript_redirect(edit_ems_physical_infra_path(params[:id]))
else
javascript_redirect :action => @refresh_partial, :id => @redirect_id
javascript_redirect(:action => @refresh_partial, :id => @redirect_id)
end
end

Expand Down Expand Up @@ -1943,10 +1927,10 @@ def set_global_session_data
when "host"
session[:tab_bc][:inf] = @breadcrumbs.dup if %w(show show_list log_viewer).include?(action_name)
when "miq_request"
if @layout == "miq_request_vm"
session[:tab_bc][:vms] = @breadcrumbs.dup if %w(show show_list).include?(action_name)
else
session[:tab_bc][:inf] = @breadcrumbs.dup if %w(show show_list).include?(action_name)
if @layout == "miq_request_vm" && %w(show show_list).include?(action_name)
session[:tab_bc][:vms] = @breadcrumbs.dup
elsif %w(show show_list).include?(action_name)
session[:tab_bc][:inf] = @breadcrumbs.dup
end
when "vm"
session[:tab_bc][:vms] = @breadcrumbs.dup if %w(
Expand Down Expand Up @@ -2015,9 +1999,9 @@ def set_global_session_data
# Clearing out session objects that are no longer needed
session[:hac_tree] = session[:vat_tree] = nil if controller_name != "ops"
session[:ch_tree] = nil if !["compliance_history"].include?(params[:display]) && params[:action] != "squash_toggle"
session[:vm_tree] = nil if !["vmtree_info"].include?(params[:display])
session[:vm_tree] = nil unless ["vmtree_info"].include?(params[:display])
session[:policy_tree] = nil if params[:action] != "policies" && params[:pressed] != "vm_protect"
session[:resolve] = session[:resolve_object] = nil unless ["catalog", "miq_ae_customization", "miq_ae_tools"].include?(request.parameters[:controller])
session[:resolve] = session[:resolve_object] = nil unless %w(catalog miq_ae_customization miq_ae_tools).include?(request.parameters[:controller])
session[:report_menu] = session[:report_folders] = session[:menu_roles_tree] = nil if controller_name != "report"
if session.class != Hash
session_hash = session.respond_to?(:to_hash) ? session.to_hash : session.data
Expand Down Expand Up @@ -2085,8 +2069,9 @@ def identify_tl_or_perf_record
end

def assert_privileges(feature)
raise MiqException::RbacPrivilegeException,
_("The user is not authorized for this task or item.") unless role_allows?(:feature => feature)
unless role_allows?(:feature => feature)
raise MiqException::RbacPrivilegeException, _('The user is not authorized for this task or item.')
end
end

# Method tests, whether the user has rights to access records sent in request
Expand Down Expand Up @@ -2127,7 +2112,7 @@ def controller_for_common_methods

def reload_trees_by_presenter(presenter, trees)
trees.each do |tree|
next unless tree.present?
next if tree.blank?
presenter.reload_tree(tree.name, tree.locals_for_render[:bs_tree])
end
end
Expand Down
Loading

0 comments on commit 07dbfda

Please sign in to comment.