Browse Source

Add UI for `CRUD` in `subject_index`

pull/6/head
alexdbondoc17 4 years ago committed by Angel Aviel Domaoan
parent
commit
fae4cc8b51
  1. 8
      app/components/subject_accordion_component.rb
  2. 51
      app/components/subject_accordion_component/subject_accordion_component.html.erb
  3. 15
      app/components/subject_index_form_component.rb
  4. 25
      app/components/subject_index_form_component/subject_index_form_component.html.erb
  5. 4
      app/controllers/application_controller.rb
  6. 69
      app/controllers/subject_indexes_controller.rb
  7. 2
      app/models/ability.rb
  8. 9
      app/models/cdao/subject.rb
  9. 11
      app/views/subject_indexes/edit.html.erb
  10. 26
      app/views/subject_indexes/index.html.erb
  11. 11
      app/views/subject_indexes/new.html.erb
  12. 23
      app/views/subject_indexes/show.html.erb

8
app/components/subject_accordion_component.rb

@ -0,0 +1,8 @@
class SubjectAccordionComponent < BaseComponent
attr_reader :parent
def initialize(current_user:, parent:)
@parent = parent
end
end

51
app/components/subject_accordion_component/subject_accordion_component.html.erb

@ -0,0 +1,51 @@
<% parent.children.each do |second_subject| %>
<div class="accordion accordion-flush ps-20" id="#secondLevelPanel<%= second_subject.id %>">
<div class="accordion-item">
<div class="<%= second_subject.children.present? ? 'accordion-button' : '' %> collapsed" data-bs-toggle="collapse" data-bs-target="#<%= ["secondLevel", second_subject.id].join %>">
<%= link_to second_subject.name, subject_index_path(second_subject.id), class: "accordion-link" %>
</div>
<div class="accordion-collapse collapse" id="<%= ["secondLevel", second_subject.id].join %>" data-bs-parent="#secondLevelPanel<%= second_subject.id %>">
<div class="accordion-body pt-1">
<% second_subject.children.each do |third_subject| %>
<div class="accordion accordion-flush ps-20" id="thirdLevelPanel<%= third_subject.id %>">
<div class="accordion-item">
<div class="<%= third_subject.children.present? ? 'accordion-button' : '' %> collapsed" data-bs-toggle="collapse" data-bs-target="#<%= ["thirdLevel", third_subject.id].join %>">
<%= link_to third_subject.name, subject_index_path(third_subject.id), class: "accordion-link" %>
</div>
<div class="accordion-collapse collapse" id="<%= ["thirdLevel", third_subject.id].join %>" data-bs-parent="#thirdLevelPanel<%= third_subject.id %>">
<div class="accordion-body pt-1">
<% third_subject.children.each do |fourth_subject| %>
<div class="accordion accordion-flush ps-20" id="fourthLevelPanel<%= fourth_subject.id %>">
<div class="accordion-item">
<div class="<%= fourth_subject.children.present? ? 'accordion-button' : '' %> collapsed" data-bs-toggle="collapse" data-bs-target="#<%= ["fourthLevel", fourth_subject.id].join %>">
<%= link_to fourth_subject.name, subject_index_path(fourth_subject.id), class: "accordion-link" %>
</div>
<div class="accordion-collapse collapse" id="<%= ["fourthLevel", fourth_subject.id].join %>" data-bs-parent="#fourthLevelPanel<%= fourth_subject.id %>">
<div class="accordion-body pt-1">
<% fourth_subject.children.each do |fifth_subject| %>
<div class="accordion accordion-flush ps-20">
<div class="accordion-item">
<div>
<%= link_to fifth_subject.name, subject_index_path(fifth_subject.id), class: "accordion-link" %>
</div>
</div>
</div>
<% end %>
</div>
</div>
</div>
</div>
<% end %>
</div>
</div>
</div>
</div>
<% end %>
</div>
</div>
</div>
</div>
<% end %>

15
app/components/subject_index_form_component.rb

@ -0,0 +1,15 @@
class SubjectIndexFormComponent < BaseComponent
attr_reader :subject_index, :form_url, :form_method, :parent_id
def initialize(current_user:, opts:)
@subject_index = opts[:subject_index]
@form_url = opts[:form_url]
@form_method = opts[:form_method]
@parent_id = parent_id
end
def render?
subject_index.present? && form_url.present? && form_method.present?
end
end

25
app/components/subject_index_form_component/subject_index_form_component.html.erb

@ -0,0 +1,25 @@
<%= form_for(@subject_index, url: subject_indexes_path, method: :post) do |form| %>
<div class="row">
<div class="<%= parent_id.present? ? 'col-sm-6' : 'col-sm-12' %> p-2">
<div class="form-floating">
<%= form.text_field :name, class: "form-control" %>
<%= label_tag :name %>
</div>
</div>
<% if parent_id.present? %>
<div class="col-sm-6 p-2">
<div class="form-floating">
<%= form.text_field :parent, class: "form-control" %>
<%= label_tag :parent %>
</div>
</div>
<% end %>
</div>
<div class="row">
<div class="col-sm-12 p-2">
<%= submit_tag "Save", class: "btn btn-primary text-dark" %>
</div>
</div>
<% end %>

4
app/controllers/application_controller.rb

@ -1,3 +1,7 @@
class ApplicationController < ActionController::Base class ApplicationController < ActionController::Base
before_action :authenticate_user! before_action :authenticate_user!
rescue_from CanCan::AccessDenied do |exception|
redirect_to root_url, notice: "You are not authorized to access this page."
end
end end

69
app/controllers/subject_indexes_controller.rb

@ -1,6 +1,6 @@
# frozen_string_literal: true
class SubjectIndexesController < ApplicationController class SubjectIndexesController < ApplicationController
load_and_authorize_resource :subject_index, class: "Cdao::Subject"
def index; end def index; end
def new; end def new; end
@ -9,9 +9,68 @@ class SubjectIndexesController < ApplicationController
def show; end def show; end
def create; end def create
respond_to do |format|
if @subject_index.save
format.html { redirect_to subject_index_path(@subject_index), notice: "Subject Index was successfully created." }
else
format.html { render :new }
end
end
end
def update
respond_to do |format|
if @subject_index.update(resource_params)
format.html { redirect_to subject_index_path(@subject_index), notice: "Subject Index was successfully updated." }
else
format.html { render :edit }
end
end
end
def destroy
respond_to do |format|
if @subject_index.destroy
format.html { redirect_to subject_indexes_path, notice: "Subject Index was successfully destroyed." }
else
format.html { redirect_to subject_index_path(@subject_index), alert: @subject_index.errors.full_messages }
end
end
end
private
def search_subject(args)
Cdao::Subject.search do
if args[:q].present?
fulltext args[:q], fields: %i[name], query_phrase_slop: 0, minimum_match: 1 do
%i[name code series_no].each { |field| highlight field, max_snippets: 3, fragment_size: 100 }
end
end
if args[:name].present?
fulltext args[:q], fields: %i[name], query_phrase_slop: 0, minimum_match: 1 do
highlight :name, max_snippets: 3, fragment_size: 100
end
end
if args[:parent_id].present?
with :parent_id, args[:parent_id].to_i
else
with :parent_id, nil
end
order_by :name, :asc
paginate page: args[:page] || 1, per_page: args[:per_page] || 20
end
end
def update; end def resource_params
params.permit(:name, parent_id)
end
def destroy; end def search_params
params.permit(:name, :parent_id, :state)
end
end end

2
app/models/ability.rb

@ -9,6 +9,8 @@ class Ability
if user.persisted? if user.persisted?
can :display, Cdao::Jurisprudence can :display, Cdao::Jurisprudence
can :manage, :all
end end
end end

9
app/models/cdao/subject.rb

@ -47,4 +47,13 @@ class Cdao::Subject < Cdao::Base
[name, "(#{product_names})"].join(" ") [name, "(#{product_names})"].join(" ")
end end
searchable do
integer :parent_id
text :name
string :name
string :state
end
end end

11
app/views/subject_indexes/edit.html.erb

@ -0,0 +1,11 @@
<div class="container-fluid mt-2 p-0">
<div class="col-sm-12">
<div class="card">
<div class="card-header"> <h2> Edit Subject Index </h2> </div>
<div class="card-body">
<%= render(SubjectIndexFormComponent.new(current_user: current_user, opts: { subject_index: @subject_index, form_url: subject_index_path(@subject_index), form_method: :patch, parent_id: @subject_index.parent_id })) %>
</div>
</div>
</div>
</div>

26
app/views/subject_indexes/index.html.erb

@ -0,0 +1,26 @@
<div class="container-fluid mt-2 p-0">
<div class="col-sm-12">
<div class="card">
<div class="card-header"> <h2> Subject Indexes </h2> </div>
<div class="card-body">
<% Cdao::Subject.roots.all.each do |subject| %>
<div class="accordion accordion-flash" id="mainPanel<%= subject.id %>">
<div class="accordion-item">
<div class="<%= subject.children.present? ? 'accordion-button' : '' %> collapsed" data-bs-toggle="collapse" data-bs-target="#<%= ["firstLevel", subject.id].join %>">
<%= link_to subject.name, subject_index_path(subject.id), class: "accordion-link" %>
</div>
<div class="accordion-collapse collapse" id="<%= ["firstLevel", subject.id].join %>" data-bs-parent="#mainPanel<%= subject.id %>">
<div class="accordion-body pt-1 pb-1">
<%= render(SubjectAccordionComponent.new(current_user: current_user, parent: subject)) %>
</div>
</div>
</div>
</div>
<% end %>
</div>
</div>
</div>
</div>

11
app/views/subject_indexes/new.html.erb

@ -0,0 +1,11 @@
<div class="container-fluid mt-2 p-0">
<div class="col-sm-12">
<div class="card">
<div class="card-header"> <h2> New Subject Index </h2> </div>
<div class="card-body">
<%= render(SubjectIndexFormComponent.new(current_user: current_user, opts: { subject_index: @subject_index, form_url: subject_indexes_path, form_method: :post, parent_id: params[:parent_id] })) %>
</div>
</div>
</div>
</div>

23
app/views/subject_indexes/show.html.erb

@ -0,0 +1,23 @@
<div class="container-fluid mt-2 p-0">
<div class="col-sm-12">
<div class="card">
<div class="card-header">
<h2> <%= @subject_index.name %>
<% if can? :destroy, Cdao::Subject %>
<a href="<%= subject_index_path(@subject_index) %>" class="btn btn-sm btn-danger right-0" data-confirm="Are you sure you want to delete this subject?" data-method="DELETE" role="button">
<i class="fas fa-trash-alt" data-toggle="tooltip" data-placement="bottom" title="Delete"></i>
</a>
<% end %>
<% if can? :update, Cdao::Subject %>
<a href="<%= edit_subject_index_path(@subject_index) %>" class="btn btn-sm btn-danger right-0" role="button">
<i class="fas fa-edit" data-toggle="tooltip" data-placement="bottom" title="Edit"></i>
</a>
<% end %>
</h2>
</div>
<div class="card-body">
<%= render(SubjectAccordionComponent.new(current_user: current_user, parent: @subject_index)) %>
</div>
</div>
</div>
</div>
Loading…
Cancel
Save