diff --git a/.rubocop.yml b/.rubocop.yml index a23458f4d..6a374e962 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -153,9 +153,6 @@ Rails/RedundantReceiverInWithOptions: Rails/ReversibleMigration: Enabled: true -Rails/SafeNavigation: - Enabled: true - Rails/SaveBang: Enabled: true diff --git a/.rubocop_basic.yml b/.rubocop_basic.yml index db624cd91..2af5aff4a 100644 --- a/.rubocop_basic.yml +++ b/.rubocop_basic.yml @@ -141,6 +141,10 @@ Rails/RelativeDateConstant: Rails/RequestReferer: Enabled: true +Rails/SafeNavigation: + Enabled: true + ConvertTry: true + Rails/TimeZone: Enabled: true diff --git a/app/controllers/admin/base_controller.rb b/app/controllers/admin/base_controller.rb index a12540871..569dfbe0c 100644 --- a/app/controllers/admin/base_controller.rb +++ b/app/controllers/admin/base_controller.rb @@ -8,7 +8,7 @@ class Admin::BaseController < ApplicationController private def verify_administrator - raise CanCan::AccessDenied unless current_user.try(:administrator?) + raise CanCan::AccessDenied unless current_user&.administrator? end end diff --git a/app/controllers/admin/poll/questions_controller.rb b/app/controllers/admin/poll/questions_controller.rb index 279f9de6f..9b07b56b4 100644 --- a/app/controllers/admin/poll/questions_controller.rb +++ b/app/controllers/admin/poll/questions_controller.rb @@ -21,7 +21,7 @@ class Admin::Poll::QuestionsController < Admin::Poll::BaseController end 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]) if @question.save diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 5451451ff..56d9dc046 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -110,9 +110,9 @@ class ApplicationController < ActionController::Base end def set_default_budget_filter - if @budget.try(:balloting?) || @budget.try(:publishing_prices?) + if @budget&.balloting? || @budget&.publishing_prices? params[:filter] ||= "selected" - elsif @budget.try(:finished?) + elsif @budget&.finished? params[:filter] ||= "winners" end end diff --git a/app/controllers/budgets/investments_controller.rb b/app/controllers/budgets/investments_controller.rb index 74414293f..6b1c07832 100644 --- a/app/controllers/budgets/investments_controller.rb +++ b/app/controllers/budgets/investments_controller.rb @@ -141,7 +141,7 @@ module Budgets def load_heading if params[:heading_id].present? @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 end end diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 18ee03e09..6504c3f27 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -85,7 +85,7 @@ class CommentsController < ApplicationController def add_notification(comment) 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 Notification.add(notifiable.author, notifiable) end diff --git a/app/controllers/management/sessions_controller.rb b/app/controllers/management/sessions_controller.rb index 49784c53e..5617c22fb 100644 --- a/app/controllers/management/sessions_controller.rb +++ b/app/controllers/management/sessions_controller.rb @@ -27,13 +27,13 @@ class Management::SessionsController < ActionController::Base end def admin? - if current_user.try(:administrator?) + if current_user&.administrator? session[:manager] = { login: "admin_user_#{current_user.id}" } end end def manager? - if current_user.try(:manager?) + if current_user&.manager? session[:manager] = { login: "manager_user_#{current_user.id}" } end end diff --git a/app/controllers/moderation/base_controller.rb b/app/controllers/moderation/base_controller.rb index c175cb1bd..39d5b19ab 100644 --- a/app/controllers/moderation/base_controller.rb +++ b/app/controllers/moderation/base_controller.rb @@ -9,7 +9,7 @@ class Moderation::BaseController < ApplicationController private 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 diff --git a/app/controllers/officing/base_controller.rb b/app/controllers/officing/base_controller.rb index 42b85860b..c1b80539d 100644 --- a/app/controllers/officing/base_controller.rb +++ b/app/controllers/officing/base_controller.rb @@ -10,7 +10,7 @@ class Officing::BaseController < ApplicationController private def verify_officer - raise CanCan::AccessDenied unless current_user.try(:poll_officer?) + raise CanCan::AccessDenied unless current_user&.poll_officer? end def check_officer_assignment diff --git a/app/controllers/polls_controller.rb b/app/controllers/polls_controller.rb index b95303384..ba2e848dd 100644 --- a/app/controllers/polls_controller.rb +++ b/app/controllers/polls_controller.rb @@ -25,7 +25,7 @@ class PollsController < ApplicationController .where.not(description: "").order(:given_order) @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 = {} @questions.each do |question| diff --git a/app/controllers/tracking/base_controller.rb b/app/controllers/tracking/base_controller.rb index f924bc088..f42ac9f5c 100644 --- a/app/controllers/tracking/base_controller.rb +++ b/app/controllers/tracking/base_controller.rb @@ -9,7 +9,7 @@ class Tracking::BaseController < ApplicationController private 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 diff --git a/app/controllers/tracking/budget_investments_controller.rb b/app/controllers/tracking/budget_investments_controller.rb index d588964ec..4586b7308 100644 --- a/app/controllers/tracking/budget_investments_controller.rb +++ b/app/controllers/tracking/budget_investments_controller.rb @@ -47,8 +47,7 @@ class Tracking::BudgetInvestmentsController < Tracking::BaseController end def heading_filters - investments = @budget.investments.by_tracker(current_user.tracker.try(:id)) - .distinct + investments = @budget.investments.by_tracker(current_user.tracker&.id).distinct investment_headings = Budget::Heading.where(id: investments.pluck(:heading_id).uniq) .order(name: :asc) all_headings_filter = [ diff --git a/app/controllers/valuation/base_controller.rb b/app/controllers/valuation/base_controller.rb index 64001e6d7..bfb865cbe 100644 --- a/app/controllers/valuation/base_controller.rb +++ b/app/controllers/valuation/base_controller.rb @@ -9,7 +9,7 @@ class Valuation::BaseController < ApplicationController private 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 diff --git a/app/controllers/valuation/budget_investments_controller.rb b/app/controllers/valuation/budget_investments_controller.rb index 73324a0a4..24b61950e 100644 --- a/app/controllers/valuation/budget_investments_controller.rb +++ b/app/controllers/valuation/budget_investments_controller.rb @@ -73,8 +73,7 @@ class Valuation::BudgetInvestmentsController < Valuation::BaseController end def heading_filters - investments = @budget.investments.by_valuator(current_user.valuator.try(:id)) - .visible_to_valuators.distinct + investments = @budget.investments.by_valuator(current_user.valuator&.id).visible_to_valuators.distinct investment_headings = Budget::Heading.where(id: investments.pluck(:heading_id)).sort_by(&:name) all_headings_filter = [ diff --git a/app/helpers/budget_headings_helper.rb b/app/helpers/budget_headings_helper.rb index 80c21e9e8..fc014131b 100644 --- a/app/helpers/budget_headings_helper.rb +++ b/app/helpers/budget_headings_helper.rb @@ -8,7 +8,7 @@ module BudgetHeadingsHelper def heading_link(assigned_heading = nil, budget = nil) 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) end diff --git a/app/helpers/site_customization_helper.rb b/app/helpers/site_customization_helper.rb index 4a3e631b6..5046c071a 100644 --- a/app/helpers/site_customization_helper.rb +++ b/app/helpers/site_customization_helper.rb @@ -12,7 +12,7 @@ module SiteCustomizationHelper I18nContentTranslation.where( i18n_content_id: content.id, locale: locale - ).first.try(:value) + ).first&.value else false end diff --git a/app/models/abilities/common.rb b/app/models/abilities/common.rb index d26a38680..8087e4dba 100644 --- a/app/models/abilities/common.rb +++ b/app/models/abilities/common.rb @@ -72,7 +72,7 @@ module Abilities can [:create, :destroy], Follow can [:destroy], Document do |document| - document.documentable.try(:author_id) == user.id + document.documentable&.author_id == user.id end can [:destroy], Image, imageable: { author_id: user.id } diff --git a/app/models/admin_notification.rb b/app/models/admin_notification.rb index 3fdec9c16..7971bc9f3 100644 --- a/app/models/admin_notification.rb +++ b/app/models/admin_notification.rb @@ -25,7 +25,7 @@ class AdminNotification < ApplicationRecord end def list_of_recipients_count - list_of_recipients.try(:count) || 0 + list_of_recipients&.count || 0 end def deliver diff --git a/app/models/budget.rb b/app/models/budget.rb index 8b8b7c6f6..3ca9d2147 100644 --- a/app/models/budget.rb +++ b/app/models/budget.rb @@ -79,7 +79,7 @@ class Budget < ApplicationRecord if phases.exists? && phases.send(phase).description.present? phases.send(phase).description else - send("description_#{phase}").try(:html_safe) + send("description_#{phase}")&.html_safe end end diff --git a/app/models/budget/ballot/line.rb b/app/models/budget/ballot/line.rb index c64ed39c0..b52f9e562 100644 --- a/app/models/budget/ballot/line.rb +++ b/app/models/budget/ballot/line.rb @@ -34,9 +34,9 @@ class Budget private def set_denormalized_ids - self.heading_id ||= investment.try(:heading_id) - self.group_id ||= investment.try(:group_id) - self.budget_id ||= investment.try(:budget_id) + self.heading_id ||= investment&.heading_id + self.group_id ||= investment&.group_id + self.budget_id ||= investment&.budget_id end def store_user_heading diff --git a/app/models/budget/investment.rb b/app/models/budget/investment.rb index 935d33a17..c80b060fa 100644 --- a/app/models/budget/investment.rb +++ b/app/models/budget/investment.rb @@ -319,7 +319,7 @@ class Budget end 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 def should_show_aside? @@ -400,8 +400,8 @@ class Budget private def set_denormalized_ids - self.group_id = heading.try(:group_id) if heading_id_changed? - self.budget_id ||= heading.try(:group).try(:budget_id) + self.group_id = heading&.group_id if heading_id_changed? + self.budget_id ||= heading&.group&.budget_id end def change_log diff --git a/app/models/concerns/globalizable.rb b/app/models/concerns/globalizable.rb index 3b683c912..9051188b5 100644 --- a/app/models/concerns/globalizable.rb +++ b/app/models/concerns/globalizable.rb @@ -10,7 +10,7 @@ module Globalizable after_validation :copy_error_to_current_translation, on: :update def description - self.read_attribute(:description).try :html_safe + self.read_attribute(:description)&.html_safe end def locales_not_marked_for_destruction diff --git a/app/models/concerns/mappable.rb b/app/models/concerns/mappable.rb index fe2b847b9..4762da578 100644 --- a/app/models/concerns/mappable.rb +++ b/app/models/concerns/mappable.rb @@ -12,7 +12,7 @@ module Mappable def map_must_be_valid 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") errors.add(:skip_map, skip_map_error) end diff --git a/app/models/concerns/sanitizable.rb b/app/models/concerns/sanitizable.rb index 8602257ef..387d089ff 100644 --- a/app/models/concerns/sanitizable.rb +++ b/app/models/concerns/sanitizable.rb @@ -7,7 +7,7 @@ module Sanitizable unless included_modules.include? Globalizable def description - super.try :html_safe + super&.html_safe end end end diff --git a/app/models/debate.rb b/app/models/debate.rb index 375f79c5f..d5d2f4dc3 100644 --- a/app/models/debate.rb +++ b/app/models/debate.rb @@ -74,7 +74,7 @@ class Debate < ApplicationRecord { author.username => "B", tag_list.join(" ") => "B", - geozone.try(:name) => "B", + geozone&.name => "B", }.merge!(searchable_globalized_values) end diff --git a/app/models/flag.rb b/app/models/flag.rb index e69393d49..b01d7b9a9 100644 --- a/app/models/flag.rb +++ b/app/models/flag.rb @@ -23,7 +23,7 @@ class Flag < ApplicationRecord def self.flagged?(user, flaggable) return false unless user - !!by_user_and_flaggable(user, flaggable).try(:first) + !!by_user_and_flaggable(user, flaggable)&.first end end diff --git a/app/models/legislation/proposal.rb b/app/models/legislation/proposal.rb index 44becffc8..311c38de2 100644 --- a/app/models/legislation/proposal.rb +++ b/app/models/legislation/proposal.rb @@ -59,7 +59,7 @@ class Legislation::Proposal < ApplicationRecord { title => "A", author.username => "B", tag_list.join(" ") => "B", - geozone.try(:name) => "B", + geozone&.name => "B", summary => "C", description => "D" } end diff --git a/app/models/poll/question.rb b/app/models/poll/question.rb index 0016e0c02..bd8d8e79b 100644 --- a/app/models/poll/question.rb +++ b/app/models/poll/question.rb @@ -44,10 +44,10 @@ class Poll::Question < ApplicationRecord end def searchable_values - { title => "A", - proposal.try(:title) => "A", - author.username => "C", - author_visible_name => "C" } + { title => "A", + proposal&.title => "A", + author.username => "C", + author_visible_name => "C" } end def copy_attributes_from_proposal(proposal) diff --git a/app/models/poll/question/answer.rb b/app/models/poll/question/answer.rb index 48968e51b..5ac37c10a 100644 --- a/app/models/poll/question/answer.rb +++ b/app/models/poll/question/answer.rb @@ -20,7 +20,7 @@ class Poll::Question::Answer < ApplicationRecord scope :visibles, -> { where(hidden: false) } def description - self[:description].try :html_safe + self[:description]&.html_safe end def self.order_answers(ordered_array) diff --git a/app/models/poll/voter.rb b/app/models/poll/voter.rb index c899cabc9..d079ea991 100644 --- a/app/models/poll/voter.rb +++ b/app/models/poll/voter.rb @@ -42,7 +42,7 @@ class Poll private 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 def in_census? @@ -56,7 +56,7 @@ class Poll def fill_stats_fields if in_census? 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) end end diff --git a/app/models/proposal.rb b/app/models/proposal.rb index a4d9d10eb..9733a542f 100644 --- a/app/models/proposal.rb +++ b/app/models/proposal.rb @@ -135,7 +135,7 @@ class Proposal < ApplicationRecord { author.username => "B", tag_list.join(" ") => "B", - geozone.try(:name) => "B" + geozone&.name => "B" }.merge!(searchable_globalized_values) end diff --git a/app/models/proposal_notification.rb b/app/models/proposal_notification.rb index 3ea3ea906..e3ccb67a9 100644 --- a/app/models/proposal_notification.rb +++ b/app/models/proposal_notification.rb @@ -25,7 +25,7 @@ class ProposalNotification < ApplicationRecord after_create :set_author def minimum_interval - return true if proposal.try(:notifications).blank? + return true if proposal&.notifications.blank? interval = Setting[:proposal_notification_minimum_interval_in_days] minimum_interval = (Time.current - interval.to_i.days).to_datetime if proposal.notifications.last.created_at > minimum_interval diff --git a/app/models/site_customization/content_block.rb b/app/models/site_customization/content_block.rb index 2219ef5d4..a96bc8b47 100644 --- a/app/models/site_customization/content_block.rb +++ b/app/models/site_customization/content_block.rb @@ -6,6 +6,6 @@ class SiteCustomization::ContentBlock < ApplicationRecord def self.block_for(name, locale) locale ||= I18n.default_locale - find_by(name: name, locale: locale).try(:body) + find_by(name: name, locale: locale)&.body end end diff --git a/app/models/site_customization/image.rb b/app/models/site_customization/image.rb index c800e21b1..bcfbdd376 100644 --- a/app/models/site_customization/image.rb +++ b/app/models/site_customization/image.rb @@ -29,11 +29,11 @@ class SiteCustomization::Image < ApplicationRecord end def required_width - VALID_IMAGES[name].try(:first) + VALID_IMAGES[name]&.first end def required_height - VALID_IMAGES[name].try(:second) + VALID_IMAGES[name]&.second end private diff --git a/app/models/valuator.rb b/app/models/valuator.rb index 372dacc01..625395f0c 100644 --- a/app/models/valuator.rb +++ b/app/models/valuator.rb @@ -18,7 +18,7 @@ class Valuator < ApplicationRecord end def assigned_investment_ids - investment_ids + [valuator_group.try(:investment_ids)].flatten + investment_ids + [valuator_group&.investment_ids].flatten end end diff --git a/app/models/verification/email.rb b/app/models/verification/email.rb index 95faecad3..823f165cf 100644 --- a/app/models/verification/email.rb +++ b/app/models/verification/email.rb @@ -8,7 +8,7 @@ class Verification::Email def initialize(verified_user) @verified_user = verified_user - @recipient = @verified_user.try(:email) + @recipient = @verified_user&.email end def save diff --git a/app/models/verification/letter.rb b/app/models/verification/letter.rb index e70da2b5f..599a628f1 100644 --- a/app/models/verification/letter.rb +++ b/app/models/verification/letter.rb @@ -28,7 +28,7 @@ class Verification::Letter def validate_correct_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")) end end diff --git a/spec/support/common_actions/users.rb b/spec/support/common_actions/users.rb index c5271273f..27b2986f1 100644 --- a/spec/support/common_actions/users.rb +++ b/spec/support/common_actions/users.rb @@ -54,7 +54,7 @@ module Users end def confirm_email - body = ActionMailer::Base.deliveries.last.try(:body) + body = ActionMailer::Base.deliveries.last&.body expect(body).to be_present sent_token = /.*confirmation_token=(.*)".*/.match(body.to_s)[1]