diff options
author | hellekin <hellekin@cepheide.org> | 2020-10-09 12:48:13 +0200 |
---|---|---|
committer | hellekin <hellekin@cepheide.org> | 2020-10-09 12:50:16 +0200 |
commit | 383a840c50ca8f6df19677e93357e9dc6c0e043c (patch) | |
tree | 3597f5ee2a92b551a01ac207f440ae85bcb3d9c6 | |
parent | dd9e0691acae3eb63bdfbc8665eb3f8ce516536d (diff) | |
download | incommon-map-383a840c50ca8f6df19677e93357e9dc6c0e043c.tar.gz |
Enable switching Agent
Switching current Agent can help with roles and resource access.
Go to your dashboard by clicking the avatar.
Select agent and hit "Select"
-rw-r--r-- | app/controllers/my/agent_controller.rb | 29 | ||||
-rw-r--r-- | app/helpers/agents_helper.rb | 9 | ||||
-rw-r--r-- | app/views/application/_user_info.html.erb | 4 | ||||
-rw-r--r-- | app/views/welcome/dashboard.html.erb | 8 | ||||
-rw-r--r-- | config/routes.rb | 3 |
5 files changed, 48 insertions, 5 deletions
diff --git a/app/controllers/my/agent_controller.rb b/app/controllers/my/agent_controller.rb new file mode 100644 index 0000000..ac761b7 --- /dev/null +++ b/app/controllers/my/agent_controller.rb @@ -0,0 +1,29 @@ +class My::AgentController < ApplicationController + # PATCH /my/current_agent + def switch + response_status = :unprocessable_entity + new_agent = current_user.agents.find(params[:agent][:id]) + Rails.logger.info "CURRENT_AGENT #{session[:current_agent]} -> #{new_agent.name}" + if new_agent.present? + session[:current_agent] = new_agent.name + @current_agent = new_agent + response_status = :ok + respond_to do |format| + format.html { redirect_to my_dashboard_url, notice: "L'Agent est maintenant #{new_agent.name}." } + format.json { render json: { current_agent: session[:current_agent] }, status: response_status } + end + else + respond_to do |format| + format.html { redirect_to my_dashboard_url, notice: "L'Agent est toujours le meme..." } + format.json { render json: { current_agent: session[:current_agent] }, status: response_status } + end + end + + end + + private + + def agent_params + params.require(:agent).permit(:id) + end +end diff --git a/app/helpers/agents_helper.rb b/app/helpers/agents_helper.rb new file mode 100644 index 0000000..a02fb7a --- /dev/null +++ b/app/helpers/agents_helper.rb @@ -0,0 +1,9 @@ +module AgentsHelper + # Form to select an Agent + def agent_selector_form + form_for current_agent, url: '/my/current_agent', method: :patch do |f| + f.submit('select') + f.select(:id, options_for_select(current_user.agents.map { |a| [a.name, a.id] }), selected: current_agent.id) + end + end +end diff --git a/app/views/application/_user_info.html.erb b/app/views/application/_user_info.html.erb index 3220c89..be3b6c5 100644 --- a/app/views/application/_user_info.html.erb +++ b/app/views/application/_user_info.html.erb @@ -3,11 +3,11 @@ <% if current_user.present? %> <a href="/my/dashboard"><%= image_tag current_user.avatar_url, alt: current_user.username %></a> <ul> - <%= tag.li(link_to(new_agent_switch_url, method: :get, remote: true, title: "Changer d'Agent") do + <%#= tag.li(link_to(new_agent_switch_url, method: :get, remote: true, title: "Changer d'Agent") do "Agent: %s" % current_agent end, id: 'current_agent_name') %> + <%= tag.li agent_selector_form %> <%= tag.li link_to("Ajouter un point", new_agent_resource_url(agent_id: current_agent)) %> - <%= tag.li link_to("Mon compte", account_url(current_user.id)) %> <%= tag.li link_to("Terminer la session", logout_url) %> </ul> <% else %> diff --git a/app/views/welcome/dashboard.html.erb b/app/views/welcome/dashboard.html.erb index 8880434..242fc58 100644 --- a/app/views/welcome/dashboard.html.erb +++ b/app/views/welcome/dashboard.html.erb @@ -1,6 +1,12 @@ <h1>Welcome <%= h current_user.name %>!</h1> -<p>Current Agent: <%= current_agent&.name %></p> +<h3>Mes Agents</h3> + +<p><%= form_for current_agent, url: '/my/current_agent', method: :patch do |f| %> + En cours : <%= f.submit('select') %> + <%= f.select(:id, options_for_select(current_user.agents.map { |a| [a.name, a.id] }), selected: current_agent.id) %> +<% end %></p> + <section id="global_stats"> <h2>Global Count</h2> diff --git a/config/routes.rb b/config/routes.rb index fdecb3d..0de14b5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -19,8 +19,7 @@ Rails.application.routes.draw do # User routes get '/my/account', to: 'users#show', as: 'account' - get '/my/current_agent', to: 'agent_switch#new', as: 'new_agent_switch' - put '/my/current_agent', to: 'agent_switch#update', as: 'update_agent_switch' + patch '/my/current_agent', to: 'my/agent#switch', as: 'agent_switch' get '/my/dashboard', to: 'welcome#dashboard' get 'my/users', to: 'users#index', as: 'users' |