-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic admin UI to create, edit and generate new door codes (#598)
* Add pages to show existing door codes and input new ones. * Make it possible to edit existing codes. * Add capability to autogenerate 10 new codes. * Small improvements to the UI, code organization.
- Loading branch information
Showing
10 changed files
with
223 additions
and
17 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
class Admin::DoorCodesController < ApplicationController | ||
before_action :ensure_admin | ||
|
||
def index | ||
@door_codes = DoorCode.all.includes(:user).order(created_at: :asc) | ||
end | ||
|
||
def new | ||
end | ||
|
||
def edit | ||
end | ||
|
||
def create | ||
if door_code.save | ||
flash[:notice] = "You have successfully created door code #{door_code.code}" | ||
redirect_to action: :index | ||
else | ||
flash[:warning] = "Errors saving code. See details below." | ||
render :new | ||
end | ||
end | ||
|
||
def update | ||
if door_code.update(door_code_params) | ||
flash[:notice] = "You have successfully updated door code #{door_code.code}" | ||
redirect_to action: :index | ||
else | ||
flash[:warning] = "Errors saving code. See details below." | ||
render :edit | ||
end | ||
end | ||
|
||
def generate_new | ||
num_codes_to_generate = 10 | ||
num_codes_to_generate.times do | ||
DoorCode.create!(code: DoorCode.make_random_code) | ||
end | ||
|
||
flash[:notice] = "Autogenerated #{num_codes_to_generate} new codes (see table below for details)" | ||
redirect_to action: :index | ||
end | ||
|
||
private | ||
|
||
def door_code_params | ||
return {} unless params.key?(:door_code) | ||
|
||
params.require(:door_code).permit(:code, :status, :user_id, :id) | ||
end | ||
|
||
helper_method def door_code | ||
@door_code ||= params.key?(:id) ? DoorCode.find(params[:id]) : DoorCode.new(door_code_params) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<%= form_with(model: [:admin, door_code], local: true) do |door_code_form| %> | ||
<p class="bg-danger"> | ||
<% door_code.errors.messages.each do |field, msgs| %> | ||
<strong><%= field.to_s.humanize %>:</strong> | ||
<%= msgs.join(', ') %> | ||
<% end %> | ||
</p> | ||
|
||
<div class="form-group"> | ||
<%= door_code_form.label :code %> | ||
<%= door_code_form.text_field :code %> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<%= door_code_form.label :status %> | ||
<%= door_code_form.select :status, DoorCode.statuses.keys.to_a %> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<%= door_code_form.label :user_id %> | ||
<%= door_code_form.select :user_id, User.all.collect { |u| [u.name, u.id] }, { include_blank: true} %> | ||
<p class="help-block">Optional. Use only if you want to assign this code to an existing user.</p> | ||
</div> | ||
|
||
<div> | ||
<%= door_code_form.submit %> | ||
</div> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<h1>Edit Door Code</h1> | ||
|
||
<%= render partial: 'form', locals: { door_code: door_code} %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<h1>Manage Door Codes</h1> | ||
|
||
<%= link_to 'Add New Door Code', new_admin_door_code_path(), class: "btn btn-primary" %> | ||
|
||
<%= form_with url: generate_new_admin_door_codes_path(), method: :post, local: true do |form| %> | ||
<%= form.submit "Autogenerate New Door Codes", class: "btn btn-primary" %> | ||
<% end %> | ||
|
||
<h2>Existing Door Codes</h2> | ||
|
||
<% if @door_codes.empty? %> | ||
There are no door codes in the database yet! | ||
<% end %> | ||
|
||
<table class="table table-striped"> | ||
<thead> | ||
<tr> | ||
<th>Code</th> | ||
<th>Status</th> | ||
<th>Assigned To</th> | ||
<th></th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<% @door_codes.each do |door_code| %> | ||
<tr> | ||
<td> | ||
<%= door_code.code %> | ||
</td> | ||
<td> | ||
<%= door_code.status %> | ||
</td> | ||
<td> | ||
<%= door_code.user_id.present? ? door_code.user.name : "unassigned" %> | ||
</td> | ||
<td> | ||
<%= link_to 'Edit', edit_admin_door_code_path(door_code), class: "btn btn-primary" %> | ||
</td> | ||
</tr> | ||
<% end %> | ||
<tbody> | ||
</table> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<h1>Add New Door Code</h1> | ||
|
||
<%= render partial: 'form', locals: { door_code: door_code} %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe Admin::DoorCodesController do | ||
include AuthHelper | ||
|
||
render_views | ||
|
||
let(:door_code) { create(:door_code) } | ||
let(:params) { { id: door_code.id } } | ||
|
||
describe "GET :index" do | ||
context "logged in as a non-admin member" do | ||
before { login_as(:member) } | ||
|
||
it "redirects to root" do | ||
get :index | ||
expect(response).to redirect_to :root | ||
end | ||
end | ||
|
||
context "when logged in as an admin" do | ||
let!(:door_codes) { create_list(:door_code, 5)} | ||
|
||
before { login_as(:voting_member, is_admin: true) } | ||
|
||
it "shows the index" do | ||
get :index | ||
|
||
door_codes.each do |door_code| | ||
expect(response.body).to include(door_code.code) | ||
expect(response.body).to include(door_code.status.to_s) | ||
end | ||
end | ||
end | ||
end | ||
|
||
describe "POST :create" do | ||
context "when logged in as an admin" do | ||
let!(:existing_member) { create(:member) } | ||
let!(:existing_door_code) { create(:door_code) } | ||
|
||
before { login_as(:voting_member, is_admin: true) } | ||
|
||
it "saves the new door code" do | ||
expect { | ||
post :create, params: { door_code: { code: "1234567", status: "in_lock", user_id: existing_member.id } } | ||
}.to change(DoorCode, :count).from(1).to(2) | ||
expect(existing_member.door_code.code).to eq("1234567") | ||
end | ||
|
||
it "doesn't save a duplicated door code" do | ||
expect { | ||
post :create, params: { door_code: { code: existing_door_code.code, status: "in_lock" } } | ||
}.not_to change(DoorCode, :count) | ||
end | ||
end | ||
end | ||
|
||
describe "PATCH :update" do | ||
context "when logged in as an admin" do | ||
let!(:member) { create(:member) } | ||
let!(:door_code) { create(:door_code) } | ||
|
||
before { login_as(:voting_member, is_admin: true) } | ||
|
||
it "updates the door code" do | ||
expect { | ||
patch :update, params: { id: door_code.id, door_code: { user_id: member.id } } | ||
}.not_to change(DoorCode, :count) | ||
expect(door_code.reload.user.id).to eq(member.id) | ||
end | ||
end | ||
end | ||
|
||
describe "POST :generate_new" do | ||
context "when logged in as an admin" do | ||
before { login_as(:voting_member, is_admin: true) } | ||
|
||
it "updates the door code" do | ||
expect { | ||
post :generate_new | ||
}.to change(DoorCode, :count).by(10) | ||
end | ||
end | ||
end | ||
end |