diff --git a/app/components/subject_accordion_component.rb b/app/components/subject_accordion_component.rb new file mode 100644 index 0000000..20b2f3f --- /dev/null +++ b/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 + \ No newline at end of file diff --git a/app/components/subject_accordion_component/subject_accordion_component.html.erb b/app/components/subject_accordion_component/subject_accordion_component.html.erb new file mode 100644 index 0000000..ef96ed5 --- /dev/null +++ b/app/components/subject_accordion_component/subject_accordion_component.html.erb @@ -0,0 +1,51 @@ +<% parent.children.each do |second_subject| %> +
+
+ + +
" data-bs-parent="#secondLevelPanel<%= second_subject.id %>"> +
+ <% second_subject.children.each do |third_subject| %> +
+
+ + +
" data-bs-parent="#thirdLevelPanel<%= third_subject.id %>"> +
+ <% third_subject.children.each do |fourth_subject| %> +
+
+ + +
" data-bs-parent="#fourthLevelPanel<%= fourth_subject.id %>"> +
+ <% fourth_subject.children.each do |fifth_subject| %> +
+
+
+ <%= link_to fifth_subject.name, subject_index_path(fifth_subject.id), class: "accordion-link" %> +
+
+
+ <% end %> +
+
+
+
+ <% end %> +
+
+
+
+ <% end %> +
+
+
+
+<% end %> diff --git a/app/components/subject_index_form_component.rb b/app/components/subject_index_form_component.rb new file mode 100644 index 0000000..51b4df3 --- /dev/null +++ b/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 + \ No newline at end of file diff --git a/app/components/subject_index_form_component/subject_index_form_component.html.erb b/app/components/subject_index_form_component/subject_index_form_component.html.erb new file mode 100644 index 0000000..691a98d --- /dev/null +++ b/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| %> +
+
+
+ <%= form.text_field :name, class: "form-control" %> + <%= label_tag :name %> +
+
+ + <% if parent_id.present? %> +
+
+ <%= form.text_field :parent, class: "form-control" %> + <%= label_tag :parent %> +
+
+ <% end %> +
+ +
+
+ <%= submit_tag "Save", class: "btn btn-primary text-dark" %> +
+
+<% end %> \ No newline at end of file diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 6b4dcfa..1b89883 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,3 +1,7 @@ class ApplicationController < ActionController::Base 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 diff --git a/app/controllers/subject_indexes_controller.rb b/app/controllers/subject_indexes_controller.rb index 7fa9863..8d2e948 100644 --- a/app/controllers/subject_indexes_controller.rb +++ b/app/controllers/subject_indexes_controller.rb @@ -1,6 +1,6 @@ -# frozen_string_literal: true - class SubjectIndexesController < ApplicationController + load_and_authorize_resource :subject_index, class: "Cdao::Subject" + def index; end def new; end @@ -9,9 +9,68 @@ class SubjectIndexesController < ApplicationController 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 diff --git a/app/models/ability.rb b/app/models/ability.rb index 2d804bb..8b6c13c 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -9,6 +9,8 @@ class Ability if user.persisted? can :display, Cdao::Jurisprudence + + can :manage, :all end end diff --git a/app/models/cdao/subject.rb b/app/models/cdao/subject.rb index 6bb402f..d4d8354 100644 --- a/app/models/cdao/subject.rb +++ b/app/models/cdao/subject.rb @@ -47,4 +47,13 @@ class Cdao::Subject < Cdao::Base [name, "(#{product_names})"].join(" ") end + + searchable do + integer :parent_id + + text :name + + string :name + string :state + end end diff --git a/app/views/subject_indexes/edit.html.erb b/app/views/subject_indexes/edit.html.erb index e69de29..7165db6 100644 --- a/app/views/subject_indexes/edit.html.erb +++ b/app/views/subject_indexes/edit.html.erb @@ -0,0 +1,11 @@ +
+
+
+

Edit Subject Index

+ +
+ <%= 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 })) %> +
+
+
+
diff --git a/app/views/subject_indexes/index.html.erb b/app/views/subject_indexes/index.html.erb index e69de29..81f4a83 100644 --- a/app/views/subject_indexes/index.html.erb +++ b/app/views/subject_indexes/index.html.erb @@ -0,0 +1,26 @@ +
+
+
+

Subject Indexes

+ +
+ <% Cdao::Subject.roots.all.each do |subject| %> +
+
+ + +
" data-bs-parent="#mainPanel<%= subject.id %>"> +
+ + <%= render(SubjectAccordionComponent.new(current_user: current_user, parent: subject)) %> +
+
+
+
+ <% end %> +
+
+
+
diff --git a/app/views/subject_indexes/new.html.erb b/app/views/subject_indexes/new.html.erb index e69de29..335573b 100644 --- a/app/views/subject_indexes/new.html.erb +++ b/app/views/subject_indexes/new.html.erb @@ -0,0 +1,11 @@ +
+
+
+

New Subject Index

+ +
+ <%= 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] })) %> +
+
+
+
diff --git a/app/views/subject_indexes/show.html.erb b/app/views/subject_indexes/show.html.erb index e69de29..f6c7ecd 100644 --- a/app/views/subject_indexes/show.html.erb +++ b/app/views/subject_indexes/show.html.erb @@ -0,0 +1,23 @@ +
+
+
+
+

<%= @subject_index.name %> + <% if can? :destroy, Cdao::Subject %> + + + + <% end %> + <% if can? :update, Cdao::Subject %> + + + + <% end %> +

+
+
+ <%= render(SubjectAccordionComponent.new(current_user: current_user, parent: @subject_index)) %> +
+
+
+
\ No newline at end of file