20
app/assets/javascripts/annotatable.js.coffee
Normal file
20
app/assets/javascripts/annotatable.js.coffee
Normal file
@@ -0,0 +1,20 @@
|
||||
App.Annotatable =
|
||||
initialize: ->
|
||||
$("[data-annotatable-type]").each ->
|
||||
$this = $(this)
|
||||
ann_type = $this.data("annotatable-type")
|
||||
ann_id = $this.data("annotatable-id")
|
||||
|
||||
app = new annotator.App()
|
||||
.include(annotator.ui.main, { element: this })
|
||||
.include(annotator.storage.http, { prefix: "", urls: { search: "/annotations/search" } })
|
||||
.include ->
|
||||
beforeAnnotationCreated: (ann) ->
|
||||
ann[ann_type + "_id"] = ann_id
|
||||
|
||||
app.start().then ->
|
||||
app.ident.identity = $('html').data('current-user-id')
|
||||
|
||||
options = {}
|
||||
options[ann_type + "_id"] = ann_id
|
||||
app.annotations.load(options)
|
||||
@@ -1,20 +0,0 @@
|
||||
legislationSelector = ".annotate"
|
||||
|
||||
annotationsMetadata = ->
|
||||
return {
|
||||
beforeAnnotationCreated: (ann) ->
|
||||
ann.legislation_id = $(legislationSelector).data("id");
|
||||
ann.user_id = $(legislationSelector).data("user-id");
|
||||
};
|
||||
|
||||
App.annotations =
|
||||
initialize: ->
|
||||
app = new annotator.App()
|
||||
.include(annotator.ui.main, { element: $(legislationSelector)[0] })
|
||||
.include(annotator.storage.http, { prefix: "", urls: { search: "/annotations/search" } })
|
||||
.include(annotationsMetadata)
|
||||
|
||||
app.start()
|
||||
.then( ->
|
||||
app.annotations.load( { legislation_id: $(legislationSelector).data("id") } )
|
||||
)
|
||||
@@ -29,11 +29,11 @@
|
||||
//= require moderator_debates
|
||||
//= require moderator_proposals
|
||||
//= require prevent_double_submission
|
||||
//= require annotator.min
|
||||
//= require annotator
|
||||
//= require tags
|
||||
//= require users
|
||||
//= require votes
|
||||
//= require annotations
|
||||
//= require annotatable
|
||||
|
||||
|
||||
var initialize_modules = function() {
|
||||
@@ -46,7 +46,7 @@ var initialize_modules = function() {
|
||||
App.CheckAllNone.initialize();
|
||||
App.PreventDoubleSubmission.initialize();
|
||||
App.IeAlert.initialize();
|
||||
App.annotations.initialize();
|
||||
App.Annotatable.initialize();
|
||||
};
|
||||
|
||||
$(function(){
|
||||
|
||||
@@ -4,15 +4,16 @@ class AnnotationsController < ApplicationController
|
||||
|
||||
def create
|
||||
@annotation = Annotation.new(annotation_params)
|
||||
@annotation.user = current_user
|
||||
if @annotation.save
|
||||
render json: @annotation.to_json
|
||||
render json: @annotation.to_json(methods: :permissions)
|
||||
end
|
||||
end
|
||||
|
||||
def update
|
||||
@annotation = Annotation.find(params[:id])
|
||||
if @annotation.update_attributes(annotation_params)
|
||||
render json: @annotation.to_json
|
||||
render json: @annotation.to_json(methods: :permissions)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -25,7 +26,7 @@ class AnnotationsController < ApplicationController
|
||||
def search
|
||||
@annotations = Annotation.where(legislation_id: params[:legislation_id])
|
||||
annotations_hash = { total: @annotations.size, rows: @annotations }
|
||||
render json: annotations_hash.to_json
|
||||
render json: annotations_hash.to_json(methods: :permissions)
|
||||
end
|
||||
|
||||
private
|
||||
@@ -34,6 +35,6 @@ class AnnotationsController < ApplicationController
|
||||
params
|
||||
.require(:annotation)
|
||||
.permit(:quote, :text, ranges: [:start, :startOffset, :end, :endOffset])
|
||||
.merge(legislation_id: params[:legislation_id], user_id: params[:user_id])
|
||||
.merge(legislation_id: params[:legislation_id])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -17,7 +17,10 @@ class ApplicationController < ActionController::Base
|
||||
protect_from_forgery with: :exception
|
||||
|
||||
rescue_from CanCan::AccessDenied do |exception|
|
||||
redirect_to main_app.root_url, alert: exception.message
|
||||
respond_to do |format|
|
||||
format.html { redirect_to main_app.root_url, alert: exception.message }
|
||||
format.json { render json: {error: exception.message}, status: :forbidden }
|
||||
end
|
||||
end
|
||||
|
||||
layout :set_layout
|
||||
|
||||
@@ -40,7 +40,8 @@ module Abilities
|
||||
can :vote_featured, Proposal
|
||||
end
|
||||
|
||||
can :manage, Annotation
|
||||
can :create, Annotation
|
||||
can [:update, :destroy], Annotation, user_id: user.id
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
@@ -7,6 +7,7 @@ module Abilities
|
||||
can :read, Proposal
|
||||
can :read, Legislation
|
||||
can :read, User
|
||||
can [:search, :read], Annotation
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,4 +3,8 @@ class Annotation < ActiveRecord::Base
|
||||
|
||||
belongs_to :legislation
|
||||
belongs_to :user
|
||||
end
|
||||
|
||||
def permissions
|
||||
{ update: [user_id], delete: [user_id] }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="es">
|
||||
<html lang="<%= I18n.locale %>" data-current-user-id="<%= current_user.try(:id) %>">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
<% if current_user %>
|
||||
<section class="annotate" data-id=<%= @legislation.id %> data-user-id=<%= current_user.id %>>
|
||||
<% else %>
|
||||
<section>
|
||||
<% end %>
|
||||
<section data-annotatable-type="legislation"
|
||||
data-annotatable-id="<%= @legislation.id %>">
|
||||
|
||||
<div><%= @legislation.title %></div>
|
||||
<div id="test"><%= @legislation.body %></div>
|
||||
|
||||
Reference in New Issue
Block a user