Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ontoportal align: Add groups and categories administration #14

532 changes: 531 additions & 1 deletion app/assets/javascripts/bp_admin.js

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions app/assets/stylesheets/admin.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
.error {
background: #ffecec image-url("error.png") no-repeat 10px 50%;
border: 1px solid #f5aca6;
padding-left: 30px;
}

.success {
Expand Down Expand Up @@ -139,3 +140,8 @@ table.dataTable tbody tr.selected {
margin-right: 8px;
}
}

div.groupFormError {
color: red;
padding-top: 3px;
}
108 changes: 108 additions & 0 deletions app/controllers/admin/categories_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
class Admin::CategoriesController < ApplicationController

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

CATEGORIES_URL = "#{LinkedData::Client.settings.rest_url}/categories"

def index
response = _categories
render :json => response
end

def new
@category = LinkedData::Client::Models::Category.new

respond_to do |format|
format.html { render "new", :layout => false }
end
end

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

respond_to do |format|
format.html { render "edit", :layout => false }
end
end

def create
response = { errors: '', success: '' }
start = Time.now
begin
category = LinkedData::Client::Models::Category.new(values: category_params)
category_saved = category.save
if response_error?(category_saved)
response[:errors] = response_errors(category_saved)
else
response[:success] = "category successfully created in #{Time.now - start}s"
end
rescue Exception => e
response[:errors] = "Problem creating the category - #{e.message}"
end
render json: response, status: (response[:errors] == '' ? :created : :internal_server_error)

end

def update
response = { errors: '', success: ''}
start = Time.now
begin
category = LinkedData::Client::Models::Category.find_by_acronym(params[:id]).first
category.update_from_params(category_params)
category_update = category.update

if response_error?(category_update)
response[:errors] = response_errors(category_update)
else
response[:success] = "category successfully updated in #{Time.now - start}s"
end
rescue Exception => e
response[:errors] = "Problem updating the category - #{e.message}"
end
render json: response, status: (response[:errors] == '' ? :ok : :internal_server_error)
end

def destroy
response = { errors: '', success: ''}
start = Time.now
begin
category = LinkedData::Client::Models::Category.find_by_acronym(params[:id]).first
error_response = category.delete

if response_error?(error_response)
response[:errors] = response_errors(error_response)
else
response[:success] = "category successfully deleted in #{Time.now - start}s"
end
rescue Exception => e
response[:errors] = "Problem deleting the category - #{e.message}"
end
render json: response, status: (response[:errors] == '' ? :ok : :internal_server_error)
end

private

def unescape_id
params[:id] = CGI.unescape(params[:id])
end

def category_params
params.require(:category).permit(:acronym, :name, :description, :parentCategory).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[:success] = "categories successfully retrieved in #{Time.now - start}s"
LOG.add :debug, "Categories - retrieved #{response[:categories].length} groups in #{Time.now - start}s"
rescue Exception => e
response[:errors] = "Problem retrieving categories - #{e.message}"
end
response
end
end
107 changes: 107 additions & 0 deletions app/controllers/admin/groups_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
class Admin::GroupsController < ApplicationController

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

GROUPS_URL = "#{LinkedData::Client.settings.rest_url}/groups"

def index
response = _groups
render :json => response
end

def new
@group = LinkedData::Client::Models::Group.new

respond_to do |format|
format.html { render "new", :layout => false }
end
end

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

respond_to do |format|
format.html { render "edit", :layout => false }
end
end

def create
response = { errors: '', success: '' }
start = Time.now
begin
group = LinkedData::Client::Models::Group.new(values: group_params)
group_saved = group.save
if response_error?(group_saved)
response[:errors] = response_errors(group_saved)
else
response[:success] = "group successfully created in #{Time.now - start}s"
end
rescue Exception => e
response[:errors] = "Problem creating the group - #{e.message}"
end
render json: response, status: (response[:errors] == '' ? :created : :internal_server_error)

end

def update
response = { errors: '', success: ''}
start = Time.now
begin
group = LinkedData::Client::Models::Group.find_by_acronym(params[:id]).first
group.update_from_params(group_params)
group_updated = group.update
if response_error?(group_updated)
response[:errors] = response_errors(group_updated)
else
response[:success] = "group successfully updated in #{Time.now - start}s"
end
rescue Exception => e
response[:errors] = "Problem updating the group - #{e.message}"
end
render json: response, status: (response[:errors] == '' ? :ok : :internal_server_error)
end

def destroy
response = { errors: '', success: ''}
start = Time.now
begin
group = LinkedData::Client::Models::Group.find_by_acronym(params[:id]).first
error_response = group.delete

if response_error?(error_response)
response[:errors] = response_errors(error_response)
else
response[:success] = "group successfully deleted in #{Time.now - start}s"
end
rescue Exception => e
response[:errors] = "Problem deleting the group - #{e.message}"
end
render json: response, status: (response[:errors] == '' ? :ok : :internal_server_error)
end

private

def unescape_id
params[:id] = CGI.unescape(params[:id])
end

def group_params
params.require(:group).permit(:acronym, :name, :description).to_h()
end

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

response[:success] = "groups successfully retrieved in #{Time.now - start}s"
LOG.add :debug, "Groups - retrieved #{response[:groups].length} groups in #{Time.now - start}s"
rescue Exception => e
response[:errors] = "Problem retrieving groups - #{e.message}"
end
response
end
end
43 changes: 43 additions & 0 deletions app/views/admin/categories/_form.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
- new_record = @category.acronym.nil?
%div.alert-box.error{style: @errors.nil? ? "display: none" : nil }
%ul
- unless @errors.nil?
- for error in @errors
%li
= error
%div{:style => "width:500px"}
%span.asterik{:style => "float:right;"} * fields are required
%h3 #{title_text}
%table#category_form.form
%colgroup
%col
%col{style: "width: 100%"}
%tr
%th
Acronym:
%span.asterik *
%td.top
- if new_record
= f.text_field :acronym, class: "form-control"
- else
= f.text_field :acronym, class: "form-control", readonly: true
%tr
%th
Name:
%span.asterik *
%td.top
= f.text_field :name, class: "form-control"
%tr
%th
Description:
%td.top
= f.text_area :description, class: "form-control"
- unless new_record
%tr
%th
Created:
%td.top
= f.text_field :created, readonly: true, class: "form-control"
%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"
6 changes: 6 additions & 0 deletions app/views/admin/categories/edit.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- @title = "Edit category"

%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"}
6 changes: 6 additions & 0 deletions app/views/admin/categories/new.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- @title = "Create new category"

%div
= form_for :category, url: admin_categories_path, method: "POST", remote: true, data: { collection: "categories"}, html: {class: "admin-collection-form" } do |f|
= render partial: "form", locals: {f: f, title_text: "Create category",
button_text: "Create category" }
43 changes: 43 additions & 0 deletions app/views/admin/groups/_form.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
- new_record = @group.acronym.nil?
%div.alert-box.error{style: @errors.nil? ? "display: none" : nil }
%ul
- unless @errors.nil?
- for error in @errors
%li
= error
%div{:style => "width:500px"}
%span.asterik{:style => "float:right;"} * fields are required
%h3 #{title_text}
%table#group_form.form
%colgroup
%col
%col{style: "width: 100%"}
%tr
%th
Acronym:
%span.asterik *
%td.top
- if new_record
= f.text_field :acronym, class: "form-control"
- else
= f.text_field :acronym, class: "form-control", readonly: true
%tr
%th
Name:
%span.asterik *
%td.top
= f.text_field :name, class: "form-control"
%tr
%th
Description:
%td.top
= f.text_area :description, class: "form-control"
- unless new_record
%tr
%th
Created:
%td.top
= f.text_field :created, readonly: true, class: "form-control"
%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"
6 changes: 6 additions & 0 deletions app/views/admin/groups/edit.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- @title = "Edit group"

%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"}
6 changes: 6 additions & 0 deletions app/views/admin/groups/new.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- @title = "Create new group"

%div
= form_for :group, url: admin_groups_path, method: "POST", remote: true, data: { collection: "groups"}, html: {class: "admin-collection-form" } do |f|
= render partial: "form", locals: {f: f, title_text: "Create group",
button_text: "Create group" }
17 changes: 16 additions & 1 deletion app/views/admin/index.html.haml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
- @title = "BioPortal Administration"
- @title = "Administration"

%div.row
%div.col
Expand All @@ -25,6 +25,10 @@
=link_to("Users", "#users", id: "users-admin-tab", class: "nav-link", role: "tab", data: { toggle: "tab", href: users_path() }, aria: { controls: "users", selected: "false" })
%li.nav-item
=link_to("Metadata Administration", "#ontologies_metadata_curator", id: "ontologies_metadata_curator-admin-tab", class: "nav-link", role: "tab", data: { toggle: "tab"}, aria: { controls: "ontologies_metadata_curator", selected: "false" })
%li.nav-item
=link_to("Groups", "#groups", id: "groups-admin-tab", class: "nav-link", role: "tab", data: { toggle: "tab", href: "groups" }, aria: { controls: "groups", selected: "false" })
%li.nav-item
=link_to("Categories", "#categories", id: "categories-admin-tab", class: "nav-link", role: "tab", data: { toggle: "tab", href: "categories" }, aria: { controls: "categories", selected: "false" })
%div#adminTabContent.tab-content

-# Site Administration tab
Expand Down Expand Up @@ -95,6 +99,17 @@
%div.tab-pane.fade{id: "ontologies_metadata_curator", role: "tabpanel", aria: { labelledby: "ontologies_metadata_curator-admin-tab" }}
= render partial: 'ontologies_metadata_curator/metadata_tab'

-# Groups tab
%div.tab-pane.fade{id: "groups", role: "tabpanel", aria: { labelledby: "groups-admin-tab" }}
%div.ontologies_list_container.mt-3
%table#adminGroups.zebra{:cellpadding => "0", :cellspacing => "0", :width => "100%"}

-# Categories tab
%div.tab-pane.fade{id: "categories", role: "tabpanel", aria: { labelledby: "categories-admin-tab" }}
%div.ontologies_list_container.mt-3
%table#adminCategories.zebra{:cellpadding => "0", :cellspacing => "0", :width => "100%"}





2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@

namespace :admin do
resources :licenses, only: [:index, :create, :new]
resources :groups, only: [:index, :create, :new, :edit, :update, :destroy]
resources :categories, only: [:index, :create, :new, :edit, :update, :destroy]
end

resources :subscriptions
Expand Down