diff --git a/app/controllers/legislation/annotations_controller.rb b/app/controllers/legislation/annotations_controller.rb index 664cd6252..6f5069763 100644 --- a/app/controllers/legislation/annotations_controller.rb +++ b/app/controllers/legislation/annotations_controller.rb @@ -66,7 +66,7 @@ class Legislation::AnnotationsController < ApplicationController end def convert_ranges_parameters - if params[:legislation_annotation] && params[:legislation_annotation][:ranges] + if params[:legislation_annotation] && params[:legislation_annotation][:ranges] && params[:legislation_annotation][:ranges].is_a?(String) params[:legislation_annotation][:ranges] = JSON.parse(params[:legislation_annotation][:ranges]) end rescue JSON::ParserError diff --git a/spec/controllers/legislation/annotations_controller_spec.rb b/spec/controllers/legislation/annotations_controller_spec.rb index 2713fc273..4ffd91357 100644 --- a/spec/controllers/legislation/annotations_controller_spec.rb +++ b/spec/controllers/legislation/annotations_controller_spec.rb @@ -15,7 +15,7 @@ describe Legislation::AnnotationsController do post :create, process_id: @process.id, draft_version_id: @draft_version.id, - annotation: { + legislation_annotation: { "quote"=>"Ordenación Territorial", "ranges"=>[{"start"=>"/p[1]", "startOffset"=>1, "end"=>"/p[1]", "endOffset"=>3}], "text": "una anotacion" @@ -29,7 +29,7 @@ describe Legislation::AnnotationsController do post :create, process_id: @process.id, draft_version_id: @final_version.id, - annotation: { + legislation_annotation: { "quote"=>"Ordenación Territorial", "ranges"=>[{"start"=>"/p[1]", "startOffset"=>1, "end"=>"/p[1]", "endOffset"=>3}], "text": "una anotacion" @@ -44,7 +44,7 @@ describe Legislation::AnnotationsController do expect do xhr :post, :create, process_id: @process.id, draft_version_id: @draft_version.id, - annotation: { + legislation_annotation: { "quote"=>"Ordenación Territorial", "ranges"=>[{"start"=>"/p[1]", "startOffset"=>1, "end"=>"/p[1]", "endOffset"=>3}], "text": "una anotacion" @@ -59,12 +59,27 @@ describe Legislation::AnnotationsController do expect do xhr :post, :create, process_id: @process.id, draft_version_id: @draft_version.id, - annotation: { + legislation_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 + + it 'should create an annotation by parsing parameters in JSON' do + sign_in @user + + expect do + xhr :post, :create, process_id: @process.id, + draft_version_id: @draft_version.id, + legislation_annotation: { + "quote"=>"Ordenación Territorial", + "ranges"=>[{"start"=>"/p[1]", "startOffset"=>1, "end"=>"/p[1]", "endOffset"=>3}].to_json, + "text": "una anotacion" + } + end.to change { @draft_version.annotations.count }.by(1) + end + end end