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:
Enabled: true
Style/RedundantBegin:
Enabled: true
Style/RedundantCondition:
Enabled: true

View File

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

View File

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

View File

@@ -15,57 +15,55 @@ class MachineLearning
end
def run
begin
export_proposals_to_json
export_budget_investments_to_json
export_comments_to_json
export_proposals_to_json
export_budget_investments_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) &&
updated_file?(MachineLearning.proposals_tags_filename)
cleanup_proposals_tags!
import_ml_proposals_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
if updated_file?(MachineLearning.proposals_taggings_filename) &&
updated_file?(MachineLearning.proposals_tags_filename)
cleanup_proposals_tags!
import_ml_proposals_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
handle_asynchronously :run, queue: "machine_learning"

View File

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

View File

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

View File

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

View File

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

View File

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