Merge pull request #95 from medialab-prado/69-find-annotation-with-same-ranges
Create a new comment instead of a new annotation with the same ranges
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
@@ -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"
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user