Apply Style/CollectionMethods rubocop rule

We were already using `map` and `reduce` almost everywhere.
This commit is contained in:
Javi Martín
2019-10-26 00:44:19 +02:00
parent e3bfcbcd25
commit eafb4018bf
27 changed files with 57 additions and 56 deletions

View File

@@ -379,6 +379,9 @@ Style/ClassCheck:
Style/ClassVars:
Enabled: true
Style/CollectionMethods:
Enabled: true
Style/Not:
Enabled: true

View File

@@ -80,9 +80,9 @@ class Admin::StatsController < Admin::BaseController
@vote_count = @budget.lines.count
@vote_count_by_heading = @budget.lines.group(:heading_id).count.collect { |k, v| [Budget::Heading.find(k).name, v] }.sort
@vote_count_by_heading = @budget.lines.group(:heading_id).count.map { |k, v| [Budget::Heading.find(k).name, v] }.sort
@user_count_by_district = User.where.not(balloted_heading_id: nil).group(:balloted_heading_id).count.collect { |k, v| [Budget::Heading.find(k).name, v] }.sort
@user_count_by_district = User.where.not(balloted_heading_id: nil).group(:balloted_heading_id).count.map { |k, v| [Budget::Heading.find(k).name, v] }.sort
end
def polls

View File

@@ -15,7 +15,7 @@ module Dashboard::GroupSupports
def accumulate_supports(grouped_votes)
grouped_votes.each do |group, votes|
grouped_votes[group] = votes.inject(0) { |sum, vote| sum + vote.vote_weight }
grouped_votes[group] = votes.reduce(0) { |sum, vote| sum + vote.vote_weight }
end
accumulated = 0

View File

@@ -20,7 +20,7 @@ module RemotelyTranslatable
end
def resources_groups(*args)
feeds = args.detect { |arg| arg&.first.class == Widget::Feed } || []
feeds = args.find { |arg| arg&.first.class == Widget::Feed } || []
args.compact - [feeds] + feeds.map(&:items)
end

View File

@@ -58,7 +58,7 @@ class Tracking::BudgetInvestmentsController < Tracking::BaseController
}
]
investment_headings.inject(all_headings_filter) do |filters, heading|
investment_headings.reduce(all_headings_filter) do |filters, heading|
filters << {
name: heading.name,
id: heading.id,

View File

@@ -83,7 +83,7 @@ class Valuation::BudgetInvestmentsController < Valuation::BaseController
}
]
investment_headings.inject(all_headings_filter) do |filters, heading|
investment_headings.reduce(all_headings_filter) do |filters, heading|
filters << {
name: heading.name,
id: heading.id,

View File

@@ -83,9 +83,7 @@ module AdminHelper
end
def admin_select_options
Administrator.with_user
.collect { |v| [v.description_or_name, v.id] }
.sort_by { |a| a[0] }
Administrator.with_user.map { |v| [v.description_or_name, v.id] }.sort_by { |a| a[0] }
end
def admin_submit_action(resource)

View File

@@ -4,6 +4,6 @@ module GeozonesHelper
end
def geozone_select_options
Geozone.all.order(name: :asc).collect { |g| [g.name, g.id] }
Geozone.all.order(name: :asc).map { |g| [g.name, g.id] }
end
end

View File

@@ -1,6 +1,6 @@
module PollsHelper
def poll_select_options(include_all = nil)
options = @polls.collect do |poll|
options = @polls.map do |poll|
[poll.name, current_path_with_query_params(poll_id: poll.id)]
end
options << all_polls if include_all

View File

@@ -28,7 +28,7 @@ module ProposalsHelper
end
def retire_proposals_options
Proposal::RETIRE_OPTIONS.collect { |option| [t("proposals.retire_options.#{option}"), option] }
Proposal::RETIRE_OPTIONS.map { |option| [t("proposals.retire_options.#{option}"), option] }
end
def empty_recommended_proposals_message_text(user)

View File

@@ -8,7 +8,7 @@ module ShiftsHelper
def shift_recount_scrutiny_dates(booth, polls)
return [] if polls.blank?
dates = polls.map(&:ends_at).map(&:to_date).sort.inject([]) do |total, date|
dates = polls.map(&:ends_at).map(&:to_date).sort.reduce([]) do |total, date|
initial_date = date < Date.current ? Date.current : date
total << (initial_date..date + Poll::RECOUNT_DURATION).to_a
end
@@ -33,7 +33,7 @@ module ShiftsHelper
end
def officer_select_options(officers)
officers.collect { |officer| [officer.name, officer.id] }
officers.map { |officer| [officer.name, officer.id] }
end
private

View File

@@ -60,7 +60,7 @@ module TranslatableFormHelper
end
def existing_translation_for(locale)
@object.translations.detect { |translation| translation.locale == locale }
@object.translations.find { |translation| translation.locale == locale }
end
def new_translation_for(locale)

View File

@@ -1,6 +1,6 @@
module UserSegmentsHelper
def user_segments_options
UserSegments::SEGMENTS.collect do |user_segment_name|
UserSegments::SEGMENTS.map do |user_segment_name|
[t("admin.segment_recipient.#{user_segment_name}"), user_segment_name]
end
end

View File

@@ -5,11 +5,11 @@ module ValuationHelper
def valuator_select_options
Valuator.order("description ASC").order("users.email ASC").includes(:user).
collect { |v| [v.description_or_email, "valuator_#{v.id}"] }
map { |v| [v.description_or_email, "valuator_#{v.id}"] }
end
def valuator_group_select_options
ValuatorGroup.order("name ASC").collect { |g| [g.name, "group_#{g.id}"] }
ValuatorGroup.order("name ASC").map { |g| [g.name, "group_#{g.id}"] }
end
def explanation_field(field)

View File

@@ -376,11 +376,11 @@ class Budget
end
def assigned_valuators
self.valuators.collect(&:description_or_name).compact.join(", ").presence
self.valuators.map(&:description_or_name).compact.join(", ").presence
end
def assigned_valuation_groups
self.valuator_groups.collect(&:name).compact.join(", ").presence
self.valuator_groups.map(&:name).compact.join(", ").presence
end
def valuation_tag_list

View File

@@ -54,7 +54,7 @@ class Budget::Stats
end
def total_votes
budget.ballots.pluck(:ballot_lines_count).inject(0) { |sum, x| sum + x }
budget.ballots.pluck(:ballot_lines_count).reduce(0) { |sum, x| sum + x }
end
def total_selected_investments
@@ -72,18 +72,18 @@ class Budget::Stats
end
groups[:total] = Hash.new(0)
groups[:total][:total_investments_count] = groups.collect { |_k, v| v[:total_investments_count] }.sum
groups[:total][:total_participants_support_phase] = groups.collect { |_k, v| v[:total_participants_support_phase] }.sum
groups[:total][:total_participants_vote_phase] = groups.collect { |_k, v| v[:total_participants_vote_phase] }.sum
groups[:total][:total_participants_every_phase] = groups.collect { |_k, v| v[:total_participants_every_phase] }.sum
groups[:total][:total_investments_count] = groups.map { |_k, v| v[:total_investments_count] }.sum
groups[:total][:total_participants_support_phase] = groups.map { |_k, v| v[:total_participants_support_phase] }.sum
groups[:total][:total_participants_vote_phase] = groups.map { |_k, v| v[:total_participants_vote_phase] }.sum
groups[:total][:total_participants_every_phase] = groups.map { |_k, v| v[:total_participants_every_phase] }.sum
budget.headings.each do |heading|
groups[heading.id].merge!(calculate_heading_stats_with_totals(groups[heading.id], groups[:total], heading.population))
end
groups[:total][:percentage_participants_support_phase] = groups.collect { |_k, v| v[:percentage_participants_support_phase] }.sum
groups[:total][:percentage_participants_vote_phase] = groups.collect { |_k, v| v[:percentage_participants_vote_phase] }.sum
groups[:total][:percentage_participants_every_phase] = groups.collect { |_k, v| v[:percentage_participants_every_phase] }.sum
groups[:total][:percentage_participants_support_phase] = groups.map { |_k, v| v[:percentage_participants_support_phase] }.sum
groups[:total][:percentage_participants_vote_phase] = groups.map { |_k, v| v[:percentage_participants_vote_phase] }.sum
groups[:total][:percentage_participants_every_phase] = groups.map { |_k, v| v[:percentage_participants_every_phase] }.sum
groups
end

View File

@@ -15,7 +15,7 @@ module SearchCache
def searchable_values_sql
searchable_values
.select { |k, _| k.present? }
.collect { |value, weight| set_tsvector(value, weight) }
.map { |value, weight| set_tsvector(value, weight) }
.join(" || ")
end

View File

@@ -71,7 +71,7 @@ class Poll::Question < ApplicationRecord
end
def answers_total_votes
question_answers.visibles.inject(0) { |total, question_answer| total + question_answer.total_votes }
question_answers.visibles.reduce(0) { |total, question_answer| total + question_answer.total_votes }
end
def most_voted_answer_id

View File

@@ -71,7 +71,7 @@ class Signature < ApplicationRecord
end
def in_census?
document_types.detect do |document_type|
document_types.find do |document_type|
response = CensusCaller.new.call(document_type, document_number, date_of_birth, postal_code)
if response.valid?
@census_api_response = response

View File

@@ -35,7 +35,7 @@ class SignatureSheet < ApplicationRecord
end
def parsed_required_fields_to_verify_groups
required_fields_to_verify.split(/[;]/).collect { |d| d.gsub(/\s+/, "") }.map { |group| group.split(/[,]/) }
required_fields_to_verify.split(/[;]/).map { |d| d.gsub(/\s+/, "") }.map { |group| group.split(/[,]/) }
end
def signable_found

View File

@@ -12,7 +12,7 @@ class Widget::Feed < ApplicationRecord
end
def self.active
KINDS.collect do |kind|
KINDS.map do |kind|
feed = find_or_create_by!(kind: kind)
feed if feed.active?
end.compact

View File

@@ -8,7 +8,7 @@ unless defined?(Spring)
require "bundler"
lockfile = Bundler::LockfileParser.new(Bundler.default_lockfile.read)
spring = lockfile.specs.detect { |spec| spec.name == "spring" }
spring = lockfile.specs.find { |spec| spec.name == "spring" }
if spring
Gem.use_paths Gem.dir, Bundler.bundle_path.to_s, *Gem.path
gem "spring", spring.version

View File

@@ -384,7 +384,7 @@ namespace :proposal_actions do
5
]
votes_count = expected_supports.inject(0.0) { |sum, x| sum + x }
votes_count = expected_supports.reduce(0.0) { |sum, x| sum + x }
goal_votes = Setting["votes_for_proposal_success"].to_f
cached_votes_up = 0

View File

@@ -632,11 +632,11 @@ describe "Budget Investments" do
visit budget_investments_path(budget, heading_id: heading.id)
within(".submenu .is-active") { expect(page).to have_content "random" }
order = all(".budget-investment h3").collect { |i| i.text }
order = all(".budget-investment h3").map { |i| i.text }
expect(order).not_to be_empty
visit budget_investments_path(budget, heading_id: heading.id)
new_order = all(".budget-investment h3").collect { |i| i.text }
new_order = all(".budget-investment h3").map { |i| i.text }
expect(order).to eq(new_order)
end
@@ -645,14 +645,14 @@ describe "Budget Investments" do
(per_page + 2).times { create(:budget_investment, heading: heading) }
visit budget_investments_path(budget, heading_id: heading.id)
order = all(".budget-investment h3").collect { |i| i.text }
order = all(".budget-investment h3").map { |i| i.text }
expect(order).not_to be_empty
click_link "highest rated"
click_link "random"
visit budget_investments_path(budget, heading_id: heading.id)
new_order = all(".budget-investment h3").collect { |i| i.text }
new_order = all(".budget-investment h3").map { |i| i.text }
expect(order).to eq(new_order)
end
@@ -662,7 +662,7 @@ describe "Budget Investments" do
visit budget_investments_path(budget, heading_id: heading.id)
order = all(".budget-investment h3").collect { |i| i.text }
order = all(".budget-investment h3").map { |i| i.text }
expect(order).not_to be_empty
click_link "Next"
@@ -671,7 +671,7 @@ describe "Budget Investments" do
click_link "Previous"
expect(page).to have_content "You're on page 1"
new_order = all(".budget-investment h3").collect { |i| i.text }
new_order = all(".budget-investment h3").map { |i| i.text }
expect(order).to eq(new_order)
end
@@ -680,13 +680,13 @@ describe "Budget Investments" do
visit budget_investments_path(budget, heading_id: heading.id)
order = all(".budget-investment h3").collect { |i| i.text }
order = all(".budget-investment h3").map { |i| i.text }
expect(order).not_to be_empty
click_link Budget::Investment.first.title
click_link "Go back"
new_order = all(".budget-investment h3").collect { |i| i.text }
new_order = all(".budget-investment h3").map { |i| i.text }
expect(order).to eq(new_order)
end
@@ -812,11 +812,11 @@ describe "Budget Investments" do
budget.update!(phase: "finished")
visit budget_investments_path(budget, heading_id: heading.id)
order = all(".budget-investment h3").collect { |i| i.text }
order = all(".budget-investment h3").map { |i| i.text }
expect(order).not_to be_empty
visit budget_investments_path(budget, heading_id: heading.id)
new_order = all(".budget-investment h3").collect { |i| i.text }
new_order = all(".budget-investment h3").map { |i| i.text }
expect(order).to eq(new_order)
end
@@ -844,7 +844,7 @@ describe "Budget Investments" do
end
def investments_order
all(".budget-investment h3").collect { |i| i.text }
all(".budget-investment h3").map { |i| i.text }
end
end
@@ -1038,7 +1038,7 @@ describe "Budget Investments" do
visit new_budget_investment_path(budget)
select_options = find("#budget_investment_heading_id").all("option").collect(&:text)
select_options = find("#budget_investment_heading_id").all("option").map(&:text)
expect(select_options).to eq ["",
"Toda la ciudad",
"Health: More health professionals",

View File

@@ -133,7 +133,7 @@ describe "Legislation Proposals" do
end
def legislation_proposals_order
all("[id^='legislation_proposal_']").collect { |e| e[:id] }
all("[id^='legislation_proposal_']").map { |e| e[:id] }
end
scenario "Create a legislation proposal with an image", :js do

View File

@@ -23,7 +23,7 @@ end
def extract_fields(response, collection_name, field_chain)
fields = field_chain.split(".")
dig(response, "data.#{collection_name}.edges").collect do |node|
dig(response, "data.#{collection_name}.edges").map do |node|
begin
if fields.size > 1
node["node"][fields.first][fields.second]
@@ -60,8 +60,8 @@ describe "Consul Schema" do
comment_2 = create(:comment, author: comments_author, commentable: proposal)
response = execute("{ proposal(id: #{proposal.id}) { comments { edges { node { body } } } } }")
comments = dig(response, "data.proposal.comments.edges").collect { |edge| edge["node"] }
comment_bodies = comments.collect { |comment| comment["body"] }
comments = dig(response, "data.proposal.comments.edges").map { |edge| edge["node"] }
comment_bodies = comments.map { |comment| comment["body"] }
expect(comment_bodies).to match_array([comment_1.body, comment_2.body])
end

View File

@@ -59,7 +59,7 @@ describe LocalCensusRecords::Import do
import.save!
valid_document_numbers = ["44556678T", "33556678T", "22556678T", "X11556678"]
expect(import.created_records.collect(&:document_number)).to eq(valid_document_numbers)
expect(import.created_records.map(&:document_number)).to eq(valid_document_numbers)
end
it "Add invalid local census records to invalid_records array" do
@@ -74,13 +74,13 @@ describe LocalCensusRecords::Import do
invalid_records_date_of_births = [Date.parse("07/08/1984"), Date.parse("07/08/1985"), nil,
Date.parse("07/08/1987")]
invalid_records_postal_codes = ["7008", "7009", "7010", nil]
expect(import.invalid_records.collect(&:document_type))
expect(import.invalid_records.map(&:document_type))
.to eq(invalid_records_document_types)
expect(import.invalid_records.collect(&:document_number))
expect(import.invalid_records.map(&:document_number))
.to eq(invalid_records_document_numbers)
expect(import.invalid_records.collect(&:date_of_birth))
expect(import.invalid_records.map(&:date_of_birth))
.to eq(invalid_records_date_of_births)
expect(import.invalid_records.collect(&:postal_code))
expect(import.invalid_records.map(&:postal_code))
.to eq(invalid_records_postal_codes)
end
end