Apply Rails/FindBy rubocop rule

We were already using it in most places.
This commit is contained in:
Javi Martín
2019-10-20 16:57:25 +02:00
parent 794857c31c
commit 93c6347b45
18 changed files with 27 additions and 28 deletions

View File

@@ -27,9 +27,6 @@ Rails/EnvironmentComparison:
Rails/Exit: Rails/Exit:
Enabled: true Enabled: true
Rails/FindBy:
Enabled: true
Rails/FindEach: Rails/FindEach:
Enabled: true Enabled: true

View File

@@ -156,6 +156,9 @@ Rails/ApplicationRecord:
Rails/Date: Rails/Date:
Enabled: true Enabled: true
Rails/FindBy:
Enabled: true
Rails/HttpPositionalArguments: Rails/HttpPositionalArguments:
Enabled: true Enabled: true

View File

@@ -68,7 +68,7 @@ class Budget
def heading_for_group(group) def heading_for_group(group)
return nil unless has_lines_in_group?(group) return nil unless has_lines_in_group?(group)
investments.where(group: group).first.heading investments.find_by(group: group).heading
end end
def casted_offline? def casted_offline?

View File

@@ -6,7 +6,7 @@ module Relationable
end end
def find_related_content(relationable) def find_related_content(relationable)
RelatedContent.where(parent_relationable: self, child_relationable: relationable).first RelatedContent.find_by(parent_relationable: self, child_relationable: relationable)
end end
def relationed_contents def relationed_contents

View File

@@ -30,7 +30,7 @@ class Legislation::Question < ApplicationRecord
end end
def answer_for_user(user) def answer_for_user(user)
answers.where(user: user).first answers.find_by(user: user)
end end
def comments_for_verified_residents_only? def comments_for_verified_residents_only?

View File

@@ -62,7 +62,7 @@ class Newsletter < ApplicationRecord
end end
def log_delivery(recipient_email) def log_delivery(recipient_email)
user = User.where(email: recipient_email).first user = User.find_by(email: recipient_email)
Activity.log(user, :email, self) Activity.log(user, :email, self)
end end
end end

View File

@@ -45,7 +45,7 @@ class Notification < ApplicationRecord
end end
def self.existent(user, notifiable) def self.existent(user, notifiable)
unread.where(user: user, notifiable: notifiable).first unread.find_by(user: user, notifiable: notifiable)
end end
def notifiable_action def notifiable_action

View File

@@ -69,8 +69,7 @@ class Officing::Residence
end end
def find_user_by_document def find_user_by_document
User.where(document_number: document_number, User.find_by(document_number: document_number, document_type: document_type)
document_type: document_type).first
end end
def residence_in_madrid def residence_in_madrid
@@ -96,7 +95,7 @@ class Officing::Residence
end end
def geozone def geozone
Geozone.where(census_code: district_code).first Geozone.find_by(census_code: district_code)
end end
def district_code def district_code

View File

@@ -22,11 +22,11 @@ class Poll::Ballot < ApplicationRecord
end end
def ballot def ballot
Budget::Ballot.where(poll_ballot: self).first Budget::Ballot.find_by(poll_ballot: self)
end end
def find_investment(investment_id) def find_investment(investment_id)
ballot.budget.investments.where(id: investment_id).first ballot.budget.investments.find_by(id: investment_id)
end end
def not_already_added?(investment) def not_already_added?(investment)

View File

@@ -23,7 +23,7 @@ class Poll
end end
def assignment_on_poll(poll) def assignment_on_poll(poll)
booth_assignments.where(poll: poll).first booth_assignments.find_by(poll: poll)
end end
end end
end end

View File

@@ -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&.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) self.age = voter_age(census_api_response.date_of_birth)
end end
end end

View File

@@ -35,7 +35,7 @@ class Setting < ApplicationRecord
end end
def []=(key, value) def []=(key, value)
setting = where(key: key).first || new(key: key) setting = find_by(key: key) || new(key: key)
setting.value = value.presence setting.value = value.presence
setting.save! setting.save!
value value
@@ -50,7 +50,7 @@ class Setting < ApplicationRecord
end end
def remove(key) def remove(key)
setting = where(key: key).first setting = find_by(key: key)
setting.destroy if setting.present? setting.destroy if setting.present?
end end

View File

@@ -36,7 +36,7 @@ class Signature < ApplicationRecord
end end
def assign_signature_to_vote 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) vote&.update!(signature: self)
end end
@@ -55,7 +55,7 @@ class Signature < ApplicationRecord
email: nil, email: nil,
date_of_birth: @census_api_response.date_of_birth, date_of_birth: @census_api_response.date_of_birth,
gender: @census_api_response.gender, 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) User.create!(user_params)
end end
@@ -84,7 +84,7 @@ class Signature < ApplicationRecord
end end
def set_user def set_user
user = User.where(document_number: document_number).first user = User.find_by(document_number: document_number)
update(user: user) update(user: user)
end end

View File

@@ -237,8 +237,8 @@ class User < ApplicationRecord
end end
def take_votes_if_erased_document(document_number, document_type) def take_votes_if_erased_document(document_number, document_type)
erased_user = User.erased.where(document_number: document_number) erased_user = User.erased.find_by(document_number: document_number,
.where(document_type: document_type).first document_type: document_type)
if erased_user.present? if erased_user.present?
take_votes_from(erased_user) take_votes_from(erased_user)
erased_user.update!(document_number: nil, document_type: nil) erased_user.update!(document_number: nil, document_type: nil)
@@ -345,8 +345,8 @@ class User < ApplicationRecord
def self.find_for_database_authentication(warden_conditions) def self.find_for_database_authentication(warden_conditions)
conditions = warden_conditions.dup conditions = warden_conditions.dup
login = conditions.delete(:login) login = conditions.delete(:login)
where(conditions.to_hash).where(["lower(email) = ?", login.downcase]).first || where(conditions.to_hash).find_by(["lower(email) = ?", login.downcase]) ||
where(conditions.to_hash).where(["username = ?", login]).first where(conditions.to_hash).find_by(["username = ?", login])
end end
def self.find_by_manager_login(manager_login) def self.find_by_manager_login(manager_login)

View File

@@ -19,7 +19,7 @@ class Verification::Email
end end
def user def user
User.where(document_number: verified_user.document_number).first User.find_by(document_number: verified_user.document_number)
end end
def generate_token def generate_token

View File

@@ -13,7 +13,7 @@ class Verification::Management::Email
delegate :username, to: :user, allow_nil: true delegate :username, to: :user, allow_nil: true
def user def user
@user ||= User.where(email: email).first @user ||= User.find_by(email: email)
end end
def user? def user?

View File

@@ -61,7 +61,7 @@ class Verification::Residence
end end
def geozone def geozone
Geozone.where(census_code: district_code).first Geozone.find_by(census_code: district_code)
end end
def district_code def district_code

View File

@@ -8,7 +8,7 @@ class Widget::Feed < ApplicationRecord
end end
def setting def setting
Setting.where(key: "homepage.widgets.feeds.#{kind}").first Setting.find_by(key: "homepage.widgets.feeds.#{kind}")
end end
def self.active def self.active