Check phase dates and that draft version is not final before creating annotations
This commit is contained in:
@@ -20,15 +20,22 @@ class Legislation::AnnotationsController < ApplicationController
|
||||
end
|
||||
|
||||
def create
|
||||
@annotation = Legislation::Annotation.new(annotation_params)
|
||||
if !@process.open_phase?(:allegations) || @draft_version.final_version?
|
||||
render json: {}, status: :not_found and return
|
||||
end
|
||||
|
||||
@annotation = @draft_version.annotations.new(annotation_params)
|
||||
@annotation.author = current_user
|
||||
if @annotation.save
|
||||
track_event
|
||||
render json: @annotation.to_json
|
||||
else
|
||||
render json: {}, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def search
|
||||
@annotations = Legislation::Annotation.where(legislation_draft_version_id: params[:legislation_draft_version_id])
|
||||
@annotations = @draft_version.annotations
|
||||
annotations_hash = { total: @annotations.size, rows: @annotations }
|
||||
render json: annotations_hash.to_json
|
||||
end
|
||||
@@ -43,7 +50,12 @@ class Legislation::AnnotationsController < ApplicationController
|
||||
params
|
||||
.require(:annotation)
|
||||
.permit(:quote, :text, ranges: [:start, :startOffset, :end, :endOffset])
|
||||
.merge(legislation_draft_version_id: params[:legislation_draft_version_id])
|
||||
end
|
||||
|
||||
def track_event
|
||||
ahoy.track "legislation_annotation_created".to_sym,
|
||||
"legislation_annotation_id": @annotation.id,
|
||||
"legislation_draft_version_id": @draft_version.id
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -19,7 +19,7 @@ class Legislation::AnswersController < Legislation::BaseController
|
||||
end
|
||||
else
|
||||
respond_to do |format|
|
||||
format.js
|
||||
format.js { render json: {} , status: :not_found }
|
||||
format.html { redirect_to legislation_process_question_path(@process, @question), alert: t('legislation.questions.participation.phase_not_open') }
|
||||
end
|
||||
end
|
||||
|
||||
70
spec/controllers/legislation/annotations_controller_spec.rb
Normal file
70
spec/controllers/legislation/annotations_controller_spec.rb
Normal file
@@ -0,0 +1,70 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe Legislation::AnnotationsController do
|
||||
|
||||
describe 'POST create' do
|
||||
before(:each) do
|
||||
@process = create(:legislation_process, allegations_start_date: Date.current - 3.day, 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
|
||||
|
||||
it 'should create an ahoy event' do
|
||||
sign_in @user
|
||||
|
||||
post :create, process_id: @process.id,
|
||||
draft_version_id: @draft_version.id,
|
||||
annotation: {
|
||||
"quote"=>"Ordenación Territorial",
|
||||
"ranges"=>[{"start"=>"/p[1]", "startOffset"=>1, "end"=>"/p[1]", "endOffset"=>3}],
|
||||
"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 'should not create an annotation if the draft version is a final version' do
|
||||
sign_in @user
|
||||
|
||||
post :create, process_id: @process.id,
|
||||
draft_version_id: @final_version.id,
|
||||
annotation: {
|
||||
"quote"=>"Ordenación Territorial",
|
||||
"ranges"=>[{"start"=>"/p[1]", "startOffset"=>1, "end"=>"/p[1]", "endOffset"=>3}],
|
||||
"text": "una anotacion"
|
||||
}
|
||||
|
||||
expect(response).to have_http_status(:not_found)
|
||||
end
|
||||
|
||||
it 'should create an annotation if the process allegations phase is open' do
|
||||
sign_in @user
|
||||
|
||||
expect do
|
||||
xhr :post, :create, process_id: @process.id,
|
||||
draft_version_id: @draft_version.id,
|
||||
annotation: {
|
||||
"quote"=>"Ordenación Territorial",
|
||||
"ranges"=>[{"start"=>"/p[1]", "startOffset"=>1, "end"=>"/p[1]", "endOffset"=>3}],
|
||||
"text": "una anotacion"
|
||||
}
|
||||
end.to change { @draft_version.annotations.count }.by(1)
|
||||
end
|
||||
|
||||
it 'should 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)
|
||||
|
||||
expect do
|
||||
xhr :post, :create, process_id: @process.id,
|
||||
draft_version_id: @draft_version.id,
|
||||
annotation: {
|
||||
"quote"=>"Ordenación Territorial",
|
||||
"ranges"=>[{"start"=>"/p[1]", "startOffset"=>1, "end"=>"/p[1]", "endOffset"=>3}],
|
||||
"text": "una anotacion"
|
||||
}
|
||||
end.to_not change { @draft_version.annotations.count }
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -4,17 +4,12 @@ describe Legislation::AnswersController do
|
||||
|
||||
describe 'POST create' do
|
||||
before(:each) do
|
||||
InvisibleCaptcha.timestamp_enabled = false
|
||||
@process = create(:legislation_process, debate_start_date: Date.current - 3.day, 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
|
||||
|
||||
after(:each) do
|
||||
InvisibleCaptcha.timestamp_enabled = true
|
||||
end
|
||||
|
||||
it 'should create an ahoy event' do
|
||||
sign_in @user
|
||||
|
||||
|
||||
@@ -499,6 +499,10 @@ FactoryGirl.define do
|
||||
trait :published do
|
||||
status "published"
|
||||
end
|
||||
|
||||
trait :final_version do
|
||||
final_version true
|
||||
end
|
||||
end
|
||||
|
||||
factory :legislation_annotation, class: 'Legislation::Annotation' do
|
||||
|
||||
Reference in New Issue
Block a user