diff --git a/.rubocop.yml b/.rubocop.yml index 12c9957b4..bd287aa83 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -781,6 +781,9 @@ Style/RaiseArgs: Style/RedundantArgument: Enabled: true +Style/RedundantBegin: + Enabled: true + Style/RedundantCondition: Enabled: true diff --git a/app/controllers/graphql_controller.rb b/app/controllers/graphql_controller.rb index 0c64ddfb9..0a13ac349 100644 --- a/app/controllers/graphql_controller.rb +++ b/app/controllers/graphql_controller.rb @@ -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 diff --git a/app/models/legislation/annotation.rb b/app/models/legislation/annotation.rb index c9fb4d046..b1417a886 100644 --- a/app/models/legislation/annotation.rb +++ b/app/models/legislation/annotation.rb @@ -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}#{quote}#{remainder_el_end}" - rescue - "#{quote}" - end + self.context = "#{remainder_el_start}#{quote}#{remainder_el_end}" + rescue + "#{quote}" end def create_first_comment diff --git a/app/models/machine_learning.rb b/app/models/machine_learning.rb index 5541d3327..34327bd0d 100644 --- a/app/models/machine_learning.rb +++ b/app/models/machine_learning.rb @@ -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" diff --git a/config/deploy.rb b/config/deploy.rb index 265dcd95a..84d1e6fcc 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -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 diff --git a/spec/controllers/officing/voters_controller_spec.rb b/spec/controllers/officing/voters_controller_spec.rb index 81c9c6120..e1a611755 100644 --- a/spec/controllers/officing/voters_controller_spec.rb +++ b/spec/controllers/officing/voters_controller_spec.rb @@ -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) diff --git a/spec/controllers/polls/answers_controller_spec.rb b/spec/controllers/polls/answers_controller_spec.rb index 08f129c0b..171fc1cc8 100644 --- a/spec/controllers/polls/answers_controller_spec.rb +++ b/spec/controllers/polls/answers_controller_spec.rb @@ -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) diff --git a/spec/models/poll/answer_spec.rb b/spec/models/poll/answer_spec.rb index b06ccbcd7..4f3ca154e 100644 --- a/spec/models/poll/answer_spec.rb +++ b/spec/models/poll/answer_spec.rb @@ -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) diff --git a/spec/support/common_actions/graphql_api.rb b/spec/support/common_actions/graphql_api.rb index 6deceb138..452433f87 100644 --- a/spec/support/common_actions/graphql_api.rb +++ b/spec/support/common_actions/graphql_api.rb @@ -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