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 "pg", "~> 0.21.0"
gem "pg_search", "~> 2.0.1"
gem 'record_tag_helper', '~> 1.0'
gem "record_tag_helper", "~> 1.0"
gem "redcarpet", "~> 3.4.0"
gem "responders", "~> 2.4.0"
gem "rinku", "~> 2.0.2", require: "rails_rinku"

View File

@@ -27,7 +27,8 @@ module Sanitizable
end
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
def sanitize_description_translations

View File

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

View File

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

View File

@@ -128,7 +128,9 @@ class User < ApplicationRecord
end
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
def voted_investments

View File

@@ -13,7 +13,7 @@ Rails.application.configure do
config.consider_all_requests_local = true
# 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.cache_store = :memory_store

View File

@@ -74,7 +74,10 @@ module ActsAsTaggableOn
end
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
def self.graphql_field_name

View File

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

View File

@@ -15,7 +15,13 @@ describe DebatesController do
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.last.properties["debate_id"]).to eq Debate.last.id
end

View File

@@ -38,7 +38,8 @@ describe GraphqlController, type: :request do
let(:json_headers) { { "CONTENT_TYPE" => "application/json" } }
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(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
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(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 "POST create" do
before do
@process = create(:legislation_process, allegations_start_date: Date.current - 3.days, allegations_end_date: Date.current + 2.days)
@draft_version = create(:legislation_draft_version, :published, process: @process, title: "Version 1")
@final_version = create(:legislation_draft_version, :published, :final_version, process: @process, title: "Final version")
@user = create(:user, :level_two)
end
let(:legal_process) { create(:legislation_process,
allegations_start_date: Date.current - 3.days,
allegations_end_date: Date.current + 2.days) }
let(:draft_version) { create(:legislation_draft_version, :published,
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
sign_in @user
sign_in user
post :create, params: {
process_id: @process.id,
draft_version_id: @draft_version.id,
process_id: legal_process.id,
draft_version_id: draft_version.id,
legislation_annotation: {
"quote"=>"ipsum",
"ranges"=>[{"start"=>"/p[1]", "startOffset"=>6, "end"=>"/p[1]", "endOffset"=>11}],
"text": "una anotacion"
}}
"quote" => "ipsum",
"ranges" => [{
"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.last.properties["legislation_annotation_id"]).to eq Legislation::Annotation.last.id
end
it "does not create an annotation if the draft version is a final version" do
sign_in @user
sign_in user
post :create, params: {
process_id: @process.id,
draft_version_id: @final_version.id,
legislation_annotation: {
"quote" => "ipsum",
"ranges" => [{"start" => "/p[1]", "startOffset" => 6, "end" => "/p[1]", "endOffset" => 11}],
"text": "una anotacion"
}}
process_id: legal_process.id,
draft_version_id: final_version.id,
legislation_annotation: {
"quote" => "ipsum",
"ranges" => [{
"start" => "/p[1]",
"startOffset" => 6,
"end" => "/p[1]",
"endOffset" => 11
}],
"text" => "una anotacion"
}
}
expect(response).to have_http_status(:not_found)
end
it "creates an annotation if the process allegations phase is open" do
sign_in @user
sign_in user
expect do
post :create, params: {
process_id: @process.id,
draft_version_id: @draft_version.id,
post :create, xhr: true,
params: {
process_id: legal_process.id,
draft_version_id: draft_version.id,
legislation_annotation: {
"quote"=>"ipsum",
"ranges"=>[{"start"=>"/p[1]", "startOffset"=>6, "end"=>"/p[1]", "endOffset"=>11}],
"text": "una anotacion"
}},
xhr: true
end.to change { @draft_version.annotations.count }.by(1)
"quote" => "ipsum",
"ranges" => [{
"start" => "/p[1]",
"startOffset" => 6,
"end" => "/p[1]",
"endOffset" => 11
}],
"text" => "una anotacion"
}
}
end.to change { draft_version.annotations.count }.by(1)
end
it "does not create an annotation if the process allegations phase is not open" do
sign_in @user
@process.update_attribute(:allegations_end_date, Date.current - 1.day)
sign_in user
legal_process.update_attribute(:allegations_end_date, Date.current - 1.day)
expect do
post :create, params: {
process_id: @process.id,
draft_version_id: @draft_version.id,
post :create, xhr: true,
params: {
process_id: legal_process.id,
draft_version_id: draft_version.id,
legislation_annotation: {
"quote"=>"ipsum",
"ranges"=>[{"start"=>"/p[1]", "startOffset"=>6, "end"=>"/p[1]", "endOffset"=>11}],
"text": "una anotacion"
}},
xhr: true
end.to_not change { @draft_version.annotations.count }
"quote" => "ipsum",
"ranges"=> [{
"start" => "/p[1]",
"startOffset" => 6,
"end" => "/p[1]",
"endOffset" => 11
}],
"text" => "una anotacion"
}
}
end.to_not change { draft_version.annotations.count }
end
it "creates an annotation by parsing parameters in JSON" do
sign_in @user
sign_in user
expect do
post :create, params: {
process_id: @process.id,
draft_version_id: @draft_version.id,
post :create, xhr: true,
params: {
process_id: legal_process.id,
draft_version_id: draft_version.id,
legislation_annotation: {
"quote"=>"ipsum",
"ranges"=>[{"start"=>"/p[1]", "startOffset"=>6, "end"=>"/p[1]", "endOffset"=>11}].to_json,
"text": "una anotacion"
}},
xhr: true
end.to change { @draft_version.annotations.count }.by(1)
"quote" => "ipsum",
"ranges" => [{
"start" => "/p[1]",
"startOffset" => 6,
"end" => "/p[1]",
"endOffset" => 11
}].to_json,
"text" => "una anotacion"
}
}
end.to change { draft_version.annotations.count }.by(1)
end
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",
ranges: [{"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
annotation = create(:legislation_annotation, draft_version: draft_version,
text: "my annotation",
ranges: [{
"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
post :create, params: {
process_id: @process.id,
draft_version_id: @draft_version.id,
post :create, xhr: true,
params: {
process_id: legal_process.id,
draft_version_id: draft_version.id,
legislation_annotation: {
"quote"=>"ipsum",
"ranges"=>[{"start"=>"/p[1]", "startOffset"=>6, "end"=>"/p[1]", "endOffset"=>11}],
"text": "una anotacion"
}},
xhr: true
end.to_not change { @draft_version.annotations.count }
"quote" => "ipsum",
"ranges" => [{
"start" => "/p[1]",
"startOffset" => 6,
"end" => "/p[1]",
"endOffset" => 11
}],
"text" => "una anotacion"
}
}
end.to_not change { draft_version.annotations.count }
expect(annotation.reload.comments_count).to eq(2)
expect(annotation.comments.last.body).to eq("una anotacion")

View File

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

View File

@@ -5,7 +5,9 @@ describe Management::SessionsController do
describe "Sign in" do
it "denies access if wrong manager credentials" do
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
end
@@ -13,7 +15,11 @@ describe Management::SessionsController do
manager = {login: "JJB033", user_key: "31415926", date: "20151031135905"}
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(session[:manager][:login]).to eq "JJB033"
end