Merge pull request #4881 from consul/rails_6.0_methods

Use Rails 6.0 methods to simplify code
This commit is contained in:
Javi Martín
2022-08-24 18:35:17 +02:00
committed by GitHub
16 changed files with 49 additions and 68 deletions

View File

@@ -288,6 +288,9 @@ Rails/OutputSafety:
Exclude:
- app/helpers/text_with_links_helper.rb
Rails/Pick:
Enabled: true
Rails/PluckId:
Enabled: true

View File

@@ -72,7 +72,6 @@ end
group :development, :test do
gem "bullet", "~> 6.1.4"
gem "byebug", "~> 11.1.3"
gem "database_cleaner", "~> 2.0.1"
gem "factory_bot_rails", "~> 6.2.0"
gem "faker", "~> 2.18.0"
gem "i18n-tasks", "~> 0.9.34"

View File

@@ -168,12 +168,6 @@ GEM
crass (1.0.6)
daemons (1.4.0)
dalli (2.7.11)
database_cleaner (2.0.1)
database_cleaner-active_record (~> 2.0.0)
database_cleaner-active_record (2.0.1)
activerecord (>= 5.a)
database_cleaner-core (~> 2.0.0)
database_cleaner-core (2.0.1)
delayed_job (4.1.9)
activesupport (>= 3.0, < 6.2)
delayed_job_active_record (4.1.6)
@@ -730,7 +724,6 @@ DEPENDENCIES
coveralls (~> 0.8.23)
daemons (~> 1.4.0)
dalli (~> 2.7.11)
database_cleaner (~> 2.0.1)
delayed_job_active_record (~> 4.1.6)
devise (~> 4.8.0)
devise-security (~> 0.16.0)

View File

@@ -5,9 +5,8 @@ module Attachable
has_one_attached :attachment
attr_accessor :cached_attachment
validate :attachment_presence
validates :attachment,
presence: true,
file_content_type: {
allow: ->(record) { record.accepted_content_types },
if: -> { association_class && attachment.attached? },
@@ -63,12 +62,4 @@ module Attachable
def file_path
ActiveStorage::Blob.service.path_for(attachment.blob.key)
end
private
def attachment_presence
unless attachment.attached?
errors.add(:attachment, I18n.t("errors.messages.blank"))
end
end
end

View File

@@ -31,7 +31,7 @@ class Setting < ApplicationRecord
class << self
def [](key)
where(key: key).pluck(:value).first.presence
where(key: key).pick(:value).presence
end
def []=(key, value)
@@ -42,7 +42,7 @@ class Setting < ApplicationRecord
def rename_key(from:, to:)
if where(key: to).empty?
value = where(key: from).pluck(:value).first.presence
value = where(key: from).pick(:value).presence
create!(key: to, value: value)
end
remove(from)

View File

@@ -5,7 +5,7 @@
</button>
<strong>
<% errors_count = resource.errors.messages.reject { |attribute| attribute == :base }.count %>
<% errors_count = resource.errors.messages.excluding(:base).count %>
<%= pluralize errors_count, t("form.error"), t("form.errors") %>

View File

@@ -17,7 +17,7 @@ module Globalize
end
def Globalize.set_fallbacks_to_all_available_locales
Globalize.fallbacks = I18n.available_locales.each_with_object({}) do |locale, fallbacks|
fallbacks[locale] = (I18n.fallbacks[locale] + I18n.available_locales).uniq
Globalize.fallbacks = I18n.available_locales.index_with do |locale|
(I18n.fallbacks[locale] + I18n.available_locales).uniq
end
end

View File

@@ -1,7 +1,4 @@
unless Rails.env.test?
require "database_cleaner"
DatabaseCleaner.clean_with :truncation
end
ActiveRecord::Tasks::DatabaseTasks.truncate_all unless Rails.env.test?
@logger = Logger.new(STDOUT)
@logger.formatter = proc do |_severity, _datetime, _progname, msg|
msg unless @avoid_log

View File

@@ -1,33 +1,33 @@
section "Creating Admin Notifications & Templates" do
AdminNotification.create!(
random_locales_attributes(
%i[title body].map do |attribute|
[attribute, -> { I18n.t("seeds.admin_notifications.proposal.#{attribute}") }]
end.to_h
%i[title body].index_with do |attribute|
-> { I18n.t("seeds.admin_notifications.proposal.#{attribute}") }
end
).merge(link: "#{Setting["url"]}/proposals", segment_recipient: "administrators")
).deliver
AdminNotification.create!(
random_locales_attributes(
%i[title body].map do |attribute|
[attribute, -> { I18n.t("seeds.admin_notifications.help.#{attribute}") }]
end.to_h
%i[title body].index_with do |attribute|
-> { I18n.t("seeds.admin_notifications.help.#{attribute}") }
end
).merge(link: "https://crwd.in/consul", segment_recipient: "administrators")
).deliver
AdminNotification.create!(
random_locales_attributes(
%i[title body].map do |attribute|
[attribute, -> { I18n.t("seeds.admin_notifications.map.#{attribute}") }]
end.to_h
%i[title body].index_with do |attribute|
-> { I18n.t("seeds.admin_notifications.map.#{attribute}") }
end
).merge(segment_recipient: "administrators")
).deliver
AdminNotification.create!(
random_locales_attributes(
%i[title body].map do |attribute|
[attribute, -> { I18n.t("seeds.admin_notifications.budget.#{attribute}") }]
end.to_h
%i[title body].index_with do |attribute|
-> { I18n.t("seeds.admin_notifications.budget.#{attribute}") }
end
).merge(segment_recipient: "administrators", sent_at: nil)
)
end

View File

@@ -9,9 +9,9 @@ section "Creating header and cards for the homepage" do
Widget::Card.create!(
random_locales_attributes(
%i[title description link_text label].map do |attribute|
[attribute, -> { I18n.t("seeds.cards.header.#{attribute}") }]
end.to_h
%i[title description link_text label].index_with do |attribute|
-> { I18n.t("seeds.cards.header.#{attribute}") }
end
).merge(
link_url: "http://consulproject.org/",
header: true,
@@ -21,9 +21,9 @@ section "Creating header and cards for the homepage" do
Widget::Card.create!(
random_locales_attributes(
%i[title description link_text label].map do |attribute|
[attribute, -> { I18n.t("seeds.cards.debate.#{attribute}") }]
end.to_h
%i[title description link_text label].index_with do |attribute|
-> { I18n.t("seeds.cards.debate.#{attribute}") }
end
).merge(
link_url: "https://youtu.be/zU_0UN4VajY",
header: false,
@@ -33,9 +33,9 @@ section "Creating header and cards for the homepage" do
Widget::Card.create!(
random_locales_attributes(
%i[title description link_text label].map do |attribute|
[attribute, -> { I18n.t("seeds.cards.proposal.#{attribute}") }]
end.to_h
%i[title description link_text label].index_with do |attribute|
-> { I18n.t("seeds.cards.proposal.#{attribute}") }
end
).merge(
link_url: "https://youtu.be/ZHqBpT4uCoM",
header: false,
@@ -45,9 +45,9 @@ section "Creating header and cards for the homepage" do
Widget::Card.create!(
random_locales_attributes(
%i[title description link_text label].map do |attribute|
[attribute, -> { I18n.t("seeds.cards.budget.#{attribute}") }]
end.to_h
%i[title description link_text label].index_with do |attribute|
-> { I18n.t("seeds.cards.budget.#{attribute}") }
end
).merge(
link_url: "https://youtu.be/igQ8KGZdk9c",
header: false,

View File

@@ -160,7 +160,7 @@ describe Budget::Investment do
end
it "returns false in any other phase" do
Budget::Phase::PHASE_KINDS.reject { |phase| phase == "selecting" }.each do |phase|
Budget::Phase::PHASE_KINDS.excluding("selecting").each do |phase|
budget = create(:budget, phase: phase)
investment = create(:budget_investment, budget: budget)
@@ -178,7 +178,7 @@ describe Budget::Investment do
end
it "returns false in any other phase" do
Budget::Phase::PHASE_KINDS.reject { |phase| phase == "valuating" }.each do |phase|
Budget::Phase::PHASE_KINDS.excluding("valuating").each do |phase|
budget = create(:budget, phase: phase)
investment = create(:budget_investment, budget: budget)
@@ -203,7 +203,7 @@ describe Budget::Investment do
end
it "returns false in any other phase" do
Budget::Phase::PHASE_KINDS.reject { |phase| phase == "balloting" }.each do |phase|
Budget::Phase::PHASE_KINDS.excluding("balloting").each do |phase|
budget = create(:budget, phase: phase)
investment = create(:budget_investment, :selected, budget: budget)
@@ -1198,7 +1198,7 @@ describe Budget::Investment do
end
it "returns false if budget is not balloting phase" do
Budget::Phase::PHASE_KINDS.reject { |phase| phase == "balloting" }.each do |phase|
Budget::Phase::PHASE_KINDS.excluding("balloting").each do |phase|
budget.update!(phase: phase)
investment = create(:budget_investment, heading: heading1)

View File

@@ -16,7 +16,7 @@ shared_examples_for "globalizable" do |factory_name|
record.update!(attribute => "In English")
I18n.with_locale(:es) do
record.update!(required_fields.map { |field| [field, "En español"] }.to_h)
record.update!(required_fields.index_with("En español"))
record.update!(attribute => "En español")
end
@@ -26,7 +26,7 @@ shared_examples_for "globalizable" do |factory_name|
describe "Add a translation" do
it "Maintains existing translations" do
record.update!(translations_attributes: [
{ locale: :fr }.merge(fields.map { |field| [field, "En Français"] }.to_h)
{ locale: :fr }.merge(fields.index_with("En Français"))
])
record.reload
@@ -37,7 +37,7 @@ shared_examples_for "globalizable" do |factory_name|
it "Works with non-underscored locale name" do
record.update!(translations_attributes: [
{ locale: :"pt-BR" }.merge(fields.map { |field| [field, "Português"] }.to_h)
{ locale: :"pt-BR" }.merge(fields.index_with("Português"))
])
record.reload
@@ -63,7 +63,7 @@ shared_examples_for "globalizable" do |factory_name|
record.reload
record.update!(translations_attributes: [
{ locale: :de }.merge(fields.map { |field| [field, "Deutsche Sprache"] }.to_h)
{ locale: :de }.merge(fields.index_with("Deutsche Sprache"))
])
record.reload
@@ -102,7 +102,7 @@ shared_examples_for "globalizable" do |factory_name|
record.reload
record.update!(translations_attributes: [
{ id: record.translations.first.id }.merge(fields.map { |field| [field, "Actualizado"] }.to_h)
{ id: record.translations.first.id }.merge(fields.index_with("Actualizado"))
])
record.reload
@@ -155,7 +155,7 @@ shared_examples_for "globalizable" do |factory_name|
describe "Fallbacks" do
before do
I18n.with_locale(:de) do
record.update!(required_fields.map { |field| [field, "Deutsche Sprache"] }.to_h)
record.update!(required_fields.index_with("Deutsche Sprache"))
record.update!(attribute => "Deutsche Sprache")
end
end

View File

@@ -267,7 +267,7 @@ describe Poll::Stats do
expect(stats.version).to eq "v#{time.to_i}"
travel_back
unfreeze_time
travel_to 2.seconds.from_now do
expect(stats.version).to eq "v#{time.to_i}"

View File

@@ -45,7 +45,7 @@ RSpec.configure do |config|
example.run
self.use_transactional_tests = true
DatabaseCleaner.clean_with(:truncation)
ActiveRecord::Tasks::DatabaseTasks.truncate_all
Rails.application.load_seed
end

View File

@@ -96,11 +96,9 @@ describe "Admin shifts", :admin do
create(:poll_shift, :vote_collection_task, officer: officer, booth: booth, date: Date.current)
create(:poll_shift, :recount_scrutiny_task, officer: officer, booth: booth, date: Time.zone.tomorrow)
vote_collection_dates = (Date.current..poll.ends_at.to_date).to_a
.reject { |date| date == Date.current }
vote_collection_dates = (Date.current..poll.ends_at.to_date).excluding(Date.current)
.map { |date| I18n.l(date, format: :long) }
recount_scrutiny_dates = (poll.ends_at.to_date..poll.ends_at.to_date + 1.week).to_a
.reject { |date| date == Time.zone.tomorrow }
recount_scrutiny_dates = (poll.ends_at.to_date..poll.ends_at.to_date + 1.week).excluding(Time.zone.tomorrow)
.map { |date| I18n.l(date, format: :long) }
visit available_admin_booths_path

View File

@@ -176,7 +176,7 @@ describe "Voter" do
expect(page).not_to have_link(answer_yes.title)
end
travel_back
unfreeze_time
click_link "Sign out"