Skip to content

Commit

Permalink
Merge pull request #278 from ontoportal-lirmm/feature/add-agent-model…
Browse files Browse the repository at this point in the history
…-ui-components

Feature: Add agent model components and integration
  • Loading branch information
syphax-bouazzouni authored Aug 7, 2023
2 parents c5b6360 + 5db8315 commit 75992a0
Show file tree
Hide file tree
Showing 35 changed files with 648 additions and 63 deletions.
6 changes: 3 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ gem 'view_component', '~> 2.72'
gem 'turnout'
gem 'will_paginate', '~> 3.0'
gem 'inline_svg'

gem "lookbook", '~> 1.5.5'
gem 'ontologies_api_client', git: 'https://github.com/ontoportal-lirmm/ontologies_api_ruby_client.git', branch: 'development'


Expand Down Expand Up @@ -106,7 +106,7 @@ group :development do

# Use console on exceptions pages [https://github.com/rails/web-console]
gem 'web-console'
gem "lookbook", '~> 1.5.5'

end

group :test, :development do
Expand All @@ -123,4 +123,4 @@ end

gem "net-ftp", "~> 0.2.0", require: false
gem "net-http"
gem 'inline_svg'

4 changes: 4 additions & 0 deletions app/assets/stylesheets/agents.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.agents-inputs input {
@extend .form-control !optional;
height: 100% !important;
}
1 change: 1 addition & 0 deletions app/assets/stylesheets/application.css.scss.erb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
@import "login";
@import "components/index";
@import "account";
@import "agents";

@import "nav_bar";

Expand Down
2 changes: 1 addition & 1 deletion app/assets/stylesheets/components/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
@import 'pill_button';
@import "switch";
@import "table";
@import "concept_details";
@import "concept_details";
13 changes: 13 additions & 0 deletions app/components/agent_search_input_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

class AgentSearchInputComponent < ViewComponent::Base

def initialize(id:, agent_type: nil, name_prefix: nil, parent_id: , edit_on_modal: false)
super
@id = id
@agent_type = agent_type
@name_prefix = name_prefix
@parent_id = parent_id
@edit_on_modal = edit_on_modal
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
= render SearchInputComponent.new(name:'agent' + @id, ajax_url: "/ajax/agents?agent_type=#{@agent_type}&name=",
item_base_url:"/agents/#{@id}?parent_id=#{@parent_id}&edit_on_modal=#{@edit_on_modal}&name_prefix=#{@name_prefix}&agent_id=", id_key: 'id',
use_cache: false,
actions_links: {create_new_agent: {link: "/agents/new?name=&id=#{@id}&parent_id=#{@parent_id}&new_agent=false&type=#{@agent_type}&show_affiliations=false&edit_on_modal=#{@edit_on_modal}&name_prefix=#{@name_prefix}[#{@id}]", target:'_self'}}) do |s|
- s.template do
%a{href: "LINK", class: "home-search-ontology-content", 'data-turbo-frame': '_self'}
%p#seached-ontology.home-searched-ontology
NAME (IDENTIFIERS)
%p.home-result-type
TYPE
15 changes: 15 additions & 0 deletions app/components/nested_agent_search_input_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

class NestedAgentSearchInputComponent < ViewComponent::Base

include Turbo::FramesHelper, AgentHelper

def initialize(agents:, agent_type:, name_prefix:, edit_on_modal: false, parent_id: nil)
super
@agents = agents
@agent_type = agent_type
@name_prefix = name_prefix
@edit_on_modal = edit_on_modal
@parent_id = parent_id
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
= render NestedFormInputsComponent.new do |c|
- c.template do
= render TurboFrameComponent.new(id: agent_id_frame_id('NEW_RECORD', @parent_id)) do
= render AgentSearchInputComponent.new(id: 'NEW_RECORD', agent_type: @agent_type, name_prefix: @name_prefix,
parent_id: @parent_id, edit_on_modal: @edit_on_modal)

- c.empty_state do
= hidden_field_tag agent_field_name('', @name_prefix+"[#{Array(@agents).size}]")
- Array(@agents).each_with_index do |agent, i|
- if agent
- c.row do
= render partial: 'agents/agent_show', locals: {agent: agent, name_prefix: @name_prefix+"[#{i}]", edit_on_modal: @edit_on_modal, parent_id: @parent_id}
1 change: 0 additions & 1 deletion app/components/search_input_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def initialize(name: '', placeholder: '', actions_links: {},
@id_key = id_key
@links_target = links_target
end

def action_link_info(value)
if value.is_a?(Hash)
[value[:link] , value[:target]]
Expand Down
2 changes: 1 addition & 1 deletion app/components/select_input_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class SelectInputComponent < ViewComponent::Base

def initialize(id:, name:, values:, selected:, multiple: false, open_to_add_values: true)
def initialize(id:, name:, values:, selected:, multiple: false, open_to_add_values: false)
super
@id = id || ""
@name = name
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
= render FormGroupComponent.new(object: @object, name: @name, method: @method_name, label: @label_text, inline: @inline) do |c|
= render FormGroupComponent.new(object: @object, name: @name, method: @method_name, label: @label_text, inline: @inline, required: @required) do |c|
= text_field c.name, c.method_name , value: c.value, class: "form-control"
- c.help do
= help
Expand Down
173 changes: 173 additions & 0 deletions app/controllers/agents_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
class AgentsController < ApplicationController
include TurboHelper, AgentHelper
before_action :authorize_and_redirect, :only => [:edit, :update, :create, :new]

def index
@agents = LinkedData::Client::Models::Agent.all
end

def show
@agent = LinkedData::Client::Models::Agent.find(params[:agent_id])
not_found("Agent with id #{@agent.id}") if @agent.nil?

@agent_id = params[:id] || agent_id(@agent)
@name_prefix = params[:name_prefix] ? "#{params[:name_prefix]}[#{params[:id]}]" : ''
@edit_on_modal = params[:edit_on_modal]&.eql?('true')
end

def ajax_agents
filters = { name: params[:name] }
filters[:agentType] = params[:agent_type] if params[:agent_type]
@agents = LinkedData::Client::Models::Agent.all(filters)
agents_json = @agents.map do |x|
{
id: x.id,
name: x.name,
type: x.agentType,
identifiers: x.identifiers.map { |i| "#{i.schemaAgency}:#{i.notation}" }.join(', ')
}
end

render json: agents_json
end

def new
@agent = LinkedData::Client::Models::Agent.new
@agent.id = params[:id]
@agent.creator = session[:user].id
@agent.agentType = params[:type] || 'person'
@agent.name = params[:name]
@new_agent = params[:new_agent].nil? || params[:new_agent].eql?('true')
@name_prefix = params[:name_prefix] || ''
@show_affiliations = params[:show_affiliations]&.eql?('true')
end

def create
new_agent = save_agent(agent_params)
parent_id = params[:parent_id]
name_prefix = params[:name_prefix]
alert_id = agent_id_alert_container_id(params[:id], parent_id)

if new_agent.errors
render_turbo_stream alert_error(id: alert_id) { JSON.pretty_generate(response_errors(new_agent)) }
else
success_message = 'New Agent added successfully'
streams = [alert_success(id: alert_id) { success_message }]

streams << prepend('agents_table_content', partial: 'agents/show_line', locals: { agent: new_agent })

streams << replace_agent_form(new_agent, frame_id: params[:id], parent_id: parent_id, name_prefix: name_prefix) if params[:parent_id]

render_turbo_stream(*streams)
end
end

def edit
@agent = LinkedData::Client::Models::Agent.find("#{REST_URI}/Agents/#{params[:id]}")
@name_prefix = params[:parent_id] || ''
@show_affiliations = params[:show_affiliations].nil? || params[:show_affiliations].eql?('true')
end

def update
agent_update, agent = update_agent(params[:id].split('/').last, agent_params)
parent_id = params[:parent_id]
alert_id = agent_alert_container_id(agent, parent_id)

if response_error?(agent_update)
render_turbo_stream(alert_error(id: alert_id) { JSON.pretty_generate(response_errors(agent_update)) })
else
success_message = 'Agent successfully updated'
table_line_id = agent_table_line_id(agent_id(agent))

streams = [alert_success(id: alert_id) { success_message },
replace(table_line_id, partial: 'agents/show_line', locals: { agent: agent })
]

streams << replace_agent_form(agent, parent_id: parent_id) if params[:parent_id]

render_turbo_stream(*streams)
end
end

def destroy
error = nil
@agent = LinkedData::Client::Models::Agent.find("#{REST_URI}/Agents/#{params[:id]}")
success_text = ''

if @agent.nil?
success_text = "Agent #{params[:id]} already deleted"
else
error_response = @agent.delete

if response_error?(error_response)
error = response_errors(error_response)
else
success_text = "Agent #{params[:id]} deleted successfully"
end
end

respond_to do |format|
format.turbo_stream do
if error.nil?
render turbo_stream: [
alert(type: 'success') { success_text },
turbo_stream.remove(agent_table_line_id(params[:id]))
]

else
render alert(type: 'danger') { error }
end
end
format.html { render json: { success: success_text, error: error } }
end

end

private

def replace_agent_form(agent, frame_id: nil, parent_id:, partial: 'agents/agent_show', name_prefix: '')

frame_id = frame_id ? agent_id_frame_id(frame_id, parent_id) : agent_frame_id(agent, parent_id)

replace(frame_id, partial: partial,
locals: { agent: agent, name_prefix: name_prefix, parent_id: parent_id, edit_on_modal: false })
end

def save_agent(params)
agent = LinkedData::Client::Models::Agent.new(values: params)
agent.creator = session[:user].id
agent.save
end

def update_agent(id = params[:id], params)
agent = LinkedData::Client::Models::Agent.find("#{REST_URI}/Agents/#{id}")

params[:creator] = session[:user].id if (agent.creator.nil? || agent.creator.empty?) && (params[:creator] || '').empty?

res = agent.update(values: params)
[res, agent.update_from_params(params)]
end

def agent_params
p = params.permit(:agentType, :name, :email, :acronym, :homepage, :creator,
{ identifiers: [:notation, :schemaAgency, :creator] },
{ affiliations: [:id, :agentType, :name, :homepage, :acronym, :creator, { identifiers: [:notation, :schemaAgency, :creator] }] }
)
p = p.to_h
p.transform_values do |v|
if v.is_a? Hash
v.values.reject(&:empty?)
elsif v.is_a? Array
v.reject(&:empty?)
else
v
end
end
p[:identifiers] = (p[:identifiers] || {}).values
p[:affiliations] = (p[:affiliations] || {}).values
p[:affiliations].each do |affiliation|
affiliation[:identifiers] = affiliation[:identifiers].values if affiliation.is_a?(Hash) && affiliation[:identifiers]
end
p
end
end
25 changes: 20 additions & 5 deletions app/controllers/concerns/submission_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ def update_submission(new_submission_hash)
@ontology = LinkedData::Client::Models::Ontology.find_by_acronym(new_submission_hash[:ontology]).first
@submission = @ontology.explore.submissions({ display: 'all' }, new_submission_hash[:id])

@submission.update_from_params(submission_params(new_submission_hash))
new_values = submission_params(new_submission_hash)
new_values.each do |key, values|
@submission.send("#{key}=", values)
rescue StandardError
next
end

update_ontology_summary_only
@submission.update(cache_refresh_all: false)
@submission.update(values: new_values, cache_refresh_all: false)
end

def add_ontologies_to_object(ontologies,object)
Expand Down Expand Up @@ -52,8 +57,8 @@ def delete_ontologies_from_object(new_ontologies,old_ontologies,object)

private

def update_ontology_summary_only
@ontology.summaryOnly = @submission.isRemote.eql?('3')
def update_ontology_summary_only(is_remote = @submission.isRemote)
@ontology.summaryOnly = is_remote&.eql?('3')
@ontology.update
end

Expand Down Expand Up @@ -117,7 +122,7 @@ def submission_params(params)
end
end
p = params.permit(attributes.uniq)
p.to_h.transform_values do |v|
p = p.to_h.transform_values do |v|
if v.is_a? Hash
v.values.reject(&:empty?)
elsif v.is_a? Array
Expand All @@ -126,5 +131,15 @@ def submission_params(params)
v
end
end

submission_metadata.each do |m|
m_attr = m['attribute'].to_sym
if p[m_attr] && m['enforce'].include?('list')
p[m_attr] = Array(p[m_attr]) unless p[m_attr].is_a?(Array)
p[m_attr] = p[m_attr].map { |x| x.is_a?(Hash) ? x.values.reject(&:empty?) : x.reject(&:empty?) }.flatten.uniq if m['enforce'].include?('Agent')
end
end

p
end
end
Loading

0 comments on commit 75992a0

Please sign in to comment.