From 53d13d85249ece169910e495c76d920c13e56b90 Mon Sep 17 00:00:00 2001 From: Amaia Castro Date: Wed, 25 Jan 2017 16:04:10 +0100 Subject: [PATCH] Create a new comment instead of a new annotation with the same ranges --- .../legislation/annotations_controller.rb | 24 ++++++++++++++----- app/models/legislation/annotation.rb | 8 +++++++ ...anges_fields_to_legislation_annotations.rb | 10 ++++++++ db/schema.rb | 7 +++++- .../annotations_controller_spec.rb | 20 ++++++++++++++++ spec/factories.rb | 4 ++++ 6 files changed, 66 insertions(+), 7 deletions(-) create mode 100644 db/migrate/20170125093101_add_ranges_fields_to_legislation_annotations.rb diff --git a/app/controllers/legislation/annotations_controller.rb b/app/controllers/legislation/annotations_controller.rb index c97165e2a..0e536dd5e 100644 --- a/app/controllers/legislation/annotations_controller.rb +++ b/app/controllers/legislation/annotations_controller.rb @@ -25,13 +25,25 @@ class Legislation::AnnotationsController < ApplicationController 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 + existing_annotation = @draft_version.annotations.where( + range_start: annotation_params[:ranges].first[:start], range_start_offset: annotation_params[:ranges].first[:startOffset].to_i, + range_end: annotation_params[:ranges].first[:end], range_end_offset: annotation_params[:ranges].first[:endOffset].to_i).first + + if @annotation = existing_annotation + if comment = @annotation.comments.create(body: annotation_params[:text], user: current_user) + render json: @annotation.to_json + else + render json: comment.errors.full_messages, status: :unprocessable_entity + end else - render json: @annotation.errors.full_messages, status: :unprocessable_entity + @annotation = @draft_version.annotations.new(annotation_params) + @annotation.author = current_user + if @annotation.save + track_event + render json: @annotation.to_json + else + render json: @annotation.errors.full_messages, status: :unprocessable_entity + end end end diff --git a/app/models/legislation/annotation.rb b/app/models/legislation/annotation.rb index b038b5e51..f9f612abd 100644 --- a/app/models/legislation/annotation.rb +++ b/app/models/legislation/annotation.rb @@ -14,8 +14,16 @@ class Legislation::Annotation < ActiveRecord::Base validates :draft_version, presence: true validates :author, presence: true + before_save :store_range after_create :create_first_comment + def store_range + self.range_start = ranges.first["start"] + self.range_start_offset = ranges.first["startOffset"] + self.range_end = ranges.first["end"] + self.range_end_offset = ranges.first["endOffset"] + end + def create_first_comment comments.create(body: self.text, user: self.author) end diff --git a/db/migrate/20170125093101_add_ranges_fields_to_legislation_annotations.rb b/db/migrate/20170125093101_add_ranges_fields_to_legislation_annotations.rb new file mode 100644 index 000000000..69ae9df65 --- /dev/null +++ b/db/migrate/20170125093101_add_ranges_fields_to_legislation_annotations.rb @@ -0,0 +1,10 @@ +class AddRangesFieldsToLegislationAnnotations < ActiveRecord::Migration + def change + add_column :legislation_annotations, :range_start, :string + add_column :legislation_annotations, :range_start_offset, :integer + add_column :legislation_annotations, :range_end, :string + add_column :legislation_annotations, :range_end_offset, :integer + + add_index :legislation_annotations, [:range_start, :range_end] + end +end diff --git a/db/schema.rb b/db/schema.rb index 8118089ee..a847e445e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170114154421) do +ActiveRecord::Schema.define(version: 20170125093101) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -335,11 +335,16 @@ ActiveRecord::Schema.define(version: 20170114154421) do t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "comments_count", default: 0 + t.string "range_start" + t.integer "range_start_offset" + t.string "range_end" + t.integer "range_end_offset" end add_index "legislation_annotations", ["author_id"], name: "index_legislation_annotations_on_author_id", using: :btree add_index "legislation_annotations", ["hidden_at"], name: "index_legislation_annotations_on_hidden_at", using: :btree add_index "legislation_annotations", ["legislation_draft_version_id"], name: "index_legislation_annotations_on_legislation_draft_version_id", using: :btree + add_index "legislation_annotations", ["range_start", "range_end"], name: "index_legislation_annotations_on_range_start_and_range_end", using: :btree create_table "legislation_answers", force: :cascade do |t| t.integer "legislation_question_id" diff --git a/spec/controllers/legislation/annotations_controller_spec.rb b/spec/controllers/legislation/annotations_controller_spec.rb index 4ffd91357..b8be3c630 100644 --- a/spec/controllers/legislation/annotations_controller_spec.rb +++ b/spec/controllers/legislation/annotations_controller_spec.rb @@ -81,5 +81,25 @@ describe Legislation::AnnotationsController do end.to change { @draft_version.annotations.count }.by(1) end + it 'should create 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"=>5, "end"=>"/p[1]", "endOffset"=>10}], + range_start: "/p[1]", range_start_offset: 5, range_end: "/p[1]", range_end_offset: 10) + 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"=>5, "end"=>"/p[1]", "endOffset"=>10}], + "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") + end + end end diff --git a/spec/factories.rb b/spec/factories.rb index 5ed16fc30..c0137e175 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -511,6 +511,10 @@ FactoryGirl.define do quote "ipsum" text "Loremp ipsum dolor" ranges [{"start"=>"/div[1]", "startOffset"=>5, "end"=>"/div[1]", "endOffset"=>10}] + range_start "/div[1]" + range_start_offset 5 + range_end "/div[1]" + range_end_offset 10 end factory :legislation_question, class: 'Legislation::Question' do