diff --git a/.rubocop.yml b/.rubocop.yml index edd745f80..df06cf101 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -27,9 +27,6 @@ Rails/EnvironmentComparison: Rails/Exit: Enabled: true -Rails/FindBy: - Enabled: true - Rails/FindEach: Enabled: true diff --git a/.rubocop_basic.yml b/.rubocop_basic.yml index 81e130604..94d7c4916 100644 --- a/.rubocop_basic.yml +++ b/.rubocop_basic.yml @@ -156,6 +156,13 @@ Rails/ApplicationRecord: Rails/Date: Enabled: true +Rails/DynamicFindBy: + Enabled: true + Severity: refactor + +Rails/FindBy: + Enabled: true + Rails/HttpPositionalArguments: Enabled: true diff --git a/app/models/budget/ballot.rb b/app/models/budget/ballot.rb index 9539119b0..f184e63a0 100644 --- a/app/models/budget/ballot.rb +++ b/app/models/budget/ballot.rb @@ -68,7 +68,7 @@ class Budget def heading_for_group(group) return nil unless has_lines_in_group?(group) - investments.where(group: group).first.heading + investments.find_by(group: group).heading end def casted_offline? diff --git a/app/models/budget/phase.rb b/app/models/budget/phase.rb index 859beb51f..c13a7825c 100644 --- a/app/models/budget/phase.rb +++ b/app/models/budget/phase.rb @@ -30,7 +30,7 @@ class Budget scope :published, -> { enabled.where.not(kind: "drafting") } PHASE_KINDS.each do |phase| - define_singleton_method(phase) { find_by_kind(phase) } + define_singleton_method(phase) { find_by(kind: phase) } end def next_enabled_phase diff --git a/app/models/concerns/relationable.rb b/app/models/concerns/relationable.rb index 4ce6bd3d3..85f396ebb 100644 --- a/app/models/concerns/relationable.rb +++ b/app/models/concerns/relationable.rb @@ -6,7 +6,7 @@ module Relationable end def find_related_content(relationable) - RelatedContent.where(parent_relationable: self, child_relationable: relationable).first + RelatedContent.find_by(parent_relationable: self, child_relationable: relationable) end def relationed_contents diff --git a/app/models/concerns/sluggable.rb b/app/models/concerns/sluggable.rb index bdcdff856..7f9bff56b 100644 --- a/app/models/concerns/sluggable.rb +++ b/app/models/concerns/sluggable.rb @@ -5,11 +5,11 @@ module Sluggable before_validation :generate_slug, if: :generate_slug? def self.find_by_slug_or_id(slug_or_id) - find_by_slug(slug_or_id) || find_by_id(slug_or_id) + find_by(slug: slug_or_id) || find_by(id: slug_or_id) end def self.find_by_slug_or_id!(slug_or_id) - find_by_slug(slug_or_id) || find(slug_or_id) + find_by(slug: slug_or_id) || find(slug_or_id) end end diff --git a/app/models/legislation/question.rb b/app/models/legislation/question.rb index 91b542d4b..d28029305 100644 --- a/app/models/legislation/question.rb +++ b/app/models/legislation/question.rb @@ -30,7 +30,7 @@ class Legislation::Question < ApplicationRecord end def answer_for_user(user) - answers.where(user: user).first + answers.find_by(user: user) end def comments_for_verified_residents_only? diff --git a/app/models/newsletter.rb b/app/models/newsletter.rb index e6fcf7720..021c8c7a8 100644 --- a/app/models/newsletter.rb +++ b/app/models/newsletter.rb @@ -62,7 +62,7 @@ class Newsletter < ApplicationRecord end def log_delivery(recipient_email) - user = User.where(email: recipient_email).first + user = User.find_by(email: recipient_email) Activity.log(user, :email, self) end end diff --git a/app/models/notification.rb b/app/models/notification.rb index 66343ae6a..e295c736c 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -45,7 +45,7 @@ class Notification < ApplicationRecord end def self.existent(user, notifiable) - unread.where(user: user, notifiable: notifiable).first + unread.find_by(user: user, notifiable: notifiable) end def notifiable_action diff --git a/app/models/officing/residence.rb b/app/models/officing/residence.rb index 7e61a0062..7ec430005 100644 --- a/app/models/officing/residence.rb +++ b/app/models/officing/residence.rb @@ -69,8 +69,7 @@ class Officing::Residence end def find_user_by_document - User.where(document_number: document_number, - document_type: document_type).first + User.find_by(document_number: document_number, document_type: document_type) end def residence_in_madrid @@ -96,7 +95,7 @@ class Officing::Residence end def geozone - Geozone.where(census_code: district_code).first + Geozone.find_by(census_code: district_code) end def district_code diff --git a/app/models/poll/ballot.rb b/app/models/poll/ballot.rb index 09cbf5916..55500cf02 100644 --- a/app/models/poll/ballot.rb +++ b/app/models/poll/ballot.rb @@ -22,11 +22,11 @@ class Poll::Ballot < ApplicationRecord end def ballot - Budget::Ballot.where(poll_ballot: self).first + Budget::Ballot.find_by(poll_ballot: self) end def find_investment(investment_id) - ballot.budget.investments.where(id: investment_id).first + ballot.budget.investments.find_by(id: investment_id) end def not_already_added?(investment) diff --git a/app/models/poll/booth.rb b/app/models/poll/booth.rb index 64174a7a6..d253c5c09 100644 --- a/app/models/poll/booth.rb +++ b/app/models/poll/booth.rb @@ -23,7 +23,7 @@ class Poll end def assignment_on_poll(poll) - booth_assignments.where(poll: poll).first + booth_assignments.find_by(poll: poll) end end end diff --git a/app/models/poll/voter.rb b/app/models/poll/voter.rb index d079ea991..8b6b79fd2 100644 --- a/app/models/poll/voter.rb +++ b/app/models/poll/voter.rb @@ -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&.id + self.geozone_id = Geozone.select(:id).find_by(census_code: census_api_response.district_code)&.id self.age = voter_age(census_api_response.date_of_birth) end end diff --git a/app/models/setting.rb b/app/models/setting.rb index db0b6645c..a63cda9ac 100644 --- a/app/models/setting.rb +++ b/app/models/setting.rb @@ -35,7 +35,7 @@ class Setting < ApplicationRecord end def []=(key, value) - setting = where(key: key).first || new(key: key) + setting = find_by(key: key) || new(key: key) setting.value = value.presence setting.save! value @@ -50,7 +50,7 @@ class Setting < ApplicationRecord end def remove(key) - setting = where(key: key).first + setting = find_by(key: key) setting.destroy if setting.present? end diff --git a/app/models/signature.rb b/app/models/signature.rb index 8395e7cca..2c70db00b 100644 --- a/app/models/signature.rb +++ b/app/models/signature.rb @@ -36,7 +36,7 @@ class Signature < ApplicationRecord end def assign_signature_to_vote - vote = Vote.where(votable: signable, voter: user).first + vote = Vote.find_by(votable: signable, voter: user) vote&.update!(signature: self) end @@ -55,7 +55,7 @@ class Signature < ApplicationRecord email: nil, date_of_birth: @census_api_response.date_of_birth, gender: @census_api_response.gender, - geozone: Geozone.where(census_code: @census_api_response.district_code).first + geozone: Geozone.find_by(census_code: @census_api_response.district_code) } User.create!(user_params) end @@ -84,7 +84,7 @@ class Signature < ApplicationRecord end def set_user - user = User.where(document_number: document_number).first + user = User.find_by(document_number: document_number) update(user: user) end diff --git a/app/models/user.rb b/app/models/user.rb index c1f032781..a6abde504 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -237,8 +237,8 @@ class User < ApplicationRecord end def take_votes_if_erased_document(document_number, document_type) - erased_user = User.erased.where(document_number: document_number) - .where(document_type: document_type).first + erased_user = User.erased.find_by(document_number: document_number, + document_type: document_type) if erased_user.present? take_votes_from(erased_user) erased_user.update!(document_number: nil, document_type: nil) @@ -345,8 +345,8 @@ class User < ApplicationRecord def self.find_for_database_authentication(warden_conditions) conditions = warden_conditions.dup login = conditions.delete(:login) - where(conditions.to_hash).where(["lower(email) = ?", login.downcase]).first || - where(conditions.to_hash).where(["username = ?", login]).first + where(conditions.to_hash).find_by(["lower(email) = ?", login.downcase]) || + where(conditions.to_hash).find_by(["username = ?", login]) end def self.find_by_manager_login(manager_login) diff --git a/app/models/verification/email.rb b/app/models/verification/email.rb index 2a4815279..91b981f24 100644 --- a/app/models/verification/email.rb +++ b/app/models/verification/email.rb @@ -19,7 +19,7 @@ class Verification::Email end def user - User.where(document_number: verified_user.document_number).first + User.find_by(document_number: verified_user.document_number) end def generate_token diff --git a/app/models/verification/management/email.rb b/app/models/verification/management/email.rb index b538d0505..dd3111222 100644 --- a/app/models/verification/management/email.rb +++ b/app/models/verification/management/email.rb @@ -13,7 +13,7 @@ class Verification::Management::Email delegate :username, to: :user, allow_nil: true def user - @user ||= User.where(email: email).first + @user ||= User.find_by(email: email) end def user? diff --git a/app/models/verification/residence.rb b/app/models/verification/residence.rb index eaa56ca42..c33de983d 100644 --- a/app/models/verification/residence.rb +++ b/app/models/verification/residence.rb @@ -61,7 +61,7 @@ class Verification::Residence end def geozone - Geozone.where(census_code: district_code).first + Geozone.find_by(census_code: district_code) end def district_code diff --git a/app/models/widget/feed.rb b/app/models/widget/feed.rb index 775048860..4dfce59f4 100644 --- a/app/models/widget/feed.rb +++ b/app/models/widget/feed.rb @@ -8,7 +8,7 @@ class Widget::Feed < ApplicationRecord end def setting - Setting.where(key: "homepage.widgets.feeds.#{kind}").first + Setting.find_by(key: "homepage.widgets.feeds.#{kind}") end def self.active diff --git a/config/initializers/i18n_translation.rb b/config/initializers/i18n_translation.rb index aa500468c..aae984e1f 100644 --- a/config/initializers/i18n_translation.rb +++ b/config/initializers/i18n_translation.rb @@ -9,7 +9,7 @@ module ActionView def t(key, options = {}) current_locale = options[:locale].presence || I18n.locale - i18_content = I18nContent.find_by_key(key) + i18_content = I18nContent.find_by(key: key) translation = I18nContentTranslation.where(i18n_content_id: i18_content&.id, locale: current_locale).first&.value translation.presence || translate(key, options) diff --git a/db/pages/accessibility.rb b/db/pages/accessibility.rb index 5741a57cc..006368164 100644 --- a/db/pages/accessibility.rb +++ b/db/pages/accessibility.rb @@ -1,4 +1,4 @@ -if SiteCustomization::Page.find_by_slug("accessibility").nil? +if SiteCustomization::Page.find_by(slug: "accessibility").nil? page = SiteCustomization::Page.new(slug: "accessibility", status: "published") page.title = I18n.t("pages.accessibility.title") diff --git a/db/pages/conditions.rb b/db/pages/conditions.rb index 8a57b63cc..c47216b51 100644 --- a/db/pages/conditions.rb +++ b/db/pages/conditions.rb @@ -1,4 +1,4 @@ -if SiteCustomization::Page.find_by_slug("conditions").nil? +if SiteCustomization::Page.find_by(slug: "conditions").nil? page = SiteCustomization::Page.new(slug: "conditions", status: "published") page.print_content_flag = true page.title = I18n.t("pages.conditions.title") diff --git a/db/pages/faq.rb b/db/pages/faq.rb index 6479e1b57..3e960ae14 100644 --- a/db/pages/faq.rb +++ b/db/pages/faq.rb @@ -1,4 +1,4 @@ -if SiteCustomization::Page.find_by_slug("faq").nil? +if SiteCustomization::Page.find_by(slug: "faq").nil? page = SiteCustomization::Page.new(slug: "faq", status: "published") page.title = I18n.t("pages.help.faq.page.title") page.content = "
#{I18n.t("pages.help.faq.page.description")}
" diff --git a/db/pages/privacy.rb b/db/pages/privacy.rb index f06b1687c..71e79880a 100644 --- a/db/pages/privacy.rb +++ b/db/pages/privacy.rb @@ -1,4 +1,4 @@ -if SiteCustomization::Page.find_by_slug("privacy").nil? +if SiteCustomization::Page.find_by(slug: "privacy").nil? page = SiteCustomization::Page.new(slug: "privacy", status: "published") page.print_content_flag = true page.title = I18n.t("pages.privacy.title") diff --git a/db/pages/welcome_level_three_verified.rb b/db/pages/welcome_level_three_verified.rb index 2c00e4fb0..8c2caaaac 100644 --- a/db/pages/welcome_level_three_verified.rb +++ b/db/pages/welcome_level_three_verified.rb @@ -1,4 +1,4 @@ -if SiteCustomization::Page.find_by_slug("welcome_level_three_verified").nil? +if SiteCustomization::Page.find_by(slug: "welcome_level_three_verified").nil? page = SiteCustomization::Page.new(slug: "welcome_level_three_verified", status: "published") page.title = I18n.t("welcome.welcome.title") diff --git a/db/pages/welcome_level_two_verified.rb b/db/pages/welcome_level_two_verified.rb index 911a0c850..a459a6fa6 100644 --- a/db/pages/welcome_level_two_verified.rb +++ b/db/pages/welcome_level_two_verified.rb @@ -1,4 +1,4 @@ -if SiteCustomization::Page.find_by_slug("welcome_level_two_verified").nil? +if SiteCustomization::Page.find_by(slug: "welcome_level_two_verified").nil? page = SiteCustomization::Page.new(slug: "welcome_level_two_verified", status: "published") page.title = I18n.t("welcome.welcome.title") diff --git a/db/pages/welcome_not_verified.rb b/db/pages/welcome_not_verified.rb index 51df2ac39..3ff99142d 100644 --- a/db/pages/welcome_not_verified.rb +++ b/db/pages/welcome_not_verified.rb @@ -1,4 +1,4 @@ -if SiteCustomization::Page.find_by_slug("welcome_not_verified").nil? +if SiteCustomization::Page.find_by(slug: "welcome_not_verified").nil? page = SiteCustomization::Page.new(slug: "welcome_not_verified", status: "published") page.title = I18n.t("welcome.welcome.title") diff --git a/spec/features/admin/site_customization/pages_spec.rb b/spec/features/admin/site_customization/pages_spec.rb index 4be5819c4..17b619659 100644 --- a/spec/features/admin/site_customization/pages_spec.rb +++ b/spec/features/admin/site_customization/pages_spec.rb @@ -25,7 +25,7 @@ describe "Admin custom pages" do expect(SiteCustomization::Page.count).to be 7 slugs.each do |slug| - expect(SiteCustomization::Page.find_by_slug(slug).status).to eq "published" + expect(SiteCustomization::Page.find_by(slug: slug).status).to eq "published" end expect(all("[id^='site_customization_page_']").count).to be 7