From b11de5c5f0886b6246dfbaf0b499b129680a8efe Mon Sep 17 00:00:00 2001 From: Mateus Rambo Strey <mars11@inf.ufpr.br> Date: Tue, 10 Nov 2015 13:17:35 -0200 Subject: [PATCH] add and remove users from institution in management --- app/assets/javascripts/management.js | 5 +- .../management/institutions.coffee | 49 +++++++++++++++++++ .../{management.css => management.scss} | 3 ++ app/controllers/institutions_controller.rb | 8 ++- .../management/institutions_controller.rb | 28 +++++++++-- app/controllers/users_controller.rb | 4 ++ app/repositories/orient_db/base.rb | 4 +- app/views/institutions/index.json.jbuilder | 4 -- app/views/institutions/users.json.jbuilder | 1 + .../management/institutions/index.html.erb | 2 + app/views/users/list.json.jbuilder | 1 + config/routes.rb | 14 +++++- 12 files changed, 106 insertions(+), 17 deletions(-) create mode 100644 app/assets/javascripts/management/institutions.coffee rename app/assets/stylesheets/{management.css => management.scss} (93%) delete mode 100644 app/views/institutions/index.json.jbuilder create mode 100644 app/views/institutions/users.json.jbuilder create mode 100644 app/views/users/list.json.jbuilder diff --git a/app/assets/javascripts/management.js b/app/assets/javascripts/management.js index 2d3b0688..02ead06a 100644 --- a/app/assets/javascripts/management.js +++ b/app/assets/javascripts/management.js @@ -14,13 +14,12 @@ //= require jquery_ujs //= require turbolinks //= require jquery.turbolinks +//= require bootstrap-sprockets +// www.chartjs.org/docs/ //= require Chart //= require locastyle //= require_tree ./management - -//mais infos: www.chartjs.org/docs/ - var visualised,non_visualised; $(document).ready(function(){ diff --git a/app/assets/javascripts/management/institutions.coffee b/app/assets/javascripts/management/institutions.coffee new file mode 100644 index 00000000..24edb353 --- /dev/null +++ b/app/assets/javascripts/management/institutions.coffee @@ -0,0 +1,49 @@ +$ -> + $(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 diff --git a/app/assets/stylesheets/management.css b/app/assets/stylesheets/management.scss similarity index 93% rename from app/assets/stylesheets/management.css rename to app/assets/stylesheets/management.scss index 280f4bb3..1375cc6a 100644 --- a/app/assets/stylesheets/management.css +++ b/app/assets/stylesheets/management.scss @@ -14,3 +14,6 @@ *= require locastyle *= require_self */ + +@import "bootstrap-sprockets"; +@import "bootstrap"; diff --git a/app/controllers/institutions_controller.rb b/app/controllers/institutions_controller.rb index b668a26a..9bc354c5 100644 --- a/app/controllers/institutions_controller.rb +++ b/app/controllers/institutions_controller.rb @@ -1,5 +1,5 @@ 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.json @@ -57,10 +57,14 @@ class InstitutionsController < ApplicationController end end + def users + @users = institution_repository.associated_with @institution + end + private def set_institution - @institution = institution_repository.find("##{params[:id]}") + @institution = institution_repository.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. diff --git a/app/controllers/management/institutions_controller.rb b/app/controllers/management/institutions_controller.rb index 5b21afc9..7ae24fe5 100644 --- a/app/controllers/management/institutions_controller.rb +++ b/app/controllers/management/institutions_controller.rb @@ -1,5 +1,5 @@ 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.json @@ -28,7 +28,7 @@ class Management::InstitutionsController < ManagementController respond_to do |format| 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 } else format.html { render :new } @@ -37,12 +37,32 @@ class Management::InstitutionsController < ManagementController 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.json def update respond_to do |format| 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 } else format.html { render :edit } @@ -56,7 +76,7 @@ class Management::InstitutionsController < ManagementController def destroy institution_repository.destroy @institution 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 } end end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index bcb00428..5075b7b8 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -31,6 +31,10 @@ class UsersController < ApplicationController ] end + def list + @users = user_repository.all + end + private diff --git a/app/repositories/orient_db/base.rb b/app/repositories/orient_db/base.rb index 8ccf9939..e6eba5e3 100644 --- a/app/repositories/orient_db/base.rb +++ b/app/repositories/orient_db/base.rb @@ -10,7 +10,7 @@ class OrientDb::Base end def find(id) - result = get_by_rid(id) + result = get_by_rid(CGI::unescape(id)) build_object result end @@ -45,7 +45,7 @@ class OrientDb::Base objects 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. def build_hash(object) hash = {'@class' => odb_class} diff --git a/app/views/institutions/index.json.jbuilder b/app/views/institutions/index.json.jbuilder deleted file mode 100644 index 6bcb70c3..00000000 --- a/app/views/institutions/index.json.jbuilder +++ /dev/null @@ -1,4 +0,0 @@ -json.array!(@learning_objects) do |learning_object| - json.extract! learning_object, :id - json.url learning_object_url(learning_object, format: :json) -end diff --git a/app/views/institutions/users.json.jbuilder b/app/views/institutions/users.json.jbuilder new file mode 100644 index 00000000..d42e36db --- /dev/null +++ b/app/views/institutions/users.json.jbuilder @@ -0,0 +1 @@ +json.array! @users diff --git a/app/views/management/institutions/index.html.erb b/app/views/management/institutions/index.html.erb index b1402bee..d7e667fa 100644 --- a/app/views/management/institutions/index.html.erb +++ b/app/views/management/institutions/index.html.erb @@ -12,6 +12,7 @@ <th>Descrição</th> <!-- <th></th> --> <th></th> + <th></th> </tr> </thead> @@ -24,6 +25,7 @@ <td><%= institution.address %></td> <td><%= institution.description %></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> </tr> <% end %> diff --git a/app/views/users/list.json.jbuilder b/app/views/users/list.json.jbuilder new file mode 100644 index 00000000..d42e36db --- /dev/null +++ b/app/views/users/list.json.jbuilder @@ -0,0 +1 @@ +json.array! @users diff --git a/config/routes.rb b/config/routes.rb index eedad418..5c4ae98b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -10,7 +10,12 @@ Rails.application.routes.draw do namespace :management do root 'welcome#index' 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 :carousels @@ -38,7 +43,11 @@ Rails.application.routes.draw do root 'welcome#index' resources :subjects, only: [:index, :show] - resources :institutions + resources :institutions do + member do + get :users + end + end resources :learning_objects do member do @@ -58,6 +67,7 @@ Rails.application.routes.draw do end end + get '/users/list' => 'users#list', as: :list_all_users resources :users, only: [:show] do collection do get :me -- GitLab