diff --git a/app/assets/javascripts/management.js b/app/assets/javascripts/management.js index 2d3b06881e852c5fa40034fb4d8a6911e844d9d7..02ead06a5e6ae6d91d4cd72f113066c3e97e53e0 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 0000000000000000000000000000000000000000..24edb353d62c2955277978889d478edf15b0666b --- /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 280f4bb3720693b96278863b5483e7161ac5d46d..1375cc6a9534629bdfd81e5be9fbdb083db99ab7 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 b668a26a983e732e35f363de431f1c24eda430b4..9bc354c56c7823d2b44ef7eb378807a43ab2ec7e 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 5b21afc9eb4a530d02584e6503d0b246b1cf9390..7ae24fe55649646028423c0171b9fbb2fd3ae115 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 bcb0042840393766e51abae96b10f19f747fb8be..5075b7b8bab78bac209f57a7c90d0f198effa5a7 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 8ccf9939697b27eb58be82e3073654af83962b50..e6eba5e3aaa558536f5f8ae84b6255c5b1032944 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 6bcb70c3173cb9b75a2cdc32f829874ad1868728..0000000000000000000000000000000000000000 --- 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 0000000000000000000000000000000000000000..d42e36dbf1498119b8e8bce484a495d8014861c9 --- /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 b1402beeb0fd9d223a4c4d59c943590c04b5034d..d7e667fa6d655beed2489b5764729a3bf04d8891 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 0000000000000000000000000000000000000000..d42e36dbf1498119b8e8bce484a495d8014861c9 --- /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 eedad4187f98b92e4f00fcdebbd5c4cddd21586d..5c4ae98b8b7d5e2fa405d51833648a491d668d08 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