Fix several rubocop warnings

Metrics/LineLength: Line is too long.
RSpec/InstanceVariable: Use let instead of an instance variable.
Layout/TrailingBlankLines: Final newline missing.
Style/StringLiterals: Prefer double-quoted strings.
This commit is contained in:
Julian Herrero
2019-03-30 19:02:37 +01:00
parent 83e129d5b7
commit 6e88031537
14 changed files with 230 additions and 114 deletions

View File

@@ -40,7 +40,7 @@ gem "paperclip", "~> 5.2.1"
gem "paranoia", "~> 2.4.1" gem "paranoia", "~> 2.4.1"
gem "pg", "~> 0.21.0" gem "pg", "~> 0.21.0"
gem "pg_search", "~> 2.0.1" gem "pg_search", "~> 2.0.1"
gem 'record_tag_helper', '~> 1.0' gem "record_tag_helper", "~> 1.0"
gem "redcarpet", "~> 3.4.0" gem "redcarpet", "~> 3.4.0"
gem "responders", "~> 2.4.0" gem "responders", "~> 2.4.0"
gem "rinku", "~> 2.0.2", require: "rails_rinku" gem "rinku", "~> 2.0.2", require: "rails_rinku"

View File

@@ -27,7 +27,8 @@ module Sanitizable
end end
def translatable_description? def translatable_description?
self.class.included_modules.include?(Globalizable) && self.class.translated_attribute_names.include?(:description) self.class.included_modules.include?(Globalizable) &&
self.class.translated_attribute_names.include?(:description)
end end
def sanitize_description_translations def sanitize_description_translations

View File

@@ -89,9 +89,11 @@ class Document < ApplicationRecord
def validate_attachment_content_type def validate_attachment_content_type
if documentable_class && if documentable_class &&
!accepted_content_types(documentable_class).include?(attachment_content_type) !accepted_content_types(documentable_class).include?(attachment_content_type)
errors.add(:attachment, I18n.t("documents.errors.messages.wrong_content_type", accepted_content_types = documentable_humanized_accepted_content_types(documentable_class)
message = I18n.t("documents.errors.messages.wrong_content_type",
content_type: attachment_content_type, content_type: attachment_content_type,
accepted_content_types: documentable_humanized_accepted_content_types(documentable_class))) accepted_content_types: accepted_content_types)
errors.add(:attachment, message)
end end
end end

View File

@@ -87,9 +87,10 @@ class Image < ApplicationRecord
def validate_attachment_content_type def validate_attachment_content_type
if imageable_class && !attachment_of_valid_content_type? if imageable_class && !attachment_of_valid_content_type?
errors.add(:attachment, I18n.t("images.errors.messages.wrong_content_type", message = I18n.t("images.errors.messages.wrong_content_type",
content_type: attachment_content_type, content_type: attachment_content_type,
accepted_content_types: imageable_humanized_accepted_content_types)) accepted_content_types: imageable_humanized_accepted_content_types)
errors.add(:attachment, message)
end end
end end

View File

@@ -128,7 +128,9 @@ class User < ApplicationRecord
end end
def headings_voted_within_group(group) def headings_voted_within_group(group)
Budget::Heading.joins(:translations).order("name").where(id: voted_investments.by_group(group).pluck(:heading_id)) Budget::Heading.joins(:translations)
.order("name")
.where(id: voted_investments.by_group(group).pluck(:heading_id))
end end
def voted_investments def voted_investments

View File

@@ -13,7 +13,7 @@ Rails.application.configure do
config.consider_all_requests_local = true config.consider_all_requests_local = true
# Enable/disable caching. By default caching is disabled. # Enable/disable caching. By default caching is disabled.
if Rails.root.join('tmp/caching-dev.txt').exist? if Rails.root.join("tmp/caching-dev.txt").exist?
config.action_controller.perform_caching = true config.action_controller.perform_caching = true
config.cache_store = :memory_store config.cache_store = :memory_store

View File

@@ -74,7 +74,10 @@ module ActsAsTaggableOn
end end
def self.spending_proposal_tags def self.spending_proposal_tags
ActsAsTaggableOn::Tag.where("taggings.taggable_type" => "SpendingProposal").includes(:taggings).order(:name).distinct ActsAsTaggableOn::Tag.where("taggings.taggable_type" => "SpendingProposal")
.includes(:taggings)
.order(:name)
.distinct
end end
def self.graphql_field_name def self.graphql_field_name

View File

@@ -3,36 +3,57 @@ require "rails_helper"
describe CommentsController do describe CommentsController do
describe "POST create" do describe "POST create" do
before do
@process = create(:legislation_process, debate_start_date: Date.current - 3.days, debate_end_date: Date.current + 2.days) let(:legal_process) { create(:legislation_process, debate_start_date: Date.current - 3.days,
@question = create(:legislation_question, process: @process, title: "Question 1") debate_end_date: Date.current + 2.days) }
@user = create(:user, :level_two) let(:question) { create(:legislation_question, process: legal_process, title: "Question 1") }
@unverified_user = create(:user) let(:user) { create(:user, :level_two) }
end let(:unverified_user) { create(:user) }
it "creates an comment if the comments are open" do it "creates an comment if the comments are open" do
sign_in @user sign_in user
expect do expect do
post :create, params: {comment: {commentable_id: @question.id, commentable_type: "Legislation::Question", body: "a comment"}}, xhr: true post :create, xhr: true,
end.to change { @question.reload.comments_count }.by(1) params: {
comment: {
commentable_id: question.id,
commentable_type: "Legislation::Question",
body: "a comment"
}
}
end.to change { question.reload.comments_count }.by(1)
end end
it "does not create a comment if the comments are closed" do it "does not create a comment if the comments are closed" do
sign_in @user sign_in user
@process.update_attribute(:debate_end_date, Date.current - 1.day) legal_process.update_attribute(:debate_end_date, Date.current - 1.day)
expect do expect do
post :create, params: {comment: {commentable_id: @question.id, commentable_type: "Legislation::Question", body: "a comment"}}, xhr: true post :create, xhr: true,
end.not_to change { @question.reload.comments_count } params: {
comment: {
commentable_id: question.id,
commentable_type: "Legislation::Question",
body: "a comment"
}
}
end.not_to change { question.reload.comments_count }
end end
it "does not create a comment for unverified users when the commentable requires it" do it "does not create a comment for unverified users when the commentable requires it" do
sign_in @unverified_user sign_in unverified_user
expect do expect do
post :create, params: {comment: {commentable_id: @question.id, commentable_type: "Legislation::Question", body: "a comment"}}, xhr: true post :create, xhr: true,
end.not_to change { @question.reload.comments_count } params: {
comment: {
commentable_id: question.id,
commentable_type: "Legislation::Question",
body: "a comment"
}
}
end.not_to change { question.reload.comments_count }
end end
end end
end end

View File

@@ -15,7 +15,13 @@ describe DebatesController do
sign_in create(:user) sign_in create(:user)
post :create, params: { debate: { title: "A sample debate", description: "this is a sample debate", terms_of_service: 1 }} post :create, params: {
debate: {
title: "A sample debate",
description: "this is a sample debate",
terms_of_service: 1
}
}
expect(Ahoy::Event.where(name: :debate_created).count).to eq 1 expect(Ahoy::Event.where(name: :debate_created).count).to eq 1
expect(Ahoy::Event.last.properties["debate_id"]).to eq Debate.last.id expect(Ahoy::Event.last.properties["debate_id"]).to eq Debate.last.id
end end

View File

@@ -38,7 +38,8 @@ describe GraphqlController, type: :request do
let(:json_headers) { { "CONTENT_TYPE" => "application/json" } } let(:json_headers) { { "CONTENT_TYPE" => "application/json" } }
specify "with json-encoded query string inside body" do specify "with json-encoded query string inside body" do
post "/graphql", params: { query: "{ proposal(id: #{proposal.id}) { title } }" }.to_json, headers: json_headers post "/graphql", params: { query: "{ proposal(id: #{proposal.id}) { title } }" }.to_json,
headers: json_headers
expect(response).to have_http_status(:ok) expect(response).to have_http_status(:ok)
expect(JSON.parse(response.body)["data"]["proposal"]["title"]).to eq(proposal.title) expect(JSON.parse(response.body)["data"]["proposal"]["title"]).to eq(proposal.title)
@@ -46,7 +47,8 @@ describe GraphqlController, type: :request do
specify "with raw query string inside body" do specify "with raw query string inside body" do
graphql_headers = { "CONTENT_TYPE" => "application/graphql" } graphql_headers = { "CONTENT_TYPE" => "application/graphql" }
post "/graphql", params: "{ proposal(id: #{proposal.id}) { title } }", headers: graphql_headers post "/graphql", params: "{ proposal(id: #{proposal.id}) { title } }",
headers: graphql_headers
expect(response).to have_http_status(:ok) expect(response).to have_http_status(:ok)
expect(JSON.parse(response.body)["data"]["proposal"]["title"]).to eq(proposal.title) expect(JSON.parse(response.body)["data"]["proposal"]["title"]).to eq(proposal.title)

View File

@@ -3,109 +3,161 @@ require "rails_helper"
describe Legislation::AnnotationsController do describe Legislation::AnnotationsController do
describe "POST create" do describe "POST create" do
before do
@process = create(:legislation_process, allegations_start_date: Date.current - 3.days, allegations_end_date: Date.current + 2.days) let(:legal_process) { create(:legislation_process,
@draft_version = create(:legislation_draft_version, :published, process: @process, title: "Version 1") allegations_start_date: Date.current - 3.days,
@final_version = create(:legislation_draft_version, :published, :final_version, process: @process, title: "Final version") allegations_end_date: Date.current + 2.days) }
@user = create(:user, :level_two) let(:draft_version) { create(:legislation_draft_version, :published,
end process: legal_process,
title: "Version 1") }
let(:final_version) { create(:legislation_draft_version, :published,
:final_version,
process: legal_process,
title: "Final version") }
let(:user) { create(:user, :level_two) }
it "creates an ahoy event" do it "creates an ahoy event" do
sign_in @user sign_in user
post :create, params: { post :create, params: {
process_id: @process.id, process_id: legal_process.id,
draft_version_id: @draft_version.id, draft_version_id: draft_version.id,
legislation_annotation: { legislation_annotation: {
"quote"=>"ipsum", "quote" => "ipsum",
"ranges"=>[{"start"=>"/p[1]", "startOffset"=>6, "end"=>"/p[1]", "endOffset"=>11}], "ranges" => [{
"text": "una anotacion" "start" => "/p[1]",
}} "startOffset" => 6,
"end" => "/p[1]",
"endOffset" => 11
}],
"text" => "una anotacion"
}
}
expect(Ahoy::Event.where(name: :legislation_annotation_created).count).to eq 1 expect(Ahoy::Event.where(name: :legislation_annotation_created).count).to eq 1
expect(Ahoy::Event.last.properties["legislation_annotation_id"]).to eq Legislation::Annotation.last.id expect(Ahoy::Event.last.properties["legislation_annotation_id"]).to eq Legislation::Annotation.last.id
end end
it "does not create an annotation if the draft version is a final version" do it "does not create an annotation if the draft version is a final version" do
sign_in @user sign_in user
post :create, params: { post :create, params: {
process_id: @process.id, process_id: legal_process.id,
draft_version_id: @final_version.id, draft_version_id: final_version.id,
legislation_annotation: { legislation_annotation: {
"quote" => "ipsum", "quote" => "ipsum",
"ranges" => [{"start" => "/p[1]", "startOffset" => 6, "end" => "/p[1]", "endOffset" => 11}], "ranges" => [{
"text": "una anotacion" "start" => "/p[1]",
}} "startOffset" => 6,
"end" => "/p[1]",
"endOffset" => 11
}],
"text" => "una anotacion"
}
}
expect(response).to have_http_status(:not_found) expect(response).to have_http_status(:not_found)
end end
it "creates an annotation if the process allegations phase is open" do it "creates an annotation if the process allegations phase is open" do
sign_in @user sign_in user
expect do expect do
post :create, params: { post :create, xhr: true,
process_id: @process.id, params: {
draft_version_id: @draft_version.id, process_id: legal_process.id,
draft_version_id: draft_version.id,
legislation_annotation: { legislation_annotation: {
"quote"=>"ipsum", "quote" => "ipsum",
"ranges"=>[{"start"=>"/p[1]", "startOffset"=>6, "end"=>"/p[1]", "endOffset"=>11}], "ranges" => [{
"text": "una anotacion" "start" => "/p[1]",
}}, "startOffset" => 6,
xhr: true "end" => "/p[1]",
end.to change { @draft_version.annotations.count }.by(1) "endOffset" => 11
}],
"text" => "una anotacion"
}
}
end.to change { draft_version.annotations.count }.by(1)
end end
it "does not create an annotation if the process allegations phase is not open" do it "does not create an annotation if the process allegations phase is not open" do
sign_in @user sign_in user
@process.update_attribute(:allegations_end_date, Date.current - 1.day) legal_process.update_attribute(:allegations_end_date, Date.current - 1.day)
expect do expect do
post :create, params: { post :create, xhr: true,
process_id: @process.id, params: {
draft_version_id: @draft_version.id, process_id: legal_process.id,
draft_version_id: draft_version.id,
legislation_annotation: { legislation_annotation: {
"quote"=>"ipsum", "quote" => "ipsum",
"ranges"=>[{"start"=>"/p[1]", "startOffset"=>6, "end"=>"/p[1]", "endOffset"=>11}], "ranges"=> [{
"text": "una anotacion" "start" => "/p[1]",
}}, "startOffset" => 6,
xhr: true "end" => "/p[1]",
end.to_not change { @draft_version.annotations.count } "endOffset" => 11
}],
"text" => "una anotacion"
}
}
end.to_not change { draft_version.annotations.count }
end end
it "creates an annotation by parsing parameters in JSON" do it "creates an annotation by parsing parameters in JSON" do
sign_in @user sign_in user
expect do expect do
post :create, params: { post :create, xhr: true,
process_id: @process.id, params: {
draft_version_id: @draft_version.id, process_id: legal_process.id,
draft_version_id: draft_version.id,
legislation_annotation: { legislation_annotation: {
"quote"=>"ipsum", "quote" => "ipsum",
"ranges"=>[{"start"=>"/p[1]", "startOffset"=>6, "end"=>"/p[1]", "endOffset"=>11}].to_json, "ranges" => [{
"text": "una anotacion" "start" => "/p[1]",
}}, "startOffset" => 6,
xhr: true "end" => "/p[1]",
end.to change { @draft_version.annotations.count }.by(1) "endOffset" => 11
}].to_json,
"text" => "una anotacion"
}
}
end.to change { draft_version.annotations.count }.by(1)
end end
it "creates a new comment on an existing annotation when range is the same" do it "creates a new comment on an existing annotation when range is the same" do
annotation = create(:legislation_annotation, draft_version: @draft_version, text: "my annotation", annotation = create(:legislation_annotation, draft_version: draft_version,
ranges: [{"start" => "/p[1]", "startOffset" => 6, "end" => "/p[1]", "endOffset" => 11}], text: "my annotation",
range_start: "/p[1]", range_start_offset: 6, range_end: "/p[1]", range_end_offset: 11) ranges: [{
sign_in @user "start" => "/p[1]",
"startOffset" => 6,
"end" => "/p[1]",
"endOffset" => 11
}],
range_start: "/p[1]",
range_start_offset: 6,
range_end: "/p[1]",
range_end_offset: 11)
sign_in user
expect do expect do
post :create, params: { post :create, xhr: true,
process_id: @process.id, params: {
draft_version_id: @draft_version.id, process_id: legal_process.id,
draft_version_id: draft_version.id,
legislation_annotation: { legislation_annotation: {
"quote"=>"ipsum", "quote" => "ipsum",
"ranges"=>[{"start"=>"/p[1]", "startOffset"=>6, "end"=>"/p[1]", "endOffset"=>11}], "ranges" => [{
"text": "una anotacion" "start" => "/p[1]",
}}, "startOffset" => 6,
xhr: true "end" => "/p[1]",
end.to_not change { @draft_version.annotations.count } "endOffset" => 11
}],
"text" => "una anotacion"
}
}
end.to_not change { draft_version.annotations.count }
expect(annotation.reload.comments_count).to eq(2) expect(annotation.reload.comments_count).to eq(2)
expect(annotation.comments.last.body).to eq("una anotacion") expect(annotation.comments.last.body).to eq("una anotacion")

View File

@@ -3,36 +3,56 @@ require "rails_helper"
describe Legislation::AnswersController do describe Legislation::AnswersController do
describe "POST create" do describe "POST create" do
before do
@process = create(:legislation_process, debate_start_date: Date.current - 3.days, debate_end_date: Date.current + 2.days) let(:legal_process) { create(:legislation_process, debate_start_date: Date.current - 3.days,
@question = create(:legislation_question, process: @process, title: "Question 1") debate_end_date: Date.current + 2.days) }
@question_option = create(:legislation_question_option, question: @question, value: "Yes") let(:question) { create(:legislation_question, process: legal_process, title: "Question 1") }
@user = create(:user, :level_two) let(:question_option) { create(:legislation_question_option, question: question, value: "Yes") }
end let(:user) { create(:user, :level_two) }
it "creates an ahoy event" do it "creates an ahoy event" do
sign_in @user sign_in user
post :create, params: {process_id: @process.id, question_id: @question.id, legislation_answer: { legislation_question_option_id: @question_option.id }} post :create, params: {
process_id: legal_process.id,
question_id: question.id,
legislation_answer: {
legislation_question_option_id: question_option.id
}
}
expect(Ahoy::Event.where(name: :legislation_answer_created).count).to eq 1 expect(Ahoy::Event.where(name: :legislation_answer_created).count).to eq 1
expect(Ahoy::Event.last.properties["legislation_answer_id"]).to eq Legislation::Answer.last.id expect(Ahoy::Event.last.properties["legislation_answer_id"]).to eq Legislation::Answer.last.id
end end
it "creates an answer if the process debate phase is open" do it "creates an answer if the process debate phase is open" do
sign_in @user sign_in user
expect do expect do
post :create, params: {process_id: @process.id, question_id: @question.id, legislation_answer: { legislation_question_option_id: @question_option.id }}, xhr: true post :create, xhr: true,
end.to change { @question.reload.answers_count }.by(1) params: {
process_id: legal_process.id,
question_id: question.id,
legislation_answer: {
legislation_question_option_id: question_option.id
}
}
end.to change { question.reload.answers_count }.by(1)
end end
it "does not create an answer if the process debate phase is not open" do it "does not create an answer if the process debate phase is not open" do
sign_in @user sign_in user
@process.update_attribute(:debate_end_date, Date.current - 1.day) legal_process.update_attribute(:debate_end_date, Date.current - 1.day)
expect do expect do
post :create, params: {process_id: @process.id, question_id: @question.id, legislation_answer: { legislation_question_option_id: @question_option.id }}, xhr: true post :create, xhr: true,
end.not_to change { @question.reload.answers_count } params: {
process_id: legal_process.id,
question_id: question.id,
legislation_answer: {
legislation_question_option_id: question_option.id
}
}
end.not_to change { question.reload.answers_count }
end end
end end
end end

View File

@@ -5,7 +5,9 @@ describe Management::SessionsController do
describe "Sign in" do describe "Sign in" do
it "denies access if wrong manager credentials" do it "denies access if wrong manager credentials" do
allow_any_instance_of(ManagerAuthenticator).to receive(:auth).and_return(false) allow_any_instance_of(ManagerAuthenticator).to receive(:auth).and_return(false)
expect { get :create, params: { login: "nonexistent" , clave_usuario: "wrong" }}.to raise_error CanCan::AccessDenied expect {
get :create, params: { login: "nonexistent", clave_usuario: "wrong" }
}.to raise_error CanCan::AccessDenied
expect(session[:manager]).to be_nil expect(session[:manager]).to be_nil
end end
@@ -13,7 +15,11 @@ describe Management::SessionsController do
manager = {login: "JJB033", user_key: "31415926", date: "20151031135905"} manager = {login: "JJB033", user_key: "31415926", date: "20151031135905"}
allow_any_instance_of(ManagerAuthenticator).to receive(:auth).and_return(manager) allow_any_instance_of(ManagerAuthenticator).to receive(:auth).and_return(manager)
get :create, params: { login: "JJB033" , clave_usuario: "31415926", fecha_conexion: "20151031135905" } get :create, params: {
login: "JJB033" ,
clave_usuario: "31415926",
fecha_conexion: "20151031135905"
}
expect(response).to be_redirect expect(response).to be_redirect
expect(session[:manager][:login]).to eq "JJB033" expect(session[:manager][:login]).to eq "JJB033"
end end