Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Adjustment #382

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .hound.yml

This file was deleted.

31 changes: 31 additions & 0 deletions app/assets/javascripts/shortcuts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
function focusField(event, key, field) {
const tag = event.target.tagName;
if (field === null || event.defaultPrevented ||
event.key !== key || tag === 'INPUT' || tag === 'TEXTAREA') {
return; // Do nothing if the event was already processed or key was not s
}

field.focus();
event.preventDefault();
}

function searchKeydown(event) {
focusField(event, 's', document.getElementById('search-card'))
}

function adjustKey(event) {
focusField(event, 'a', document.getElementById('adjust'))
}

function setupShortcuts() {
document.addEventListener('keydown', adjustKey, true);
document.addEventListener('keydown', searchKeydown, true);
}

function removeShortcuts() {
document.addEventListener('keydown', adjustKey, true);
document.removeEventListener('keydown', searchKeydown, true);
}

document.addEventListener('turbolinks:load', setupShortcuts);
document.addEventListener('turbolinks:before-cache', removeShortcuts);
9 changes: 3 additions & 6 deletions app/controllers/admin/searches_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ class SearchesController < Admin::BaseController
authorize_resource(class: false)

def card
@vote_users = User.card_number(card_params)
card_number = params.require(:search).permit(:card_number)
@vote_user = User.card_number(card_number.fetch(:card_number, ''))
end

def user
Expand All @@ -14,18 +15,14 @@ def user

private

def card_params
params.require(:search).permit(:card_number).fetch(:card_number, '')
end

def presence
res = params.require(:search).fetch(:presence, '')
return [true, false] if res.blank?
res
end

def user_params
params.require(:search).permit(:firstname, :lastname)
params.require(:search).permit(:firstname, :lastname, :card_number)
.reject { |_, v| v.blank? }
end
end
Expand Down
15 changes: 9 additions & 6 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# Model for allowing users to identify and sign in
class User < ApplicationRecord
EMPTY_CARD = '____-____-____-____'
acts_as_paranoid
paginates_per(40)
devise(:database_authenticatable, :registerable,
Expand All @@ -14,10 +15,11 @@ class User < ApplicationRecord
validates :firstname, :lastname, presence: true
validates :votecode, uniqueness: true, allow_nil: true

validates :card_number, uniqueness: { allow_blank: true },
format: { with: /\A\b[0-9]{4}\-[0-9]{4}\-[0-9]{4}\-[0-9]{4}\z/,
message: I18n.t('model.user.card_number_format'),
allow_blank: true }
validates :card_number,
uniqueness: { allow_blank: true },
format: { with: /\A\b[0-9]{4}\-[0-9]{4}\-[0-9]{4}\-[0-9]{4}\z/,
message: I18n.t('model.user.card_number_format'),
allow_blank: true }

validate :presence_require_confirmation, :votecode_require_confirmation

Expand All @@ -38,12 +40,13 @@ class User < ApplicationRecord
end)

def self.card_number(card_number)
return if card_number == '____-____-____-____'
User.where('card_number LIKE ?', "%#{card_number}%")
return if card_number == EMPTY_CARD
User.where(card_number: card_number).first
end

def self.search(options)
return User.all if options.empty?
options = options.reject! { |_, v| v.blank? || v == EMPTY_CARD }
User.fuzzy_search(options.to_h)
end

Expand Down
16 changes: 8 additions & 8 deletions app/views/admin/adjustments/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
</div>
</div>

<div class="col-md-3 col-sm-12">
<div class="col-12 col-md-3">
<%= render('application/admin_sidebar', current: :adjustments) %>
<hr />
<% if can_administrate? :manage, :vote_user %>
Expand All @@ -13,20 +13,20 @@
<% end %>
</div>

<div class="col-12 col-md-9" id="vote-status" data-position="adjustment-index">
<%= render '/admin/votes/status', vote_status_view: @vote_status_view %>
</div>

<div class="col-12 col-md-9 offset-md-3">
<div class="col-12 col-md-9">
<div id="vote-status" data-position="adjustment-index">
<%= render '/admin/votes/status', vote_status_view: @vote_status_view %>
</div>
<div id="status-display" class="d-none border border-success rounded text-3 p-3 mb-3"></div>
<div id="error-display" class="d-none border border-danger rounded text-3 p-3 mb-3"></div>
<%= simple_form_for(:search, url: card_admin_search_path, remote: true) do |f| %>
<%= f.input(:card_number, required: false, autofocus: true,
label: User.human_attribute_name(:card_number),
input_html: { data: { mask: '9999-9999-9999-9999' },
autocomplete: 'off', id: 'search-card' }) %>
<% end %>

<div id="vote-users-table">
<%= render('/admin/vote_users/table') %>
<div id="vote-user">
</div>

<hr>
Expand Down
28 changes: 18 additions & 10 deletions app/views/admin/searches/_user_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
<%= simple_form_for(:search, url: user_admin_search_path,
wrapper: :inline_form,
html: { class: 'form-inline searches' },
html: { class: 'form-inline searches flex-column align-items-start' },
remote: true) do |f| %>
<%= f.input(:firstname, placeholder: User.human_attribute_name(:firstname),
required: false) %>
<%= f.input(:lastname, placeholder: User.human_attribute_name(:lastname),
required: false) %>
<%= f.input(:presence, collection: search_presences,
include_blank: false,
label: User.human_attribute_name(:presence),
required: false) %>
<%= f.button(:submit, t('.search'), class: 'px-3 py-1 m-0 ml-2 mb-2') %>
<div class="d-flex mb-2">
<%= f.input(:firstname, placeholder: User.human_attribute_name(:firstname),
required: false) %>
<%= f.input(:lastname, placeholder: User.human_attribute_name(:lastname),
required: false) %>
</div>
<div class="d-flex mb-2">
<%= f.input(:card_number, required: false, autofocus: true,
label: User.human_attribute_name(:card_number),
input_html: { data: { mask: '9999-9999-9999-9999' },
autocomplete: 'off', id: 'search-card' }) %>
<%= f.input(:presence, collection: search_presences,
include_blank: false,
label: User.human_attribute_name(:presence),
required: false) %>
<%= f.button(:submit, t('.search'), class: 'px-3 py-1 m-0 ml-2 mb-2') %>
</div>
<% end %>

<br />
Expand Down
13 changes: 8 additions & 5 deletions app/views/admin/searches/card.js.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
var table = document.getElementById('vote-users-table')
table.innerHTML = "<%= j render('admin/vote_users/table', vote_users: @vote_users) %>"
var user = document.getElementById('vote-user')
var statusDisplay = document.getElementById('status-display');
<% if @vote_users.any? %>
statusDisplay.innerHTML = "";
<% if @vote_user.present? %>
user.innerHTML = "<%= j render('admin/vote_users/user', vote_user: @vote_user) %>"
statusDisplay.classList.replace('d-flex', 'd-none');
statusDisplay.innerHTML = "";
var link = document.getElementById('adjust');
link.focus()
<% else %>
user.innerHTML = "";
statusDisplay.classList.replace('d-none', 'd-flex');
statusDisplay.innerHTML = "<%= j t('admin.searches.no_results') %>";
statusDisplay.innerHTML = "<%= t('admin.searches.no_results') %>";
<% end %>
24 changes: 24 additions & 0 deletions app/views/admin/vote_users/_user.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<div class="bg-info rounded p-3">
<h3>
<%= vote_user.to_s %>
</h3>
<div class="mb-2">
<%= vote_user.card_number %>
</div>

<div class="mb-2">
<% if vote_user.presence %>
<%= link_to(t('model.vote_user.make_not_present'),
admin_attendance_path(vote_user),
method: :delete, remote: true,
class: 'btn btn-primary',
id: 'adjust') %>
<% else %>
<%= link_to(t('model.vote_user.make_present'),
admin_attendance_path(vote_user),
method: :patch, remote: true,
class: 'btn btn-primary',
id: 'adjust') %>
<% end %>
</div>
</div>
9 changes: 4 additions & 5 deletions app/views/admin/vote_users/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@
</div>
</div>

<div class="col-12 col-md-9" id="vote-status" data-position="vote-user-index">
<%= render '/admin/votes/status', vote_status_view: @vote_status_view %>
</div>

<div class="col-12 col-md-9 offset-md-3">
<div class="col-12 col-md-9">
<div id="vote-status" data-position="vote-user-index">
<%= render '/admin/votes/status', vote_status_view: @vote_status_view %>
</div>
<%= render('admin/searches/user_form') %>
<div id="vote-users-table">
<%= render('table', vote_users: @vote_users, pagination: true) %>
Expand Down