Browse Source

Merge pull request #120 from lexintegritastech/enhance-form-for-doctrines

Enhance `doctrines#form`
pull/121/head
Alexander D. Bondoc 4 years ago committed by GitHub
parent
commit
45bec150ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      app/components/doctrine_form_component.rb
  2. 32
      app/components/doctrine_form_component/doctrine_form_component.html.erb
  3. 4
      app/controllers/doctrines_controller.rb
  4. 25
      app/javascript/controllers/doctrines_controller.js
  5. 6
      app/reflexes/doctrines_reflex.rb
  6. 12
      app/views/doctrines/edit.html.erb
  7. 17
      app/views/doctrines/new.html.erb
  8. 2
      app/views/document/doctrines/show.html.erb
  9. 2
      app/views/documents/show.html.erb
  10. 64
      app/views/shared/_annotation_form.html.erb
  11. 6
      config/routes.rb

10
app/components/doctrine_form_component.rb

@ -0,0 +1,10 @@
class DoctrineFormComponent < BaseComponent
attr_reader :doctrine, :jurisprudence_id, :opts
def initialize(current_user:, doctrine:, jurisprudence_id:, opts: {})
@doctrine = doctrine
@jurisprudence_id = jurisprudence_id
@opts = opts
end
end

32
app/components/doctrine_form_component/doctrine_form_component.html.erb

@ -0,0 +1,32 @@
<div class="row">
<div class="col-sm-12">
<label for="subject-name" class="col-form-label"> Subjects </label>
<%= hidden_field_tag :document_id, jurisprudence_id, data: { target: "doctrines.document_id" } %>
<%= hidden_field_tag :doctrine_id, nil, data: { target: "doctrines.doctrine_id" } %>
<select class="form-control subject-ids-selectize ps-0" name="subject_ids[]" id="subject_ids_" multiple="true" required> </select>
<div class="invalid-feedback" id="subjects-blank-message"> Subject/s cannot be blank. </div>
</div>
</div>
<div class="row" id="doctine_content_suggestions_div">
<div class="col-sm-12">
<label for="subject-name" class="col-form-label">Doctine content suggestions</label>
<select id="doctine_content_suggestions" class="form-control ps-0"> </select>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<label for="subject-name" class="col-form-label">Head Note</label>
<%= text_field_tag :headnote, nil, class: "form-control", placeholder: "Head note", required: true, data: { target: "doctrines.headnote" } %>
<div class="invalid-feedback" id="headnote-blank-message"> Headnote cannot be blank. </div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<label for="subject-name" class="col-form-label">Content</label>
<%= rich_text_area_tag :content, nil, placeholder: "Doctrine Content", required: true, data: { target: "doctrines.content" } %>
<div class="invalid-feedback" id="content-blank-message"> Content cannot be blank. </div>
<div>
</div>

4
app/controllers/doctrines_controller.rb

@ -4,6 +4,10 @@ class DoctrinesController < ApplicationController
load_and_authorize_resource :doctrine, class: "Doctrine" load_and_authorize_resource :doctrine, class: "Doctrine"
def new; end
def edit; end
def index def index
@search = doctrine_search(search_params) @search = doctrine_search(search_params)
@doctrines = @search.results @doctrines = @search.results

25
app/javascript/controllers/doctrines_controller.js

@ -49,21 +49,22 @@ export default class extends ApplicationController {
} }
save () { save () {
var $this = this, $modal = $("#doctrineModal"), document_id = $this.document_idTarget.value, doctrine_id = $this.doctrine_idTarget.value var $this = this, $form = $("#doctrineForm"), document_id = $this.document_idTarget.value, doctrine_id = $this.doctrine_idTarget.value
var params = { subject_ids: $modal.find("select[name='subject_ids[]']").val(), headnote: $this.headnoteTarget.value, var params = { subject_ids: $form.find("select[name='subject_ids[]']").val(), headnote: $this.headnoteTarget.value,
content: $this.contentTarget.value } content: $this.contentTarget.value }
console.log(params)
if (!params.subject_ids.length) { if (!params.subject_ids.length) {
$("#subjects-blank-message").show() $form.find("#subjects-blank-message").show()
} }
if (!params.headnote.length) { if (!params.headnote.length) {
$("#headnote-blank-message").show() $form.find("#headnote-blank-message").show()
} }
if (!params.content.length) { if (!params.content.length) {
$("#content-blank-message").show() $form.find("#content-blank-message").show()
} }
if (params.subject_ids.length && params.headnote.length && params.content.length) { if (params.subject_ids.length && params.headnote.length && params.content.length) {
@ -93,7 +94,7 @@ export default class extends ApplicationController {
const contrim_alert = confirm("Do you want to create an Annotation?") const contrim_alert = confirm("Do you want to create an Annotation?")
if (contrim_alert) { if (contrim_alert) {
$this.stimulate("DoctrinesReflex#render_annotation_modal_form", document_id, data.id) $this.stimulate("DoctrinesReflex#render_annotation_form", document_id, data.id)
$('.toast').fadeOut(5000) $('.toast').fadeOut(5000)
} else { } else {
$(".btn-close-x").trigger("click") $(".btn-close-x").trigger("click")
@ -132,18 +133,18 @@ export default class extends ApplicationController {
} }
saveAnnotation () { saveAnnotation () {
var $modal = $("#doctrineModal") var $form = $("#doctrineForm")
const params = { doctrine_id: $modal.find("#doctrine_id").val(), document_id: $modal.find("#document_id").val(), const params = { doctrine_id: $form.find("#doctrine_id").val(), document_id: $form.find("#document_id").val(),
document_ids: $modal.find("#document_ids").val().split(","), content: $modal.find("#document_title").val(), document_ids: $form.find("#document_ids").val().split(","), content: $form.find("#document_title").val(),
phil_rep: $modal.find("#phil_rep").val(), editor_notes: $modal.find("#editor_notes").val(), phil_rep: $form.find("#phil_rep").val(), editor_notes: $form.find("#editor_notes").val(),
annomark_ids: $modal.find("select[name='annomark_ids[]']").val() } annomark_ids: $form.find("select[name='annomark_ids[]']").val() }
$.post("/api/doctrines/" + params.doctrine_id + "/annotations", params, function (result, status) { $.post("/api/doctrines/" + params.doctrine_id + "/annotations", params, function (result, status) {
if(status === "success") { if(status === "success") {
$(".btn-close-x").trigger("click"); $(".btn-close-x").trigger("click");
$('.toast').addClass('bg-success').show(); $('.toast').addClass('bg-success').show();
$( ".toast-body" ).html( "<span><b>Submitted Successfully</b></span>" ); $( ".toast-body" ).html( "<span><b>Submitted Successfully</b></span>" );
location.pathname = "/documents/" + $modal.find("#current_document_id").val() + "/doctrines/" + params.doctrine_id location.pathname = "/documents/" + $form.find("#current_document_id").val() + "/doctrines/" + params.doctrine_id
} else { } else {
$(".btn-close-x").trigger("click") $(".btn-close-x").trigger("click")
$('.toast').addClass('bg-danger').show() $('.toast').addClass('bg-danger').show()

6
app/reflexes/doctrines_reflex.rb

@ -13,4 +13,10 @@ class DoctrinesReflex < ApplicationReflex
document = Cdao::Document.where(reference_number: jurisprudence.reference_number).first document = Cdao::Document.where(reference_number: jurisprudence.reference_number).first
morph "#doctrineModal", render(partial: "/shared/annotation_modal_form", locals: { document: document, doctrine_id: doctrine_id, jurisprudence: jurisprudence }) morph "#doctrineModal", render(partial: "/shared/annotation_modal_form", locals: { document: document, doctrine_id: doctrine_id, jurisprudence: jurisprudence })
end end
def render_annotation_form(document_id, doctrine_id)
jurisprudence = Cdao::Jurisprudence.find(document_id)
document = Cdao::Document.where(reference_number: jurisprudence.reference_number).first
morph "#doctrineForm", render(partial: "/shared/annotation_form", locals: { annotation: Annotation.new, document: document, doctrine_id: doctrine_id, jurisprudence: jurisprudence })
end
end end

12
app/views/doctrines/edit.html.erb

@ -0,0 +1,12 @@
<div class="container-fluid mt-2 p-0">
<div class="col-sm-12">
<div class="card-header" style="border-bottom: 1px solid darkred;"> <h4 class="mb-0"> New Doctrine </h4> </div>
<div class="card-body" id="doctrineForm">
<%= render(DoctrineFormComponent.new(current_user: current_user, doctrine: @doctrine)) %>
</div>
<div class="card-footer d-flex justify-content-end mt-2">
<button type="button" class="btn btn-success me-2" data-action="click->doctrines#save">Save</button>
<button type="button" class="btn btn-danger ms-2" data-bs-dismiss="modal">Back</button>
</div>
</div>
</div>

17
app/views/doctrines/new.html.erb

@ -0,0 +1,17 @@
<div class="container-fluid mt-2 p-0" data-controller="doctrines" id="doctrineForm">
<div class="col-sm-12">
<div class="card-header" style="border-bottom: 1px solid darkred;"> <h4 class="mb-0"> New Doctrine </h4> </div>
<div class="card-body">
<%= render(DoctrineFormComponent.new(current_user: current_user, doctrine: @doctrine, jurisprudence_id: params[:jurisprudence_id])) %>
</div>
<div class="card-footer d-flex justify-content-end mt-2">
<button type="button" class="btn btn-success me-2" data-action="click->doctrines#save">Save</button>
<% if params[:doctrine_id].present? %>
<a class="btn btn-danger ms-2" href="<%= document_doctrine_path(params[:jurisprudence_id], params[:doctrine_id]) %>">Back</a>
<% else %>
<a class="btn btn-danger ms-2" href="<%= document_path(params[:jurisprudence_id]) %>">Back</a>
<% end %>
</div>
</div>
</div>
</div>

2
app/views/document/doctrines/show.html.erb

@ -47,7 +47,7 @@
<div class="card-body pt-0"> <div class="card-body pt-0">
<div class="row"> <div class="row">
<div class="d-flex justify-content-end p-2"> <div class="d-flex justify-content-end p-2">
<a class="btn btn-success" data-controller="doctrines" data-document-id="<%= @document.id %>" data-action="click->doctrines#renderForm" data-bs-toggle="modal" data-bs-target="#doctrineModal"> Add Doctrine </a> <a class="btn btn-success" href="<%= new_doctrine_path(jurisprudence_id: @document.id, doctrine_id: @doctrine.id) %>"> Add Doctrine </a>
</div> </div>
</div> </div>

2
app/views/documents/show.html.erb

@ -65,7 +65,7 @@
<div class="tab-pane fade show <%= params[:is_document_view].blank? && params[:is_citator_tab].blank? ? 'active show' : '' %>" id="analysisTabContent" role="tabpanel" aria-labelledby="home-tab"> <div class="tab-pane fade show <%= params[:is_document_view].blank? && params[:is_citator_tab].blank? ? 'active show' : '' %>" id="analysisTabContent" role="tabpanel" aria-labelledby="home-tab">
<div class="row"> <div class="row">
<div class="d-flex justify-content-end" style="margin: 10px 0 0 -20px;"> <div class="d-flex justify-content-end" style="margin: 10px 0 0 -20px;">
<a class="btn btn-warning" data-controller="doctrines" data-document-id="<%= @document.id %>" data-action="click->doctrines#renderForm" data-bs-toggle="modal" data-bs-target="#doctrineModal"> Add Doctrine </a> <a class="btn btn-warning" href="<%= new_doctrine_path(jurisprudence_id: @document.id) %>"> Add Doctrine </a>
</div> </div>
<div class="container-sm row-flex col-sm-12 mt-2"> <div class="container-sm row-flex col-sm-12 mt-2">
<%= render(DoctrineModalFormComponent.new(current_user: current_user)) %> <%= render(DoctrineModalFormComponent.new(current_user: current_user)) %>

64
app/views/shared/_annotation_form.html.erb

@ -0,0 +1,64 @@
<div class="col-sm-12" data-controller="selectize">
<div class="card-header" style="border-bottom: 1px solid darkred;"> <h4 class="mb-0"> New Annotation </h4> </div>
<div class="card-body">
<div class="row">
<div class="col-sm-12 p-2">
<strong> <%= label_tag :annotation_marks %> </strong>
<%= hidden_field_tag :doctrine_id, doctrine_id %>
<%= hidden_field_tag :current_document_id, jurisprudence.id %>
<%= hidden_field_tag :document_id, document.id %>
<%= hidden_field_tag :document_ids, nil %>
<%= select_tag "annomark_ids[]", options_from_collection_for_select(Annomark.all.order(name: :asc), :id, :name),
class: "form-control default-selectize ps-0", multiple: true, prompt: "Please select" %>
</div>
</div>
<div class="row">
<div class="col-sm-12 p-2">
<strong> <%= label_tag :document_title %> </strong>
<%= text_area_tag :document_title, document.display_text, class: "form-control selected-citing-docs" %>
</div>
</div>
<div id="annotatedDocumentsSearchResults">
<div class="row">
<div class="col-sm-10 p-2">
<%= text_field_tag :q, nil, class: "form-control", placeholder: "Search GR Number, Title or Short Title", data: { target: "annotations.q" } %>
</div>
<div class="col-sm-1 p-2">
<button type="button" class="btn btn-success" data-action="click->annotations#searchDocuments"> Search </button>
</div>
</div>
<div class="row tab">
<table class="table table-striped table-hover mb-0" style="width: 98%; margin: 0 auto;">
<thead>
<th class="bg-light"> Reference No. </th>
<th class="bg-light"> Title </th>
<th class="bg-light"> Date </th>
<th class="bg-light"> </th>
</thead>
</table>
</div>
</div>
<div class="row">
<div class="col-sm-12 p-2">
<strong> <%= label_tag :phil_rep %> </strong>
<%= text_field_tag :phil_rep, document.phil_rep, class: "form-control" %>
</div>
</div>
<div class="row">
<div class="col-sm-12 p-2">
<strong> <%= label_tag :editor_notes %> </strong>
<%= rich_text_area_tag :editor_notes, nil, placeholder: "Editor Notes" %>
</div>
</div>
</div>
<div class="card-footer d-flex justify-content-end mt-2">
<button type="button" class="btn btn-success me-2" data-action="click->doctrines#saveAnnotation">Save</button>
<button type="button" class="btn btn-danger ms-2" href="<%= document_doctrine(jurisprudence.id, doctrine_id) %>">Back</button>
</div>
</div>

6
config/routes.rb

@ -3,7 +3,7 @@ Rails.application.routes.draw do
root to: "documents#index" root to: "documents#index"
resources :doctrines, only: %i[index show] do resources :doctrines, only: %i[index show new edit] do
get :search, on: :collection get :search, on: :collection
scope module: :doctrine do scope module: :doctrine do
resources :annotations, only: %i[create update destroy] resources :annotations, only: %i[create update destroy]
@ -27,6 +27,10 @@ Rails.application.routes.draw do
resources :reports, only: %i[index] resources :reports, only: %i[index]
namespace :api, defaults: { format: :html } do
resources :doctrines, only: %i[new edit]
end
namespace :api, defaults: { format: :json } do namespace :api, defaults: { format: :json } do
resources :doctrines do resources :doctrines do
scope module: :doctrine do scope module: :doctrine do

Loading…
Cancel
Save