Apply Rails/SafeNavigation rubocop rule

This commit is contained in:
Javi Martín
2019-06-22 23:00:33 +02:00
parent daa86ca3fc
commit 9fe8c47528
39 changed files with 52 additions and 53 deletions

View File

@@ -153,9 +153,6 @@ Rails/RedundantReceiverInWithOptions:
Rails/ReversibleMigration: Rails/ReversibleMigration:
Enabled: true Enabled: true
Rails/SafeNavigation:
Enabled: true
Rails/SaveBang: Rails/SaveBang:
Enabled: true Enabled: true

View File

@@ -141,6 +141,10 @@ Rails/RelativeDateConstant:
Rails/RequestReferer: Rails/RequestReferer:
Enabled: true Enabled: true
Rails/SafeNavigation:
Enabled: true
ConvertTry: true
Rails/TimeZone: Rails/TimeZone:
Enabled: true Enabled: true

View File

@@ -8,7 +8,7 @@ class Admin::BaseController < ApplicationController
private private
def verify_administrator def verify_administrator
raise CanCan::AccessDenied unless current_user.try(:administrator?) raise CanCan::AccessDenied unless current_user&.administrator?
end end
end end

View File

@@ -21,7 +21,7 @@ class Admin::Poll::QuestionsController < Admin::Poll::BaseController
end end
def create def create
@question.author = @question.proposal.try(:author) || current_user @question.author = @question.proposal&.author || current_user
@question.votation_type = VotationType.build_by_type(@question, params[:votation_type]) @question.votation_type = VotationType.build_by_type(@question, params[:votation_type])
if @question.save if @question.save

View File

@@ -110,9 +110,9 @@ class ApplicationController < ActionController::Base
end end
def set_default_budget_filter def set_default_budget_filter
if @budget.try(:balloting?) || @budget.try(:publishing_prices?) if @budget&.balloting? || @budget&.publishing_prices?
params[:filter] ||= "selected" params[:filter] ||= "selected"
elsif @budget.try(:finished?) elsif @budget&.finished?
params[:filter] ||= "winners" params[:filter] ||= "winners"
end end
end end

View File

@@ -141,7 +141,7 @@ module Budgets
def load_heading def load_heading
if params[:heading_id].present? if params[:heading_id].present?
@heading = @budget.headings.find_by_slug_or_id! params[:heading_id] @heading = @budget.headings.find_by_slug_or_id! params[:heading_id]
@assigned_heading = @ballot.try(:heading_for_group, @heading.try(:group)) @assigned_heading = @ballot&.heading_for_group(@heading&.group)
load_map load_map
end end
end end

View File

@@ -85,7 +85,7 @@ class CommentsController < ApplicationController
def add_notification(comment) def add_notification(comment)
notifiable = comment.reply? ? comment.parent : comment.commentable notifiable = comment.reply? ? comment.parent : comment.commentable
notifiable_author_id = notifiable.try(:author_id) notifiable_author_id = notifiable&.author_id
if notifiable_author_id.present? && notifiable_author_id != comment.author_id if notifiable_author_id.present? && notifiable_author_id != comment.author_id
Notification.add(notifiable.author, notifiable) Notification.add(notifiable.author, notifiable)
end end

View File

@@ -27,13 +27,13 @@ class Management::SessionsController < ActionController::Base
end end
def admin? def admin?
if current_user.try(:administrator?) if current_user&.administrator?
session[:manager] = { login: "admin_user_#{current_user.id}" } session[:manager] = { login: "admin_user_#{current_user.id}" }
end end
end end
def manager? def manager?
if current_user.try(:manager?) if current_user&.manager?
session[:manager] = { login: "manager_user_#{current_user.id}" } session[:manager] = { login: "manager_user_#{current_user.id}" }
end end
end end

View File

@@ -9,7 +9,7 @@ class Moderation::BaseController < ApplicationController
private private
def verify_moderator def verify_moderator
raise CanCan::AccessDenied unless current_user.try(:moderator?) || current_user.try(:administrator?) raise CanCan::AccessDenied unless current_user&.moderator? || current_user&.administrator?
end end
end end

View File

@@ -10,7 +10,7 @@ class Officing::BaseController < ApplicationController
private private
def verify_officer def verify_officer
raise CanCan::AccessDenied unless current_user.try(:poll_officer?) raise CanCan::AccessDenied unless current_user&.poll_officer?
end end
def check_officer_assignment def check_officer_assignment

View File

@@ -25,7 +25,7 @@ class PollsController < ApplicationController
.where.not(description: "").order(:given_order) .where.not(description: "").order(:given_order)
@answers_by_question_id = {} @answers_by_question_id = {}
poll_answers = ::Poll::Answer.by_question(@poll.question_ids).by_author(current_user.try(:id)) poll_answers = ::Poll::Answer.by_question(@poll.question_ids).by_author(current_user&.id)
@last_pair_question_answers = {} @last_pair_question_answers = {}
@questions.each do |question| @questions.each do |question|

View File

@@ -9,7 +9,7 @@ class Tracking::BaseController < ApplicationController
private private
def verify_tracker def verify_tracker
raise CanCan::AccessDenied unless current_user.try(:tracker?) || current_user.try(:administrator?) raise CanCan::AccessDenied unless current_user&.tracker? || current_user&.administrator?
end end
end end

View File

@@ -47,8 +47,7 @@ class Tracking::BudgetInvestmentsController < Tracking::BaseController
end end
def heading_filters def heading_filters
investments = @budget.investments.by_tracker(current_user.tracker.try(:id)) investments = @budget.investments.by_tracker(current_user.tracker&.id).distinct
.distinct
investment_headings = Budget::Heading.where(id: investments.pluck(:heading_id).uniq) investment_headings = Budget::Heading.where(id: investments.pluck(:heading_id).uniq)
.order(name: :asc) .order(name: :asc)
all_headings_filter = [ all_headings_filter = [

View File

@@ -9,7 +9,7 @@ class Valuation::BaseController < ApplicationController
private private
def verify_valuator def verify_valuator
raise CanCan::AccessDenied unless current_user.try(:valuator?) || current_user.try(:administrator?) raise CanCan::AccessDenied unless current_user&.valuator? || current_user&.administrator?
end end
end end

View File

@@ -73,8 +73,7 @@ class Valuation::BudgetInvestmentsController < Valuation::BaseController
end end
def heading_filters def heading_filters
investments = @budget.investments.by_valuator(current_user.valuator.try(:id)) investments = @budget.investments.by_valuator(current_user.valuator&.id).visible_to_valuators.distinct
.visible_to_valuators.distinct
investment_headings = Budget::Heading.where(id: investments.pluck(:heading_id)).sort_by(&:name) investment_headings = Budget::Heading.where(id: investments.pluck(:heading_id)).sort_by(&:name)
all_headings_filter = [ all_headings_filter = [

View File

@@ -8,7 +8,7 @@ module BudgetHeadingsHelper
def heading_link(assigned_heading = nil, budget = nil) def heading_link(assigned_heading = nil, budget = nil)
return nil unless assigned_heading && budget return nil unless assigned_heading && budget
heading_path = budget_investments_path(budget, heading_id: assigned_heading.try(:id)) heading_path = budget_investments_path(budget, heading_id: assigned_heading&.id)
link_to(assigned_heading.name, heading_path) link_to(assigned_heading.name, heading_path)
end end

View File

@@ -12,7 +12,7 @@ module SiteCustomizationHelper
I18nContentTranslation.where( I18nContentTranslation.where(
i18n_content_id: content.id, i18n_content_id: content.id,
locale: locale locale: locale
).first.try(:value) ).first&.value
else else
false false
end end

View File

@@ -72,7 +72,7 @@ module Abilities
can [:create, :destroy], Follow can [:create, :destroy], Follow
can [:destroy], Document do |document| can [:destroy], Document do |document|
document.documentable.try(:author_id) == user.id document.documentable&.author_id == user.id
end end
can [:destroy], Image, imageable: { author_id: user.id } can [:destroy], Image, imageable: { author_id: user.id }

View File

@@ -25,7 +25,7 @@ class AdminNotification < ApplicationRecord
end end
def list_of_recipients_count def list_of_recipients_count
list_of_recipients.try(:count) || 0 list_of_recipients&.count || 0
end end
def deliver def deliver

View File

@@ -79,7 +79,7 @@ class Budget < ApplicationRecord
if phases.exists? && phases.send(phase).description.present? if phases.exists? && phases.send(phase).description.present?
phases.send(phase).description phases.send(phase).description
else else
send("description_#{phase}").try(:html_safe) send("description_#{phase}")&.html_safe
end end
end end

View File

@@ -34,9 +34,9 @@ class Budget
private private
def set_denormalized_ids def set_denormalized_ids
self.heading_id ||= investment.try(:heading_id) self.heading_id ||= investment&.heading_id
self.group_id ||= investment.try(:group_id) self.group_id ||= investment&.group_id
self.budget_id ||= investment.try(:budget_id) self.budget_id ||= investment&.budget_id
end end
def store_user_heading def store_user_heading

View File

@@ -319,7 +319,7 @@ class Budget
end end
def set_responsible_name def set_responsible_name
self.responsible_name = author.try(:document_number) if author.try(:document_number).present? self.responsible_name = author&.document_number if author&.document_number.present?
end end
def should_show_aside? def should_show_aside?
@@ -400,8 +400,8 @@ class Budget
private private
def set_denormalized_ids def set_denormalized_ids
self.group_id = heading.try(:group_id) if heading_id_changed? self.group_id = heading&.group_id if heading_id_changed?
self.budget_id ||= heading.try(:group).try(:budget_id) self.budget_id ||= heading&.group&.budget_id
end end
def change_log def change_log

View File

@@ -10,7 +10,7 @@ module Globalizable
after_validation :copy_error_to_current_translation, on: :update after_validation :copy_error_to_current_translation, on: :update
def description def description
self.read_attribute(:description).try :html_safe self.read_attribute(:description)&.html_safe
end end
def locales_not_marked_for_destruction def locales_not_marked_for_destruction

View File

@@ -12,7 +12,7 @@ module Mappable
def map_must_be_valid def map_must_be_valid
return true if skip_map? return true if skip_map?
unless map_location.try(:available?) unless map_location&.available?
skip_map_error = I18n.t("activerecord.errors.models.map_location.attributes.map.invalid") skip_map_error = I18n.t("activerecord.errors.models.map_location.attributes.map.invalid")
errors.add(:skip_map, skip_map_error) errors.add(:skip_map, skip_map_error)
end end

View File

@@ -7,7 +7,7 @@ module Sanitizable
unless included_modules.include? Globalizable unless included_modules.include? Globalizable
def description def description
super.try :html_safe super&.html_safe
end end
end end
end end

View File

@@ -74,7 +74,7 @@ class Debate < ApplicationRecord
{ {
author.username => "B", author.username => "B",
tag_list.join(" ") => "B", tag_list.join(" ") => "B",
geozone.try(:name) => "B", geozone&.name => "B",
}.merge!(searchable_globalized_values) }.merge!(searchable_globalized_values)
end end

View File

@@ -23,7 +23,7 @@ class Flag < ApplicationRecord
def self.flagged?(user, flaggable) def self.flagged?(user, flaggable)
return false unless user return false unless user
!!by_user_and_flaggable(user, flaggable).try(:first) !!by_user_and_flaggable(user, flaggable)&.first
end end
end end

View File

@@ -59,7 +59,7 @@ class Legislation::Proposal < ApplicationRecord
{ title => "A", { title => "A",
author.username => "B", author.username => "B",
tag_list.join(" ") => "B", tag_list.join(" ") => "B",
geozone.try(:name) => "B", geozone&.name => "B",
summary => "C", summary => "C",
description => "D" } description => "D" }
end end

View File

@@ -45,7 +45,7 @@ class Poll::Question < ApplicationRecord
def searchable_values def searchable_values
{ title => "A", { title => "A",
proposal.try(:title) => "A", proposal&.title => "A",
author.username => "C", author.username => "C",
author_visible_name => "C" } author_visible_name => "C" }
end end

View File

@@ -20,7 +20,7 @@ class Poll::Question::Answer < ApplicationRecord
scope :visibles, -> { where(hidden: false) } scope :visibles, -> { where(hidden: false) }
def description def description
self[:description].try :html_safe self[:description]&.html_safe
end end
def self.order_answers(ordered_array) def self.order_answers(ordered_array)

View File

@@ -42,7 +42,7 @@ class Poll
private private
def set_denormalized_booth_assignment_id def set_denormalized_booth_assignment_id
self.booth_assignment_id ||= officer_assignment.try(:booth_assignment_id) self.booth_assignment_id ||= officer_assignment&.booth_assignment_id
end end
def in_census? def in_census?
@@ -56,7 +56,7 @@ class Poll
def fill_stats_fields def fill_stats_fields
if in_census? if in_census?
self.gender = census_api_response.gender self.gender = census_api_response.gender
self.geozone_id = Geozone.select(:id).where(census_code: census_api_response.district_code).first.try(:id) self.geozone_id = Geozone.select(:id).where(census_code: census_api_response.district_code).first&.id
self.age = voter_age(census_api_response.date_of_birth) self.age = voter_age(census_api_response.date_of_birth)
end end
end end

View File

@@ -135,7 +135,7 @@ class Proposal < ApplicationRecord
{ {
author.username => "B", author.username => "B",
tag_list.join(" ") => "B", tag_list.join(" ") => "B",
geozone.try(:name) => "B" geozone&.name => "B"
}.merge!(searchable_globalized_values) }.merge!(searchable_globalized_values)
end end

View File

@@ -25,7 +25,7 @@ class ProposalNotification < ApplicationRecord
after_create :set_author after_create :set_author
def minimum_interval def minimum_interval
return true if proposal.try(:notifications).blank? return true if proposal&.notifications.blank?
interval = Setting[:proposal_notification_minimum_interval_in_days] interval = Setting[:proposal_notification_minimum_interval_in_days]
minimum_interval = (Time.current - interval.to_i.days).to_datetime minimum_interval = (Time.current - interval.to_i.days).to_datetime
if proposal.notifications.last.created_at > minimum_interval if proposal.notifications.last.created_at > minimum_interval

View File

@@ -6,6 +6,6 @@ class SiteCustomization::ContentBlock < ApplicationRecord
def self.block_for(name, locale) def self.block_for(name, locale)
locale ||= I18n.default_locale locale ||= I18n.default_locale
find_by(name: name, locale: locale).try(:body) find_by(name: name, locale: locale)&.body
end end
end end

View File

@@ -29,11 +29,11 @@ class SiteCustomization::Image < ApplicationRecord
end end
def required_width def required_width
VALID_IMAGES[name].try(:first) VALID_IMAGES[name]&.first
end end
def required_height def required_height
VALID_IMAGES[name].try(:second) VALID_IMAGES[name]&.second
end end
private private

View File

@@ -18,7 +18,7 @@ class Valuator < ApplicationRecord
end end
def assigned_investment_ids def assigned_investment_ids
investment_ids + [valuator_group.try(:investment_ids)].flatten investment_ids + [valuator_group&.investment_ids].flatten
end end
end end

View File

@@ -8,7 +8,7 @@ class Verification::Email
def initialize(verified_user) def initialize(verified_user)
@verified_user = verified_user @verified_user = verified_user
@recipient = @verified_user.try(:email) @recipient = @verified_user&.email
end end
def save def save

View File

@@ -28,7 +28,7 @@ class Verification::Letter
def validate_correct_code def validate_correct_code
return if errors.include?(:verification_code) return if errors.include?(:verification_code)
if user.try(:letter_verification_code).to_i != verification_code.to_i if user&.letter_verification_code.to_i != verification_code.to_i
errors.add(:verification_code, I18n.t("verification.letter.errors.incorrect_code")) errors.add(:verification_code, I18n.t("verification.letter.errors.incorrect_code"))
end end
end end

View File

@@ -54,7 +54,7 @@ module Users
end end
def confirm_email def confirm_email
body = ActionMailer::Base.deliveries.last.try(:body) body = ActionMailer::Base.deliveries.last&.body
expect(body).to be_present expect(body).to be_present
sent_token = /.*confirmation_token=(.*)".*/.match(body.to_s)[1] sent_token = /.*confirmation_token=(.*)".*/.match(body.to_s)[1]