Skip to content

Commit

Permalink
Merge pull request #254 from ontoportal-lirmm/feature/add-ontology-co…
Browse files Browse the repository at this point in the history
…unt-to-group-and-category

Feature: add column ontologies count to categories and-groups admin page
  • Loading branch information
syphax-bouazzouni committed Sep 5, 2023
1 parent 69af592 commit 0121b16
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 25 deletions.
28 changes: 23 additions & 5 deletions app/assets/javascripts/bp_admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -1303,8 +1303,14 @@ function displayGroups(data, group) {
"targets": 4,
"searchable": false,
"orderable": false,
"title": "Actions",
"width": "210px"
"title": "Count",
},
{
"targets": 5,
"searchable": false,
"orderable": false,
"title": "Actions",
"width": "200px"
}
],
"autoWidth": false,
Expand Down Expand Up @@ -1333,10 +1339,13 @@ function populateGroupRows(data) {
let description = group['description']
let created = group['created'];
let id = group['acronym'];
let nb = [
'<a href="/ontologies?groups='+id+'" target="_blank"> ' + group['ontologies'].length + ' </a>',
];
let actions = [
'<a href="javascript:;" class="edit-group mx-1" data-group-name="' + id + '">Edit</a>',
]
return [name, description, created, id , actions.join('|')];
return [name, description, created, id , nb, actions.join('|')];
})

return allRows;
Expand Down Expand Up @@ -1456,8 +1465,14 @@ function displayCategories(data, category) {
"targets": 5,
"searchable": false,
"orderable": false,
"title": "Count",
},
{
"targets": 6,
"searchable": false,
"orderable": false,
"title": "Actions",
"width": "210px"
"width": "250px"
}
],
"autoWidth": false,
Expand Down Expand Up @@ -1487,10 +1502,13 @@ function populateCategoryRows(data) {
let created = category['created'];
let id = category['acronym'];
let parentCategory = category['parentCategory'];
let nb = [
'<a href="/ontologies?categories='+id+'" target="_blank"> ' + category['ontologies'].length + ' </a>',
];
let actions = [
'<a href="javascript:;" class="edit-category mx-1" data-category-name="' + id + '">Edit</a>',
]
return [name, description, created, id , parentCategory, actions.join('|')];
return [name, description, created, id , parentCategory, nb , actions.join('|')];
})

return allRows;
Expand Down
3 changes: 2 additions & 1 deletion app/components/select_input_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

class SelectInputComponent < ViewComponent::Base

def initialize(id:, name:, values:, selected:, multiple: false)
def initialize(id:, name:, values:, selected:, multiple: false, open_to_add_values: false)
super
@id = id
@name = name
@values = values
@selected = selected
@multiple = multiple
@open_to_add_values = open_to_add_values
end

def options_values
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
%div{:data => { controller: "select-input", 'select-input': {'multiple-value': @multiple.to_s}}}
= select_tag(@name, options_values, { multiple: @multiple, class: "form-control", id: "select_#{@id}", data: { action: "select-input#toggleOtherValue", "select-input-target": "selectedValues" } })

%div.d-flex.mt-1
%div.d-flex.mt-1{style: "display:#{@open_to_add_values ? 'none !important;' : 'block;'}"}
= text_field_tag("add_#{@id}", nil, :style => "margin-right: 1em;width: 16em;display: none;", :placeholder => "Or provide the value",
data: {action: "keydown.enter->select-input#addValue", "select-input-target": "inputValueField"}, class: 'metadataInput form-control form-control-sm')

Expand Down
15 changes: 9 additions & 6 deletions app/controllers/admin/categories_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
class Admin::CategoriesController < ApplicationController
include SubmissionUpdater

layout :determine_layout
before_action :unescape_id, only: [:edit, :show, :update, :destroy]
before_action :authorize_admin

CATEGORIES_URL = "#{LinkedData::Client.settings.rest_url}/categories"
ATTRIBUTE_TO_INCLUDE = 'name,acronym,created,description,parentCategory,ontologies'

def index
response = _categories
Expand All @@ -20,8 +22,8 @@ def new
end

def edit
@category = LinkedData::Client::Models::Category.find_by_acronym(params[:id]).first

@category = LinkedData::Client::Models::Category.find_by_acronym(params[:id], include:'name,acronym,created,description,parentCategory,ontologies' ).first
@ontologies_category = LinkedData::Client::Models::Ontology.all(include: 'acronym').map {|o|[o.acronym, o.id] }
respond_to do |format|
format.html { render "edit", :layout => false }
end
Expand Down Expand Up @@ -49,10 +51,11 @@ def update
response = { errors: '', success: ''}
start = Time.now
begin
category = LinkedData::Client::Models::Category.find_by_acronym(params[:id]).first
category = LinkedData::Client::Models::Category.find_by_acronym(params[:id], include: ATTRIBUTE_TO_INCLUDE ).first
add_ontologies_to_object(category_params[:ontologies],category) if (category_params[:ontologies].size > 0 && category_params[:ontologies].first != '')
delete_ontologies_from_object(category_params[:ontologies],category.ontologies,category)
category.update_from_params(category_params)
category_update = category.update

if response_error?(category_update)
response[:errors] = response_errors(category_update)
else
Expand Down Expand Up @@ -89,14 +92,14 @@ def unescape_id
end

def category_params
params.require(:category).permit(:acronym, :name, :description, :parentCategory).to_h
params.require(:category).permit(:acronym, :name, :description, :parentCategory, {ontologies:[]}).to_h
end

def _categories
response = { categories: Hash.new, errors: '', success: '' }
start = Time.now
begin
response[:categories] = JSON.parse(LinkedData::Client::HTTP.get(CATEGORIES_URL, { include: 'all' }, raw: true))
response[:categories] = JSON.parse(LinkedData::Client::HTTP.get(CATEGORIES_URL, { include: ATTRIBUTE_TO_INCLUDE }, raw: true))

response[:success] = "categories successfully retrieved in #{Time.now - start}s"
LOG.add :debug, "Categories - retrieved #{response[:categories].length} groups in #{Time.now - start}s"
Expand Down
8 changes: 6 additions & 2 deletions app/controllers/admin/groups_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Admin::GroupsController < ApplicationController
include SubmissionUpdater

layout :determine_layout
before_action :unescape_id, only: [:edit, :show, :update, :destroy]
Expand All @@ -21,7 +22,8 @@ def new

def edit
@group = LinkedData::Client::Models::Group.find_by_acronym(params[:id]).first

@acronyms = @group.ontologies.map { |url| url.match(/\/([^\/]+)$/)[1] }
@ontologies_group = LinkedData::Client::Models::Ontology.all(include: 'acronym').map {|o|[o.acronym, o.id] }
respond_to do |format|
format.html { render "edit", :layout => false }
end
Expand Down Expand Up @@ -50,6 +52,8 @@ def update
start = Time.now
begin
group = LinkedData::Client::Models::Group.find_by_acronym(params[:id]).first
add_ontologies_to_object(group_params[:ontologies],group) if (group_params[:ontologies].size > 0 && group_params[:ontologies].first != '')
delete_ontologies_from_object(group_params[:ontologies],group.ontologies,group)
group.update_from_params(group_params)
group_updated = group.update
if response_error?(group_updated)
Expand Down Expand Up @@ -88,7 +92,7 @@ def unescape_id
end

def group_params
params.require(:group).permit(:acronym, :name, :description).to_h()
params.require(:group).permit(:acronym, :name, :description, {ontologies:[]}).to_h()
end

def _groups
Expand Down
26 changes: 26 additions & 0 deletions app/controllers/concerns/submission_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,32 @@ def update_submission(new_submission_hash)
@submission.update(cache_refresh_all: false)
end

def add_ontologies_to_object(ontologies,object)
ontologies.each do |ont|
next if object.ontologies.include?(ont)
ontology = LinkedData::Client::Models::Ontology.find(ont)
if object.type.match(/\/([^\/]+)$/)[1] == 'Group'
ontology.group.push(object.id)
else
ontology.hasDomain.push(object.id)
end
ontology.update
end
end

def delete_ontologies_from_object(new_ontologies,old_ontologies,object)
ontologies = old_ontologies - new_ontologies
ontologies.each do |ont|
ontology = LinkedData::Client::Models::Ontology.find(ont)
if object.type.match(/\/([^\/]+)$/)[1] == 'Group'
ontology.group.delete(object.id)
else
ontology.hasDomain.delete(object.id)
end
ontology.update
end
end

private

def update_ontology_summary_only
Expand Down
14 changes: 10 additions & 4 deletions app/views/admin/categories/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
%col{style: "width: 100%"}
%tr
%th
Acronym:
Acronym
%span.asterik *
%td.top
- if new_record
Expand All @@ -23,21 +23,27 @@
= f.text_field :acronym, class: "form-control", readonly: true
%tr
%th
Name:
Name
%span.asterik *
%td.top
= f.text_field :name, class: "form-control"
%tr
%th
Description:
Description
%td.top
= f.text_area :description, class: "form-control"
- unless new_record
%tr
%th
Created:
Created
%td.top
= f.text_field :created, readonly: true, class: "form-control"
%tr
%th
Ontologies
%td.top
= render SelectInputComponent.new(id: "category_ontologies", name: "category[ontologies]", values: @ontologies_category, selected: @category.ontologies, multiple: true, open_to_add_values: true)
%div.d-flex.mt-1{:style => "display: none;"}
%div.mt-2
= f.submit button_text, class: "btn btn-primary mr-sm-2 group-form-accept"
= link_to "Cancel", "javascript:;", class: "btn btn-primary dismiss-dialog"
2 changes: 1 addition & 1 deletion app/views/admin/categories/edit.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
%div
= form_for :category, url: admin_categories_path + "/" + escape(@category.acronym), method: "PATCH", remote: true, data: { collection: "categories"}, html: {class: "admin-collection-form" } do |f|
= render partial: "form", locals: {f: f, title_text: "Edit category",
button_text: "Edit category", button_class: "edit-category"}
button_text: "Save", button_class: "edit-category"}
13 changes: 9 additions & 4 deletions app/views/admin/groups/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
%col{style: "width: 100%"}
%tr
%th
Acronym:
Acronym
%span.asterik *
%td.top
- if new_record
Expand All @@ -23,21 +23,26 @@
= f.text_field :acronym, class: "form-control", readonly: true
%tr
%th
Name:
Name
%span.asterik *
%td.top
= f.text_field :name, class: "form-control"
%tr
%th
Description:
Description
%td.top
= f.text_area :description, class: "form-control"
- unless new_record
%tr
%th
Created:
Created
%td.top
= f.text_field :created, readonly: true, class: "form-control"
%tr
%th
Ontologies
%td.top
= render SelectInputComponent.new(id: "group_ontologies", name: "group[ontologies]", values: @ontologies_group , selected: @group.ontologies , multiple: true, open_to_add_values: true)
%div.mt-2
= f.submit button_text, class: "btn btn-primary mr-sm-2 group-form-accept"
= link_to "Cancel", "javascript:;", class: "btn btn-primary dismiss-dialog"
2 changes: 1 addition & 1 deletion app/views/admin/groups/edit.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
%div
= form_for :group, url: admin_groups_path + "/" + escape(@group.acronym), method: "PATCH", remote: true, data: { collection: "groups"}, html: {class: "admin-collection-form" } do |f|
= render partial: "form", locals: {f: f, title_text: "Edit group",
button_text: "Edit group", button_class: "edit-group"}
button_text: "Save", button_class: "edit-group"}

0 comments on commit 0121b16

Please sign in to comment.