Browse Source

Implement \`API\` request of CRUD in \`doctrines\`

pull/40/head
alexdbondoc17 4 years ago
parent
commit
d4456f337f
  1. 6
      app/components/doctrine_index_table_component.rb
  2. 10
      app/components/doctrine_index_table_component/doctrine_index_table_component.html.erb
  3. 5
      app/components/doctrine_modal_form_component/doctrine_modal_form_component.html.erb
  4. 7
      app/components/document_doctrine_index_component.rb
  5. 2
      app/components/document_doctrine_index_component/document_doctrine_index_component.html.erb
  6. 5
      app/components/document_doctrine_show_component.rb
  7. 20
      app/components/document_doctrine_show_component/document_doctrine_show_component.html.erb
  8. 1
      app/controllers/api/doctrines_controller.rb
  9. 4
      app/controllers/doctrines_controller.rb
  10. 23
      app/javascript/controllers/doctrines_controller.js
  11. 1
      app/views/api/doctrines/destroy.json.jbuilder
  12. 1
      app/views/api/doctrines/update.json.jbuilder
  13. 1
      app/views/doctrines/search.html.erb
  14. 13
      app/views/doctrines/show.html.erb
  15. 2
      app/views/layouts/application.html.erb

6
app/components/doctrine_index_table_component.rb

@ -6,6 +6,12 @@ class DoctrineIndexTableComponent < BaseComponent
@opts = opts @opts = opts
end end
def jurisprudence(search_result)
return "(No Assigned Document)" if search_result.doctrine_jurisprudences.blank?
search_result.doctrine_jurisprudences.first.jurisprudence
end
def document_reference_number(document) def document_reference_number(document)
document.reference_number document.reference_number
end end

10
app/components/doctrine_index_table_component/doctrine_index_table_component.html.erb

@ -7,11 +7,11 @@
<tbody> <tbody>
<% search_results.each do |search_result| %> <% search_results.each do |search_result| %>
<tr href="<%= document_path(search_result.doctrine_jurisprudences.first.jurisprudence_id) %>" class="clickable-tr"> <tr href="<%= jurisprudence(search_result).present? ? document_path(jurisprudence(search_result).id) : '#' %>" class="clickable-tr">
<td> <%= document_reference_number(search_result.document) %> </td> <td> <%= document_reference_number(jurisprudence(search_result)) %> </td>
<td> <%= document_title(search_result.document) %> </td> <td> <%= document_title(jurisprudence(search_result)) %> </td>
<td> <%= date_or_year(search_result.document) %> </td> <td> <%= date_or_year(jurisprudence(search_result)) %> </td>
</tr> </tr>
<% end %> <% end %>
</tbody> </tbody>
</table> </table>

5
app/components/doctrine_modal_form_component/doctrine_modal_form_component.html.erb

@ -19,6 +19,11 @@
<select id="doctine_content_suggestions" class="form-control ps-0"> </select> <select id="doctine_content_suggestions" class="form-control ps-0"> </select>
</div> </div>
<div class="mb-3">
<label for="subject-name" class="col-form-label">Head Note</label>
<%= text_field_tag :headnote, nil, class: "form-control", placeholder: "Head note", data: { target: "doctrines.headnote" } %>
</div>
<div class="mb-3"> <div class="mb-3">
<label for="subject-name" class="col-form-label">Content</label> <label for="subject-name" class="col-form-label">Content</label>
<%= rich_text_area_tag :content, nil, placeholder: "Doctrine Content", data: { target: "doctrines.content" } %> <%= rich_text_area_tag :content, nil, placeholder: "Doctrine Content", data: { target: "doctrines.content" } %>

7
app/components/document_doctrine_index_component.rb

@ -12,6 +12,7 @@ class DocumentDoctrineIndexComponent < BaseComponent
delegate :content, to: :doctrine delegate :content, to: :doctrine
delegate :annotations, to: :doctrine delegate :annotations, to: :doctrine
delegate :subjects, to: :doctrine delegate :subjects, to: :doctrine
delegate :doctrine_jurisprudences, to: :doctrine
def annotation_form_url def annotation_form_url
doctrine_annotations_path(doctrine_id: id) doctrine_annotations_path(doctrine_id: id)
@ -21,6 +22,12 @@ class DocumentDoctrineIndexComponent < BaseComponent
"(No Subjects Provided)" "(No Subjects Provided)"
end end
def jurisprudence
return nil if doctrine_jurisprudences.blank?
doctrine_jurisprudences.first.jurisprudence
end
def document_title(annotation) def document_title(annotation)
return annotation.document.short_title if annotation.document.short_title.present? return annotation.document.short_title if annotation.document.short_title.present?

2
app/components/document_doctrine_index_component/document_doctrine_index_component.html.erb

@ -1,4 +1,4 @@
<div class="row-flex m-3 mt-0 doctrine-content-body clickable-link" href="<%= opts[:document_id].present? ? document_doctrine_path(opts[:document_id], id) : doctrine_path(id)%>"> <div class="row-flex m-3 mt-0 doctrine-content-body clickable-link" href="<%= jurisprudence.present? ? document_doctrine_path(jurisprudence.id, id) : doctrine_path(id)%>">
<% if opts[:is_subjects_index].blank? %> <% if opts[:is_subjects_index].blank? %>
<% if opts[:is_doctrines_index].present? && opts[:subject_ids].reject(&:blank?).present? %> <% if opts[:is_doctrines_index].present? && opts[:subject_ids].reject(&:blank?).present? %>
<h5 style="color: darkred;"> <%= subjects.where(id: opts[:subject_ids].map(&:to_i)).map(&:lineage_name).join(" ") %> </h5> <h5 style="color: darkred;"> <%= subjects.where(id: opts[:subject_ids].map(&:to_i)).map(&:lineage_name).join(" ") %> </h5>

5
app/components/document_doctrine_show_component.rb

@ -9,6 +9,7 @@ class DocumentDoctrineShowComponent < BaseComponent
end end
delegate :id, to: :doctrine delegate :id, to: :doctrine
delegate :headnote, to: :doctrine
delegate :subject_ids, to: :doctrine delegate :subject_ids, to: :doctrine
delegate :content, to: :doctrine delegate :content, to: :doctrine
delegate :annotations, to: :doctrine delegate :annotations, to: :doctrine
@ -24,10 +25,6 @@ class DocumentDoctrineShowComponent < BaseComponent
annotation.document.title annotation.document.title
end end
def all_subjects
Cdao::Subject.all.order(name: :asc)
end
def document def document
Document.find(document_id) Document.find(document_id)
end end

20
app/components/document_doctrine_show_component/document_doctrine_show_component.html.erb

@ -5,9 +5,9 @@
<h5> <b>Doctrine Details</b> </h5> <h5> <b>Doctrine Details</b> </h5>
</div> </div>
<div class="col-sm-2 d-flex justify-content-end pe-0"> <div class="col-sm-2 d-flex justify-content-end pe-0" data-controller="doctrines" data-document-id="<%= document_id %>" data-doctrine-id="<%= id %>" >
<a class="btn btn-sm btn-secondary me-3" data-controller="doctrines" data-document-id="<%= document_id %>" data-doctrine-id="<%= id %>" data-action="click->doctrines#renderForm" data-bs-toggle="modal" data-bs-target="#doctrineModal"> Edit </a> <a class="btn btn-sm btn-secondary me-3"data-action="click->doctrines#renderForm" data-bs-toggle="modal" data-bs-target="#doctrineModal"> Edit </a>
<a class="btn btn-sm btn-danger" href="<%= api_doctrine_path(id) %>" data-confirm="Are you sure to delete this record?" data-method="DELETE"> Delete </a> <a class="btn btn-sm btn-danger" data-action="click->doctrines#delete" > Delete </a>
</div> </div>
</div> </div>
@ -19,7 +19,7 @@
<div class="card-body pt-0"> <div class="card-body pt-0">
<div class="row"> <div class="row">
<div class="col-sm-2" style="width: 100px;"> <div class="col-sm-2" style="width: 120px;">
<strong> Subjects: </strong> <strong> Subjects: </strong>
</div> </div>
@ -29,7 +29,17 @@
</div> </div>
<div class="row"> <div class="row">
<div class="col-sm-2" style="width: 100px;"> <div class="col-sm-2" style="width: 120px;">
<strong> Head Note: </strong>
</div>
<div class="col-sm-10">
<%= headnote %>
</div>
</div>
<div class="row">
<div class="col-sm-2" style="width: 120px;">
<strong> Content: </strong> <strong> Content: </strong>
</div> </div>

1
app/controllers/api/doctrines_controller.rb

@ -28,7 +28,6 @@ module Api
respond_with @doctrine respond_with @doctrine
else else
raise @doctrine.errors.full_messages.inspect
render errors: @doctrine.errors, status: 422 render errors: @doctrine.errors, status: 422
end end
end end

4
app/controllers/doctrines_controller.rb

@ -21,7 +21,9 @@ class DoctrinesController < ApplicationController
end end
end end
def show; end def show
@jurisprudence = @doctrine.doctrine_jurisprudences.present? ? @doctrine.doctrine_jurisprudences.first.jurisprudence : nil
end
def create def create
attrs = resource_params.to_unsafe_h.deep_symbolize_keys attrs = resource_params.to_unsafe_h.deep_symbolize_keys

23
app/javascript/controllers/doctrines_controller.js

@ -1,6 +1,6 @@
import ApplicationController from './application_controller' import ApplicationController from './application_controller'
export default class extends ApplicationController { export default class extends ApplicationController {
static targets = ["input", "document_id", "doctrine_id", "content"] static targets = ["input", "document_id", "doctrine_id", "headnote", "content"]
connect () { connect () {
super.connect() super.connect()
@ -32,6 +32,7 @@ export default class extends ApplicationController {
}); });
$modal.find(".trix-content").val(doctrine.content) $modal.find(".trix-content").val(doctrine.content)
$modal.find("#headnote").val(doctrine.headnote)
} }
}) })
@ -43,12 +44,14 @@ export default class extends ApplicationController {
$modal.find(".trix-content").val("") $modal.find(".trix-content").val("")
$subject_ids_selectize.setValue([]) $subject_ids_selectize.setValue([])
$modal.find("#doctrine_id").val('') $modal.find("#doctrine_id").val('')
$modal.find("#headnote").val('')
} }
} }
save () { save () {
var $modal = $("#doctrineModal"), document_id = this.document_idTarget.value, doctrine_id = this.doctrine_idTarget.value var $modal = $("#doctrineModal"), document_id = this.document_idTarget.value, doctrine_id = this.doctrine_idTarget.value
var params = { subject_ids: $modal.find("select[name='subject_ids[]']").val(), content: this.contentTarget.value } var params = { subject_ids: $modal.find("select[name='subject_ids[]']").val(), headnote: this.headnoteTarget.value,
content: this.contentTarget.value }
if (doctrine_id !== null && doctrine_id !== undefined && doctrine_id !== "") { if (doctrine_id !== null && doctrine_id !== undefined && doctrine_id !== "") {
$.ajax({ $.ajax({
@ -67,4 +70,20 @@ export default class extends ApplicationController {
}) })
} }
} }
delete(ev) {
var doctrine_id = this.element.dataset["doctrineId"], document_id = this.element.dataset["documentId"]
ev.preventDefault();
const contrim_alert = confirm("Are you sure to delete this record?")
if (contrim_alert) {
$.ajax({
url: "/api/doctrines/" + doctrine_id,
type: 'DELETE',
success: function() {
location.pathname = "/documents/" + document_id
}
})
}
}
} }

1
app/views/api/doctrines/destroy.json.jbuilder

@ -0,0 +1 @@
json.(@doctrine, *%i[id subject_ids headnote content plain_content created_at updated_at])

1
app/views/api/doctrines/update.json.jbuilder

@ -0,0 +1 @@
json.(@doctrine, *%i[id subject_ids headnote content plain_content created_at updated_at])

1
app/views/doctrines/search.html.erb

@ -8,5 +8,4 @@
<% else %> <% else %>
<%= render(DocumentDoctrineIndexComponent.with_collection(@doctrines, current_user: current_user, opts: { is_doctrines_index: true, subject_ids: params[:subject_ids] })) %> <%= render(DocumentDoctrineIndexComponent.with_collection(@doctrines, current_user: current_user, opts: { is_doctrines_index: true, subject_ids: params[:subject_ids] })) %>
<% end %> <% end %>
</div> </div>

13
app/views/doctrines/show.html.erb

@ -4,28 +4,23 @@
<tbody> <tbody>
<tr> <tr>
<td class="text-start" style="width: 160px;"> Reference Number: </td> <td class="text-start" style="width: 160px;"> Reference Number: </td>
<td class="text-start"> <strong> <%= @document.reference_number %> </strong> </td> <td class="text-start"> <strong> <%= @jurisprudence.present? ? @jurisprudence.reference_number : "(No Document Reference NUmber)" %> </strong> </td>
<td class="text-end" style="width: 50px;" > Date: </td> <td class="text-end" style="width: 50px;" > Date: </td>
<td class="text-end" style="width: 160px;"> <strong> <%= @document.docdate.present? ? @document.docdate.strftime("%m/%d/%Y") : @document.year %> </strong> </td> <td class="text-end" style="width: 160px;"> <strong> <%= @jurisprudence.present? ? @jurisprudence.docdate : "(No Document Date)" %> </strong> </td>
</tr> </tr>
<tr> <tr>
<td style="width: 160px;" class="text-start"> Title: </td> <td style="width: 160px;" class="text-start"> Title: </td>
<td colspan="3" class="text-start"> <strong> <%= @document.title %> </strong> </td> <td colspan="3" class="text-start"> <strong> <%= @jurisprudence.present? ? @jurisprudence.short_title || @jurisprudence.title : "(No Document Date)" %> </strong> </td>
</tr> </tr>
</tbody> </tbody>
</table> </table>
</div> </div>
<div class="card-body pt-0"> <div class="card-body pt-0">
<div class="row">
<div class="d-flex justify-content-end p-2">
<a class="btn btn-sm btn-primary" data-controller="doctrines" data-document-id="<%= @document.id %>" data-action="click->doctrines#renderForm" data-bs-toggle="modal" data-bs-target="#doctrineModal"> Add Doctrine </a>
</div>
</div>
<div class="row"> <div class="row">
<%= render(DocumentDoctrineShowComponent.new(current_user: current_user, doctrine: @doctrine, document_id: @document.id, subjects: @subjects)) %> <%= render(DocumentDoctrineShowComponent.new(current_user: current_user, doctrine: @doctrine, document_id: @jurisprudence.present? ? @jurisprudence.id : @jurisprudence, subjects: @subjects)) %>
</div> </div>
</div> </div>
</div> </div>

2
app/views/layouts/application.html.erb

@ -16,7 +16,7 @@
<div class="<%= current_user.present? ? 'row flex-nowrap h-100 w-100 p-0 ms-0' : 'row h-100 justify-content-center align-items-center' %>"> <div class="<%= current_user.present? ? 'row flex-nowrap h-100 w-100 p-0 ms-0' : 'row h-100 justify-content-center align-items-center' %>">
<% if current_user.present? %> <% if current_user.present? %>
<div class="col-auto col-md-3 col-xl-2 px-sm-2 px-0 bg-secondary min-vh-100" style="padding: 0 !important;"> <div class="col-auto col-md-3 col-xl-2 px-sm-2 px-0 bg-secondary min-vh-100" style="padding: 0 !important;">
<%= render(SidenavComponent.new(current_user: current_user))%> <%= render(SidenavComponent.new(current_user: current_user, opts: { is_sidenav: true }))%>
</div> </div>
<% end %> <% end %>

Loading…
Cancel
Save