Add and apply Style/RedundantBegin rubocop rule

We're about to add code which might fall into the `RedundantBegin`
category, so we're adding the rule in order to prevent that.
This commit is contained in:
Javi Martín
2024-10-30 15:55:29 +01:00
parent 9f38ed6bae
commit 07202fea10
9 changed files with 109 additions and 124 deletions

View File

@@ -781,6 +781,9 @@ Style/RaiseArgs:
Style/RedundantArgument: Style/RedundantArgument:
Enabled: true Enabled: true
Style/RedundantBegin:
Enabled: true
Style/RedundantCondition: Style/RedundantCondition:
Enabled: true Enabled: true

View File

@@ -9,25 +9,23 @@ class GraphqlController < ApplicationController
class QueryStringError < StandardError; end class QueryStringError < StandardError; end
def execute def execute
begin raise GraphqlController::QueryStringError if query_string.nil?
raise GraphqlController::QueryStringError if query_string.nil?
result = ConsulSchema.execute( result = ConsulSchema.execute(
query_string, query_string,
variables: prepare_variables, variables: prepare_variables,
context: {}, context: {},
operation_name: params[:operationName] operation_name: params[:operationName]
) )
render json: result render json: result
rescue GraphqlController::QueryStringError rescue GraphqlController::QueryStringError
render json: { message: "Query string not present" }, status: :bad_request render json: { message: "Query string not present" }, status: :bad_request
rescue JSON::ParserError rescue JSON::ParserError
render json: { message: "Error parsing JSON" }, status: :bad_request render json: { message: "Error parsing JSON" }, status: :bad_request
rescue GraphQL::ParseError rescue GraphQL::ParseError
render json: { message: "Query string is not valid JSON" }, status: :bad_request render json: { message: "Query string is not valid JSON" }, status: :bad_request
rescue ArgumentError => e rescue ArgumentError => e
render json: { message: e.message }, status: :bad_request render json: { message: e.message }, status: :bad_request
end
end end
private private

View File

@@ -25,23 +25,21 @@ class Legislation::Annotation < ApplicationRecord
end end
def store_context def store_context
begin html = draft_version.body_html
html = draft_version.body_html doc = Nokogiri::HTML(html)
doc = Nokogiri::HTML(html)
selector_start = "/html/body/#{range_start}" selector_start = "/html/body/#{range_start}"
el_start = doc.at_xpath(selector_start) el_start = doc.at_xpath(selector_start)
selector_end = "/html/body/#{range_end}" selector_end = "/html/body/#{range_end}"
el_end = doc.at_xpath(selector_end) el_end = doc.at_xpath(selector_end)
remainder_el_start = el_start.text[0..range_start_offset - 1] unless range_start_offset.zero? remainder_el_start = el_start.text[0..range_start_offset - 1] unless range_start_offset.zero?
remainder_el_end = el_end.text[range_end_offset..-1] remainder_el_end = el_end.text[range_end_offset..-1]
self.context = "#{remainder_el_start}<span class=annotator-hl>#{quote}</span>#{remainder_el_end}" self.context = "#{remainder_el_start}<span class=annotator-hl>#{quote}</span>#{remainder_el_end}"
rescue rescue
"<span class=annotator-hl>#{quote}</span>" "<span class=annotator-hl>#{quote}</span>"
end
end end
def create_first_comment def create_first_comment

View File

@@ -15,57 +15,55 @@ class MachineLearning
end end
def run def run
begin export_proposals_to_json
export_proposals_to_json export_budget_investments_to_json
export_budget_investments_to_json export_comments_to_json
export_comments_to_json
return unless run_machine_learning_scripts return unless run_machine_learning_scripts
if updated_file?(MachineLearning.proposals_taggings_filename) && if updated_file?(MachineLearning.proposals_taggings_filename) &&
updated_file?(MachineLearning.proposals_tags_filename) updated_file?(MachineLearning.proposals_tags_filename)
cleanup_proposals_tags! cleanup_proposals_tags!
import_ml_proposals_tags import_ml_proposals_tags
update_machine_learning_info_for("tags") update_machine_learning_info_for("tags")
end
if updated_file?(MachineLearning.investments_taggings_filename) &&
updated_file?(MachineLearning.investments_tags_filename)
cleanup_investments_tags!
import_ml_investments_tags
update_machine_learning_info_for("tags")
end
if updated_file?(MachineLearning.proposals_related_filename)
cleanup_proposals_related_content!
import_proposals_related_content
update_machine_learning_info_for("related_content")
end
if updated_file?(MachineLearning.investments_related_filename)
cleanup_investments_related_content!
import_budget_investments_related_content
update_machine_learning_info_for("related_content")
end
if updated_file?(MachineLearning.proposals_comments_summary_filename)
cleanup_proposals_comments_summary!
import_ml_proposals_comments_summary
update_machine_learning_info_for("comments_summary")
end
if updated_file?(MachineLearning.investments_comments_summary_filename)
cleanup_investments_comments_summary!
import_ml_investments_comments_summary
update_machine_learning_info_for("comments_summary")
end
job.update!(finished_at: Time.current)
Mailer.machine_learning_success(user).deliver_later
rescue Exception => e
handle_error(e)
raise e
end end
if updated_file?(MachineLearning.investments_taggings_filename) &&
updated_file?(MachineLearning.investments_tags_filename)
cleanup_investments_tags!
import_ml_investments_tags
update_machine_learning_info_for("tags")
end
if updated_file?(MachineLearning.proposals_related_filename)
cleanup_proposals_related_content!
import_proposals_related_content
update_machine_learning_info_for("related_content")
end
if updated_file?(MachineLearning.investments_related_filename)
cleanup_investments_related_content!
import_budget_investments_related_content
update_machine_learning_info_for("related_content")
end
if updated_file?(MachineLearning.proposals_comments_summary_filename)
cleanup_proposals_comments_summary!
import_ml_proposals_comments_summary
update_machine_learning_info_for("comments_summary")
end
if updated_file?(MachineLearning.investments_comments_summary_filename)
cleanup_investments_comments_summary!
import_ml_investments_comments_summary
update_machine_learning_info_for("comments_summary")
end
job.update!(finished_at: Time.current)
Mailer.machine_learning_success(user).deliver_later
rescue Exception => e
handle_error(e)
raise e
end end
handle_asynchronously :run, queue: "machine_learning" handle_asynchronously :run, queue: "machine_learning"

View File

@@ -84,18 +84,16 @@ end
task :install_ruby do task :install_ruby do
on roles(:app) do on roles(:app) do
within release_path do within release_path do
begin current_ruby = capture(:rvm, "current")
current_ruby = capture(:rvm, "current") rescue SSHKit::Command::Failed
rescue SSHKit::Command::Failed after "install_ruby", "rvm1:install:rvm"
after "install_ruby", "rvm1:install:ruby"
else
if current_ruby.include?("not installed")
after "install_ruby", "rvm1:install:rvm" after "install_ruby", "rvm1:install:rvm"
after "install_ruby", "rvm1:install:ruby" after "install_ruby", "rvm1:install:ruby"
else else
if current_ruby.include?("not installed") info "Ruby: Using #{current_ruby}"
after "install_ruby", "rvm1:install:rvm"
after "install_ruby", "rvm1:install:ruby"
else
info "Ruby: Using #{current_ruby}"
end
end end
end end
end end
@@ -104,19 +102,17 @@ end
task :install_node do task :install_node do
on roles(:app) do on roles(:app) do
with rails_env: fetch(:rails_env) do with rails_env: fetch(:rails_env) do
execute fetch(:fnm_install_node_command)
rescue SSHKit::Command::Failed
begin begin
execute fetch(:fnm_install_node_command) execute fetch(:fnm_setup_command)
rescue SSHKit::Command::Failed rescue SSHKit::Command::Failed
begin execute fetch(:fnm_install_command)
execute fetch(:fnm_setup_command) else
rescue SSHKit::Command::Failed execute fetch(:fnm_update_command)
execute fetch(:fnm_install_command)
else
execute fetch(:fnm_update_command)
end
execute fetch(:fnm_install_node_command)
end end
execute fetch(:fnm_install_node_command)
end end
end end
end end

View File

@@ -11,13 +11,11 @@ describe Officing::VotersController do
2.times.map do 2.times.map do
Thread.new do Thread.new do
begin post :create, params: {
post :create, params: { voter: { poll_id: poll.id, user_id: user.id },
voter: { poll_id: poll.id, user_id: user.id }, format: :js
format: :js }
} rescue ActionDispatch::IllegalStateError
rescue ActionDispatch::IllegalStateError
end
end end
end.each(&:join) end.each(&:join)

View File

@@ -8,14 +8,12 @@ describe Polls::AnswersController do
2.times.map do 2.times.map do
Thread.new do Thread.new do
begin post :create, params: {
post :create, params: { question_id: question.id,
question_id: question.id, option_id: question.question_options.find_by(title: "Answer A").id,
option_id: question.question_options.find_by(title: "Answer A").id, format: :js
format: :js }
} rescue ActionDispatch::IllegalStateError, ActiveRecord::RecordInvalid
rescue ActionDispatch::IllegalStateError, ActiveRecord::RecordInvalid
end
end end
end.each(&:join) end.each(&:join)

View File

@@ -187,10 +187,8 @@ describe Poll::Answer do
[answer, other_answer].map do |poll_answer| [answer, other_answer].map do |poll_answer|
Thread.new do Thread.new do
begin poll_answer.save_and_record_voter_participation
poll_answer.save_and_record_voter_participation rescue ActiveRecord::RecordInvalid
rescue ActiveRecord::RecordInvalid
end
end end
end.each(&:join) end.each(&:join)

View File

@@ -18,14 +18,12 @@ module GraphQLAPI
def extract_fields(response, collection_name, field_chain) def extract_fields(response, collection_name, field_chain)
fields = field_chain.split(".") fields = field_chain.split(".")
dig(response, "data.#{collection_name}.edges").map do |node| dig(response, "data.#{collection_name}.edges").map do |node|
begin if fields.size > 1
if fields.size > 1 node["node"][fields.first][fields.second]
node["node"][fields.first][fields.second] else
else node["node"][fields.first]
node["node"][fields.first]
end
rescue NoMethodError
end end
rescue NoMethodError
end.compact end.compact
end end
end end