Skip to content
Snippets Groups Projects
Commit b11de5c5 authored by Mateus Rambo Strey's avatar Mateus Rambo Strey
Browse files

add and remove users from institution in management

parent 7de58652
No related branches found
No related tags found
No related merge requests found
...@@ -14,13 +14,12 @@ ...@@ -14,13 +14,12 @@
//= require jquery_ujs //= require jquery_ujs
//= require turbolinks //= require turbolinks
//= require jquery.turbolinks //= require jquery.turbolinks
//= require bootstrap-sprockets
// www.chartjs.org/docs/
//= require Chart //= require Chart
//= require locastyle //= require locastyle
//= require_tree ./management //= require_tree ./management
//mais infos: www.chartjs.org/docs/
var visualised,non_visualised; var visualised,non_visualised;
$(document).ready(function(){ $(document).ready(function(){
......
$ ->
$(document).on 'click', '*[data-institution]', ->
e = $(this)
e.popover({content: "Carregando..."}).popover('show')
all = undefined
objects = undefined
institution_id = $(this).data('institution')
url = '/institutions/' + institution_id + '/users.json'
$.get '/users/list.json', (d) ->
all = d
$.get url, (d) ->
object = d
# generate a lookup table for object's collections id
lookup = {}
i = 0
len = object.length
while i < len
lookup[object[i].id] = object[i]
i++
# mark checked objects
i = 0
len = all.length
html = ''
while i < len
name = 'user_' + all[i].rid.split(':').slice(-1)[0]
html += '<input type="checkbox" class="institutional-user-element" data-uid="' + all[i].id + '" data-institution-id="' + institution_id + '" id="' + name + '"'
if lookup[all[i].id] != undefined
html += " checked"
html += '>'
html += '<label for="' + name + '">' + all[i].name + '</label><br/>'
++i
$('.popover-content').html(html)
return
return
return
$ ->
$(document).on 'click', 'input[class=institutional-user-element]', ->
url = '/management/institutions/' + encodeURIComponent($(this).data('institution-id')) + '/user/' + encodeURIComponent($(this).data('uid'))
if this.checked
$.ajax {method: "POST", url: url }, (d) ->
d
else
$.ajax {method: "DELETE", url: url }, (d) ->
d
return
...@@ -14,3 +14,6 @@ ...@@ -14,3 +14,6 @@
*= require locastyle *= require locastyle
*= require_self *= require_self
*/ */
@import "bootstrap-sprockets";
@import "bootstrap";
class InstitutionsController < ApplicationController class InstitutionsController < ApplicationController
before_action :set_institution, only: [:show, :edit, :update, :destroy, :like] before_action :set_institution, only: [:show, :edit, :update, :destroy, :like, :users]
# GET /institutions # GET /institutions
# GET /institutions.json # GET /institutions.json
...@@ -57,10 +57,14 @@ class InstitutionsController < ApplicationController ...@@ -57,10 +57,14 @@ class InstitutionsController < ApplicationController
end end
end end
def users
@users = institution_repository.associated_with @institution
end
private private
def set_institution def set_institution
@institution = institution_repository.find("##{params[:id]}") @institution = institution_repository.find(params[:id])
end end
# Never trust parameters from the scary internet, only allow the white list through. # Never trust parameters from the scary internet, only allow the white list through.
......
class Management::InstitutionsController < ManagementController class Management::InstitutionsController < ManagementController
before_action :set_institution, only: [:show, :edit, :update, :destroy] before_action :set_institution, only: [:show, :edit, :update, :destroy, :add_user, :remove_user]
# GET /management/institutions # GET /management/institutions
# GET /management/institutions.json # GET /management/institutions.json
...@@ -28,7 +28,7 @@ class Management::InstitutionsController < ManagementController ...@@ -28,7 +28,7 @@ class Management::InstitutionsController < ManagementController
respond_to do |format| respond_to do |format|
if institution_repository.create @institution if institution_repository.create @institution
format.html { redirect_to [:management, @institution], notice: 'Institution was successfully created.' } format.html { redirect_to [:management, @institution], notice: 'Instituição criada com sucesso.' }
format.json { render :show, status: :created, location: @institution } format.json { render :show, status: :created, location: @institution }
else else
format.html { render :new } format.html { render :new }
...@@ -37,12 +37,32 @@ class Management::InstitutionsController < ManagementController ...@@ -37,12 +37,32 @@ class Management::InstitutionsController < ManagementController
end end
end end
# POST /management/institutions/:id/user/:user_id
# add user to institution
def add_user
user = User.find(params[:user_id])
institution_repository.add_user(user, @institution) unless user.blank?
render json: true
end
# DELETE /management/institutions/:id/user/:user_id
# remove user from institution
def remove_user
user = User.find(params[:user_id])
institution_repository.remove_user(user, @institution) unless user.blank?
render json: true
end
# PATCH/PUT /management/institutions/1 # PATCH/PUT /management/institutions/1
# PATCH/PUT /management/institutions/1.json # PATCH/PUT /management/institutions/1.json
def update def update
respond_to do |format| respond_to do |format|
if @institution.update(institution_params) if @institution.update(institution_params)
format.html { redirect_to [:management, @institution], notice: 'Institution was successfully updated.' } format.html { redirect_to [:management, @institution], notice: 'Instituição modificada com sucesso.' }
format.json { render :show, status: :ok, location: @institution } format.json { render :show, status: :ok, location: @institution }
else else
format.html { render :edit } format.html { render :edit }
...@@ -56,7 +76,7 @@ class Management::InstitutionsController < ManagementController ...@@ -56,7 +76,7 @@ class Management::InstitutionsController < ManagementController
def destroy def destroy
institution_repository.destroy @institution institution_repository.destroy @institution
respond_to do |format| respond_to do |format|
format.html { redirect_to management_institutions_url, notice: 'Institution was successfully destroyed.' } format.html { redirect_to management_institutions_url, notice: 'Instituição deletada com sucesso.' }
format.json { head :no_content } format.json { head :no_content }
end end
end end
......
...@@ -31,6 +31,10 @@ class UsersController < ApplicationController ...@@ -31,6 +31,10 @@ class UsersController < ApplicationController
] ]
end end
def list
@users = user_repository.all
end
private private
......
...@@ -10,7 +10,7 @@ class OrientDb::Base ...@@ -10,7 +10,7 @@ class OrientDb::Base
end end
def find(id) def find(id)
result = get_by_rid(id) result = get_by_rid(CGI::unescape(id))
build_object result build_object result
end end
...@@ -45,7 +45,7 @@ class OrientDb::Base ...@@ -45,7 +45,7 @@ class OrientDb::Base
objects objects
end end
# Take the object and make a hash in the OrientDB format. # Take the object and make a hash in the OrientDB format.
# Used to create a document. # Used to create a document.
def build_hash(object) def build_hash(object)
hash = {'@class' => odb_class} hash = {'@class' => odb_class}
......
json.array!(@learning_objects) do |learning_object|
json.extract! learning_object, :id
json.url learning_object_url(learning_object, format: :json)
end
json.array! @users
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
<th>Descrição</th> <th>Descrição</th>
<!-- <th></th> --> <!-- <th></th> -->
<th></th> <th></th>
<th></th>
</tr> </tr>
</thead> </thead>
...@@ -24,6 +25,7 @@ ...@@ -24,6 +25,7 @@
<td><%= institution.address %></td> <td><%= institution.address %></td>
<td><%= institution.description %></td> <td><%= institution.description %></td>
<!-- <td class="ls-txt-right ls-regroup"><%= link_to 'Editar', edit_management_institution_path(institution.id) %></td> --> <!-- <td class="ls-txt-right ls-regroup"><%= link_to 'Editar', edit_management_institution_path(institution.id) %></td> -->
<td><%= link_to 'Usuários', 'javascript:void(0);', 'data-institution':ERB::Util.url_encode(institution.id) %></td>
<td><%= link_to 'Excluir', management_institution_path(institution.id), method: :delete, data: { confirm: 'Você tem certeza?' } %></td> <td><%= link_to 'Excluir', management_institution_path(institution.id), method: :delete, data: { confirm: 'Você tem certeza?' } %></td>
</tr> </tr>
<% end %> <% end %>
......
json.array! @users
...@@ -10,7 +10,12 @@ Rails.application.routes.draw do ...@@ -10,7 +10,12 @@ Rails.application.routes.draw do
namespace :management do namespace :management do
root 'welcome#index' root 'welcome#index'
resource :complaints resource :complaints
resources :institutions resources :institutions do
member do
post '/user/:user_id', as: :add_user, action: :add_user
delete '/user/:user_id', as: :remove_user, action: :remove_user
end
end
resources :highlights resources :highlights
resources :carousels resources :carousels
...@@ -38,7 +43,11 @@ Rails.application.routes.draw do ...@@ -38,7 +43,11 @@ Rails.application.routes.draw do
root 'welcome#index' root 'welcome#index'
resources :subjects, only: [:index, :show] resources :subjects, only: [:index, :show]
resources :institutions resources :institutions do
member do
get :users
end
end
resources :learning_objects do resources :learning_objects do
member do member do
...@@ -58,6 +67,7 @@ Rails.application.routes.draw do ...@@ -58,6 +67,7 @@ Rails.application.routes.draw do
end end
end end
get '/users/list' => 'users#list', as: :list_all_users
resources :users, only: [:show] do resources :users, only: [:show] do
collection do collection do
get :me get :me
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment