Skip to content

Commit

Permalink
99/scaffold - add table to add users in requests (#138)
Browse files Browse the repository at this point in the history
* add table to add users in requests #99

* Add the option to assign users to requests

* Add the option to select users with sudo rights

* Fix linter errors

* add required migrations

* autocorrect for rubocop

* fix parts of codefactor

* try to fix more codebeat

* try to fix more codebeat

* try to fix more codebeat

* try to fix more codefactor

* try to fix more codefactor
  • Loading branch information
Kenneth-Schroeder authored Dec 10, 2018
1 parent 39d3e8f commit 2f36be1
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ def authenticate_wimi
end

def authenticate_admin
redirect_to root_path, alert: I18n.t('authorization.unauthorized') unless current_user && current_user.admin?
redirect_to root_path, alert: I18n.t('authorization.unauthorized') unless current_user&.admin?
end
end
12 changes: 11 additions & 1 deletion app/controllers/requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def successfully_saved(format, request)
# POST /requests.json
def create
@request = Request.new(request_params)
save_sudo_rights(@request)

respond_to do |format|
if @request.save
Expand Down Expand Up @@ -100,13 +101,22 @@ def request_accept_button

private

def save_sudo_rights(request)
sudo_users_for_request = request.users_assigned_to_requests.select { |uatq| request_params[:sudo_user_ids].include?(uatq.user_id.to_s) }

sudo_users_for_request.each do |association|
association.sudo = true
end
end

# Use callbacks to share common setup or constraints between actions.
def set_request
@request = Request.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def request_params
params.require(:request).permit(:name, :cpu_cores, :ram_mb, :storage_mb, :operating_system, :comment, :rejection_information, :status)
params.require(:request).permit(:name, :cpu_cores, :ram_mb, :storage_mb, :operating_system, :comment,
:rejection_information, :status, user_ids: [], sudo_user_ids: [])
end
end
2 changes: 1 addition & 1 deletion app/controllers/users/registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Users::RegistrationsController < Devise::RegistrationsController

# If you have extra params to permit, append them to the sanitizer.
def configure_sign_up_params
devise_parameter_sanitizer.permit(:sign_up, keys: [:first_name, :last_name])
devise_parameter_sanitizer.permit(:sign_up, keys: %i[first_name last_name])
end

# If you have extra params to permit, append them to the sanitizer.
Expand Down
5 changes: 5 additions & 0 deletions app/models/request.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# frozen_string_literal: true

class Request < ApplicationRecord
has_many :users_assigned_to_requests
has_many :users, through: :users_assigned_to_requests

attr_accessor :sudo_user_ids

enum status: %i[pending accepted rejected]
validates :name, :cpu_cores, :ram_mb, :storage_mb, :operating_system, presence: true
validates :cpu_cores, numericality: { greater_than: 0, less_than: 65 }
Expand Down
5 changes: 5 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ class User < ApplicationRecord
:recoverable, :rememberable, :validatable,
:trackable
enum role: %i[user wimi admin]

has_many :users_assigned_to_requests
has_many :requests, through: :users_assigned_to_requests
has_one :user_profile
accepts_nested_attributes_for :user_profile
validates :first_name, presence: true
validates :last_name, presence: true

Expand Down
6 changes: 6 additions & 0 deletions app/models/users_assigned_to_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

class UsersAssignedToRequest < ApplicationRecord
belongs_to :user
belongs_to :request
end
18 changes: 18 additions & 0 deletions app/views/requests/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@
<%= form.number_field :storage_mb, min: 0, class: 'form-control' %>
</div>

<p>Assigned Users:</p>
<%= form.select 'user_ids',
options_from_collection_for_select(User.all,
:id,
:email),
{},
multiple: true,
size: 10 %>

<p>Users with sudo rights:</p>
<%= form.select 'sudo_user_ids',
options_from_collection_for_select(User.all,
:id,
:email),
{},
multiple: true,
size: 10 %>

<div class="field">
<%= form.label :operating_system, "Operating System" %>
<%= form.select :operating_system, operating_system_options %>
Expand Down
25 changes: 25 additions & 0 deletions app/views/requests/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<td><%= @request.operating_system %></td>
</tr>


<tr>
<td><strong>Comment:</strong></td>
<td class="col-md-4"><%= @request.comment %></td>
Expand All @@ -39,6 +40,30 @@
</tbody>
</table>

<p>
<strong>Users:</strong>
<ul>
<% @request.users.each do |user| %>
<p>
<li><%= user.email %></li>
</p>
<% end %>
</ul>
</p>

<p>
<strong>Users with sudo rights:</strong>
<ul>
<% @request.users_assigned_to_requests.each do |userAtRequest| %>
<% if userAtRequest.sudo %>
<p>
<li><%= userAtRequest.user.email %></li>
</p>
<% end %>
<% end %>
</ul>
</p>

<% if @request.rejected? %>
<p>
<strong>Rejection Information:</strong>
Expand Down
8 changes: 7 additions & 1 deletion db/migrate/20181207115117_merge_user_and_user_profile.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
# frozen_string_literal: true

class MergeUserAndUserProfile < ActiveRecord::Migration[5.2]
def up
add_column :users, :first_name, :string
add_column :users, :last_name, :string
update_users
drop_table :user_profiles
end

def update_users
User.all.each do |user|
if !user.nil?
user.first_name = user.first_name
Expand All @@ -12,7 +19,6 @@ def up
end
user.save
end
drop_table :user_profiles
end

def down
Expand Down
16 changes: 16 additions & 0 deletions db/migrate/20181209184959_create_users_assigned_to_requests.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

class CreateUsersAssignedToRequests < ActiveRecord::Migration[5.2]
def change
create_table :users_assigned_to_requests do |t|
t.boolean 'sudo'
t.integer 'request_id'
t.integer 'user_id'
t.datetime 'created_at', null: false
t.datetime 'updated_at', null: false
t.index ['request_id'], name: 'index_users_assigned_to_requests_on_request_id'
t.index ['user_id'], name: 'index_users_assigned_to_requests_on_user_id'
t.timestamps
end
end
end
14 changes: 13 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
Expand All @@ -10,7 +12,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2018_12_07_115117) do
ActiveRecord::Schema.define(version: 20_181_209_184_959) do
create_table 'operating_systems', force: :cascade do |t|
t.string 'name'
t.datetime 'created_at', null: false
Expand Down Expand Up @@ -65,4 +67,14 @@
t.index ['email'], name: 'index_users_on_email', unique: true
t.index ['reset_password_token'], name: 'index_users_on_reset_password_token', unique: true
end

create_table 'users_assigned_to_requests', force: :cascade do |t|
t.boolean 'sudo'
t.integer 'request_id'
t.integer 'user_id'
t.datetime 'created_at', null: false
t.datetime 'updated_at', null: false
t.index ['request_id'], name: 'index_users_assigned_to_requests_on_request_id'
t.index ['user_id'], name: 'index_users_assigned_to_requests_on_user_id'
end
end
6 changes: 6 additions & 0 deletions spec/factories/users_assigned_to_requests.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

FactoryBot.define do
factory :users_assigned_to_request do
end
end
7 changes: 7 additions & 0 deletions spec/models/users_assigned_to_request_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe UsersAssignedToRequest, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end

0 comments on commit 2f36be1

Please sign in to comment.