From 10eec9c080fa14dd6eace148b3089c01209eb8df Mon Sep 17 00:00:00 2001 From: Petko Bordjukov Date: Wed, 17 Sep 2014 14:23:36 +0300 Subject: [PATCH] Initial implementation of user management --- .../{navbar.css.scss => _navbar.css.scss} | 0 .../management/_record_table.css.scss | 32 +++++++++++++++++++ .../management/application.css.scss | 4 +-- .../management/users_controller.rb | 20 ++++++++++++ app/views/layouts/management.html.slim | 2 +- app/views/management/users/_user.html.slim | 17 ++++++++++ app/views/management/users/index.html.slim | 14 ++++++++ .../management/users/toggle_admin.js.erb | 1 + config/locales/management.bg.yml | 2 ++ config/locales/management.en.yml | 2 ++ config/routes.rb | 7 +++- .../management/users_controller_spec.rb | 12 +++++++ 12 files changed, 109 insertions(+), 4 deletions(-) rename app/assets/stylesheets/management/{navbar.css.scss => _navbar.css.scss} (100%) create mode 100644 app/assets/stylesheets/management/_record_table.css.scss create mode 100644 app/controllers/management/users_controller.rb create mode 100644 app/views/management/users/_user.html.slim create mode 100644 app/views/management/users/index.html.slim create mode 100644 app/views/management/users/toggle_admin.js.erb create mode 100644 config/locales/management.bg.yml create mode 100644 config/locales/management.en.yml create mode 100644 spec/controllers/management/users_controller_spec.rb diff --git a/app/assets/stylesheets/management/navbar.css.scss b/app/assets/stylesheets/management/_navbar.css.scss similarity index 100% rename from app/assets/stylesheets/management/navbar.css.scss rename to app/assets/stylesheets/management/_navbar.css.scss diff --git a/app/assets/stylesheets/management/_record_table.css.scss b/app/assets/stylesheets/management/_record_table.css.scss new file mode 100644 index 0000000..9054741 --- /dev/null +++ b/app/assets/stylesheets/management/_record_table.css.scss @@ -0,0 +1,32 @@ +.record-table { + @extend .table; + + td { + vertical-align: middle !important; + } + + .actions { + @extend .pull-right; + @extend .btn-group; + white-space: nowrap; + + a { + @extend .btn; + @extend .btn-default; + } + } + + .toggle { + a { + @extend .btn; + } + + a.checked { + @extend .btn-success; + } + + a.unchecked { + @extend .btn-primary; + } + } +} \ No newline at end of file diff --git a/app/assets/stylesheets/management/application.css.scss b/app/assets/stylesheets/management/application.css.scss index 9539166..2f826f8 100644 --- a/app/assets/stylesheets/management/application.css.scss +++ b/app/assets/stylesheets/management/application.css.scss @@ -1,6 +1,6 @@ -//= require_directory . - @import "bootstrap-sprockets"; @import "bootswatch/simplex/variables"; @import "bootstrap"; @import "bootswatch/simplex/bootswatch"; +@import "navbar"; +@import "record_table"; \ No newline at end of file diff --git a/app/controllers/management/users_controller.rb b/app/controllers/management/users_controller.rb new file mode 100644 index 0000000..f325311 --- /dev/null +++ b/app/controllers/management/users_controller.rb @@ -0,0 +1,20 @@ +module Management + class UsersController < ManagementController + before_action :assign_user, only: [:toggle_admin] + + def index + @users = User.all + end + + def toggle_admin + @user.admin = !@user.admin + @user.save + end + + private + + def assign_user + @user = User.find params[:id] + end + end +end diff --git a/app/views/layouts/management.html.slim b/app/views/layouts/management.html.slim index fee2c2c..be0938f 100644 --- a/app/views/layouts/management.html.slim +++ b/app/views/layouts/management.html.slim @@ -21,7 +21,7 @@ html .navbar-collapse.collapse ul.nav.navbar-nav li.active = link_to 'Home', root_path - li = link_to 'Users', root_path + li = link_to 'Users', management_users_path div.container == yield = javascript_include_tag "management/application" diff --git a/app/views/management/users/_user.html.slim b/app/views/management/users/_user.html.slim new file mode 100644 index 0000000..1c023b6 --- /dev/null +++ b/app/views/management/users/_user.html.slim @@ -0,0 +1,17 @@ +tr id="user-#{user.id}" + td = user.email + td = user.speaker_profile.try(:first_name) or '-' + td = user.speaker_profile.try(:last_name) or '-' + td + - if user.admin? + span.toggle + = link_to glyph(:check), toggle_admin_management_user_path(user), remote: true, method: :post, data: {confirm: t(:are_you_sure)}, class: 'checked' + - else + span.toggle + = link_to glyph(:unchecked), toggle_admin_management_user_path(user), remote: true, method: :post, data: {confirm: t(:are_you_sure)}, class: 'unchecked' + td + .actions + = link_to management_user_path(user), title: t(:edit) + = glyph(:share) + = link_to edit_management_user_path(user), title: t(:edit) + = glyph(:edit) diff --git a/app/views/management/users/index.html.slim b/app/views/management/users/index.html.slim new file mode 100644 index 0000000..7f7fe2b --- /dev/null +++ b/app/views/management/users/index.html.slim @@ -0,0 +1,14 @@ +.row + .panel.panel-primary + .panel-heading + h3.panel-title = User.model_name.human(count: 2) + table.record-table + thead + tr + th = User.human_attribute_name(:email) + th = SpeakerProfile.human_attribute_name(:first_name) + th = SpeakerProfile.human_attribute_name(:last_name) + th = SpeakerProfile.human_attribute_name(:admin) + th + tbody + = render @users diff --git a/app/views/management/users/toggle_admin.js.erb b/app/views/management/users/toggle_admin.js.erb new file mode 100644 index 0000000..2dd9be2 --- /dev/null +++ b/app/views/management/users/toggle_admin.js.erb @@ -0,0 +1 @@ +$('#user-<%= @user.id %>').replaceWith('<%= j render @user %>'); diff --git a/config/locales/management.bg.yml b/config/locales/management.bg.yml new file mode 100644 index 0000000..b315231 --- /dev/null +++ b/config/locales/management.bg.yml @@ -0,0 +1,2 @@ +bg: + are_you_sure: Сигурен ли си? \ No newline at end of file diff --git a/config/locales/management.en.yml b/config/locales/management.en.yml new file mode 100644 index 0000000..31a39c9 --- /dev/null +++ b/config/locales/management.en.yml @@ -0,0 +1,2 @@ +en: + are_you_sure: Are you sure? \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 7cef799..91f348c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,8 +4,13 @@ Rails.application.routes.draw do devise_for :users, controllers: {registrations: 'registrations', sessions: 'sessions'} - scope :management, module: :management do + namespace :management do get '/', to: 'home#index' + resources :users do + member do + post 'toggle_admin' + end + end end root 'home#index' diff --git a/spec/controllers/management/users_controller_spec.rb b/spec/controllers/management/users_controller_spec.rb new file mode 100644 index 0000000..beab27b --- /dev/null +++ b/spec/controllers/management/users_controller_spec.rb @@ -0,0 +1,12 @@ +require 'rails_helper' + +RSpec.describe Management::UsersController, :type => :controller do + + describe "GET index" do + it "returns http success" do + get :index + expect(response).to be_success + end + end + +end