Skip to content

Commit

Permalink
Merge pull request #1771 from MushroomObserver/nimmo-params-dig
Browse files Browse the repository at this point in the history
`param_lookup` to `params.dig`
  • Loading branch information
nimmolo committed Jan 6, 2024
2 parents e1972fd + 1b51491 commit 9c43556
Show file tree
Hide file tree
Showing 10 changed files with 17 additions and 31 deletions.
6 changes: 3 additions & 3 deletions app/controllers/account/login_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ def test_autologin; end
private

def normalize_login_params
@login = param_lookup([:user, :login]).to_s.strip
@password = param_lookup([:user, :password]).to_s.strip
@remember = param_lookup([:user, :remember_me]) == "1"
@login = params.dig(:user, :login).to_s.strip
@password = params.dig(:user, :password).to_s.strip
@remember = params.dig(:user, :remember_me) == "1"
end

def login_success(user)
Expand Down
14 changes: 0 additions & 14 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,20 +152,6 @@ def skip_bullet
# @view = view_context
# end

# Utility for extracting nested params where any level might be nil
def param_lookup(path, default = nil)
result = params
path.each do |arg|
result = result[arg]
break if result.nil?
end
if result.nil?
default
else
block_given? ? yield(result) : result
end
end

# Kick out agents responsible for excessive traffic.
def kick_out_excessive_traffic
return true if is_cool?
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/authors/email_requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ def create
private

def send_author_emails
subject = param_lookup([:email, :subject], "")
content = param_lookup([:email, :content], "")
subject = params.dig(:email, :subject).to_s
content = params.dig(:email, :content).to_s

(@object.authors + UserGroup.reviewers.users).uniq.each do |receiver|
QueuedEmail::AuthorRequest.create_email(@user, receiver, @object,
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/names/trackers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ def submit_tracking_form_udpate
end

def create_or_update_name_tracker_and_interest(name_id)
@note_template = param_lookup([:name_tracker, :note_template])
@note_template = params.dig(:name_tracker, :note_template)
note_template_enabled =
param_lookup([:name_tracker, :note_template_enabled]) == "1"
params.dig(:name_tracker, :note_template_enabled) == "1"
@note_template = nil if @note_template.blank? || !note_template_enabled
if @name_tracker.nil?
create_name_tracker_interest_and_flash(name_id)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/observations/images_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def init_project_vars_for_reload(obs_or_img)
end
@projects.each do |proj|
@project_checks[proj.id] =
param_lookup([:project, "id_#{proj.id}"]) == "1"
params.dig(:project, "id_#{proj.id}") == "1"
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/observations/namings/votes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def create_or_update_vote
pass_query_params
observation = load_observation_naming_includes # 1st load
@naming = observation.namings.find(params[:naming_id])
value_str = param_lookup([:vote, :value])
value_str = params.dig(:vote, :value).to_s
value = Vote.validate_value(value_str)
raise("Bad value.") unless value

Expand Down
4 changes: 2 additions & 2 deletions app/controllers/observations_controller/form_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def init_list_vars_for_reload(obs)
init_list_vars
@lists = @lists.union(obs.species_lists)
@lists.each do |list|
@list_checks[list.id] = param_lookup([:list, "id_#{list.id}"]) == "1"
@list_checks[list.id] = params.dig(:list, "id_#{list.id}") == "1"
end
end

Expand Down Expand Up @@ -200,7 +200,7 @@ def update_good_images(arg)
images.each do |image|
next unless check_permission(image)

args = param_lookup([:good_image, image.id.to_s])
args = params.dig(:good_image, image.id.to_s)
next unless args

image.attributes = args.permit(permitted_image_args)
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/observations_controller/new_and_create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ def create
if success
@observation.log(:log_observation_created)
# should always succeed
save_everything_else(param_lookup([:naming, :reasons]))
save_everything_else(params.dig(:naming, :reasons))
strip_images! if @observation.gps_hidden
flash_notice(:runtime_observation_success.t(id: @observation.id))
redirect_to_next_page

# If anything failed reload the form.
else
reload_new_form(param_lookup([:naming, :reasons]))
reload_new_form(params.dig(:naming, :reasons))
end
end

Expand Down
4 changes: 2 additions & 2 deletions app/controllers/observations_controller/validators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def validate_params(params)
end

def validate_name(params)
given_name = param_lookup([:naming, :name], "").to_s
chosen_name = param_lookup([:chosen_name, :name_id], "").to_s
given_name = params.dig(:naming, :name).to_s
chosen_name = params.dig(:chosen_name, :name_id).to_s
@resolver = Naming::NameResolver.new(
given_name, params[:approved_name], chosen_name
)
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/search_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class SearchController < ApplicationController
# /project/project_search
# /species_lists/index
def pattern
pattern = param_lookup([:search, :pattern]) { |p| p.to_s.strip_squeeze }
type = param_lookup([:search, :type], &:to_sym)
pattern = params.dig(:search, :pattern) { |p| p.to_s.strip_squeeze }
type = params.dig(:search, :type)&.to_sym

# Save it so that we can keep it in the search bar in subsequent pages.
session[:pattern] = pattern
Expand Down

0 comments on commit 9c43556

Please sign in to comment.