Merge pull request #51 from medialab-prado/14-base-legislation-annotations

Base Legislation Annotations
This commit is contained in:
Fernando Blat
2017-01-04 13:50:37 +01:00
committed by GitHub
12 changed files with 178 additions and 8 deletions

View File

@@ -49,8 +49,9 @@
//= require markdown-it
//= require markdown_editor
//= require cocoon
//= require allegations
//= require legislation
//= require legislation_allegations
//= require legislation_annotatable
//= require custom
var initialize_modules = function() {
@@ -74,8 +75,9 @@ var initialize_modules = function() {
App.Banners.initialize();
App.SocialShare.initialize();
App.MarkdownEditor.initialize();
App.Allegations.initialize();
App.LegislationAllegations.initialize();
App.Legislation.initialize();
App.LegislationAnnotatable.initialize();
};
$(function(){

View File

@@ -1,4 +1,4 @@
App.Allegations =
App.LegislationAllegations =
toggle_comments: ->
$('.draft-allegation').toggleClass('comments-on');
@@ -8,10 +8,10 @@ App.Allegations =
click: (e) ->
e.preventDefault();
e.stopPropagation();
App.Allegations.toggle_comments()
App.LegislationAllegations.toggle_comments()
$('.js-toggle-allegations').on
click: (e) ->
# Toggle comments when the section title is visible
if $(this).find('.draft-panel .panel-title:visible').length == 0
App.Allegations.toggle_comments()
App.LegislationAllegations.toggle_comments()

View File

@@ -0,0 +1,38 @@
_t = (key) -> new Gettext().gettext(key)
App.LegislationAnnotatable =
initialize: ->
current_user_id = $('html').data('current-user-id')
if current_user_id == ""
annotator.ui.editor.Editor.template = [
'<div class="annotator-outer annotator-editor annotator-hide">',
' <form class="annotator-widget">',
' ' + _t('Unregistered'),
' <div class="annotator-controls">',
' <a href="#cancel" class="annotator-cancel">' + _t('Cancel') + '</a>',
' </div>',
' </form>',
'</div>'
].join('\n')
$(".legislation-annotatable").each ->
$this = $(this)
ann_type = "legislation_draft_version"
ann_id = $this.data("legislation-draft-version-id")
base_url = $this.data("legislation-annotatable-base-url")
app = new annotator.App()
.include ->
beforeAnnotationCreated: (ann) ->
ann["legislation_draft_version_id"] = ann_id
ann.permissions = ann.permissions || {}
ann.permissions.admin = []
.include(annotator.ui.main, { element: this })
.include(annotator.storage.http, { prefix: base_url, urls: { search: "/annotations/search" } })
app.start().then ->
app.ident.identity = current_user_id
options = {}
options["legislation_draft_version_id"] = ann_id
app.annotations.load(options)

View File

@@ -0,0 +1,32 @@
class Legislation::AnnotationsController < ApplicationController
skip_before_action :verify_authenticity_token
before_action :authenticate_user!, only: [:create]
load_and_authorize_resource :process
load_and_authorize_resource :draft_version, through: :process
load_and_authorize_resource
def create
@annotation = Legislation::Annotation.new(annotation_params)
@annotation.user = current_user
if @annotation.save
render json: @annotation.to_json
end
end
def search
@annotations = Legislation::Annotation.where(legislation_draft_version_id: params[:legislation_draft_version_id])
annotations_hash = { total: @annotations.size, rows: @annotations }
render json: annotations_hash.to_json
end
private
def annotation_params
params
.require(:annotation)
.permit(:quote, :text, ranges: [:start, :startOffset, :end, :endOffset])
.merge(legislation_draft_version_id: params[:legislation_draft_version_id])
end
end

View File

@@ -15,6 +15,7 @@ module Abilities
can [:read, :changes, :go_to_version], Legislation::DraftVersion
can [:read], Legislation::Question
can [:create], Legislation::Answer
can [:search, :read, :create], Legislation::Annotation
end
end
end

View File

@@ -0,0 +1,6 @@
class Legislation::Annotation < ActiveRecord::Base
serialize :ranges, Array
belongs_to :draft_version, class_name: 'Legislation::DraftVersion', foreign_key: 'legislation_draft_version_id'
belongs_to :user
end

View File

@@ -44,7 +44,12 @@
<div><span class="panel-title"><%= t('.text_body') %></span></div>
</div>
<div class="draft-text">
<%= @draft_version.body_html.html_safe %>
<section class="legislation-annotatable"
data-legislation-draft-version-id="<%= @draft_version.id %>"
data-legislation-annotatable-base-url="<%= legislation_process_draft_version_path(@process, @draft_version) %>"
>
<%= @draft_version.body_html.html_safe %>
</section>
</div>
</div>

View File

@@ -101,7 +101,9 @@ Rails.application.routes.draw do
resources :draft_versions, only: [:show] do
get :go_to_version, on: :collection
get :changes
resources :annotations
resources :annotations do
get :search, on: :collection
end
end
end
end

View File

@@ -0,0 +1,16 @@
class CreateLegislationAnnotations < ActiveRecord::Migration
def change
create_table :legislation_annotations do |t|
t.string :quote
t.text :ranges
t.text :text
t.references :legislation_draft_version, index: true
t.references :user, index: true
t.datetime :hidden_at, index: true
t.timestamps null: false
end
end
end

View File

@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20161229213217) do
ActiveRecord::Schema.define(version: 20170103125835) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -228,6 +228,21 @@ ActiveRecord::Schema.define(version: 20161229213217) do
t.datetime "updated_at", null: false
end
create_table "legislation_annotations", force: :cascade do |t|
t.string "quote"
t.text "ranges"
t.text "text"
t.integer "legislation_draft_version_id"
t.integer "user_id"
t.datetime "hidden_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
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", ["user_id"], name: "index_legislation_annotations_on_user_id", using: :btree
create_table "legislation_answers", force: :cascade do |t|
t.integer "legislation_question_id"
t.integer "legislation_question_option_id"

View File

@@ -399,6 +399,18 @@ FactoryGirl.define do
status "draft"
final_version false
body "Body of the legislation text"
trait :published do
status "published"
end
end
factory :legislation_annotation, class: 'Legislation::Annotation' do
draft_version factory: :legislation_draft_version
user
quote "ipsum"
text "Loremp ipsum dolor"
ranges [{"start"=>"/div[1]", "startOffset"=>5, "end"=>"/div[1]", "endOffset"=>10}]
end
factory :legislation_question, class: 'Legislation::Question' do

View File

@@ -119,4 +119,45 @@ feature 'Legislation Draft Versions' do
end
end
context 'Annotations', :js do
let(:user) { create(:user) }
background { login_as user }
scenario 'Create' do
draft_version = create(:legislation_draft_version, :published, body: Faker::Lorem.paragraph)
visit legislation_process_draft_version_path(draft_version.process, draft_version)
page.find(:css, ".legislation-annotatable").double_click
page.find(:css, ".annotator-adder button").click
fill_in 'annotator-field-0', with: 'this is my annotation'
page.find(:css, ".annotator-controls a[href='#save']").click
expect(page).to have_css ".annotator-hl"
first(:css, ".annotator-hl").click
expect(page).to have_content "this is my annotation"
visit legislation_process_draft_version_path(draft_version.process, draft_version)
expect(page).to have_css ".annotator-hl"
first(:css, ".annotator-hl").click
expect(page).to have_content "this is my annotation"
end
scenario 'Search' do
draft_version = create(:legislation_draft_version, :published, body: Faker::Lorem.paragraph)
annotation1 = create(:legislation_annotation, draft_version: draft_version, text: "my annotation", ranges: [{"start"=>"/p[1]", "startOffset"=>5, "end"=>"/p[1]", "endOffset"=>10}])
annotation2 = create(:legislation_annotation, draft_version: draft_version, text: "my other annotation", ranges: [{"start"=>"/p[1]", "startOffset"=>12, "end"=>"/p[1]", "endOffset"=>19}])
visit legislation_process_draft_version_path(draft_version.process, draft_version)
expect(page).to have_css ".annotator-hl"
first(:css, ".annotator-hl").click
expect(page).to have_content "my annotation"
all(".annotator-hl")[1].click
expect(page).to have_content "my other annotation"
end
end
end