Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 9 additions & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ class UsersController < ApplicationController

# GET /users
def index
if params[:barcode] != nil
@user = User.find_by(barcode: params[:barcode])
if @user
redirect_to @user
else
redirect_to users_url, :flash => { :error => "no user with that barcode found" }
end
end
@users = User.order(active: :desc).order_by_name_asc
# index.html.haml
end
Expand Down Expand Up @@ -138,7 +146,7 @@ def buy_drink
end

def user_params
params.require(:user).permit(:name, :avatar_provider, :avatar, :balance, :active, :audit, :redirect)
params.require(:user).permit(:name, :avatar_provider, :avatar, :balance, :active, :audit, :redirect, :barcode)
end

def warn_user_if_audit
Expand Down
8 changes: 8 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@

class User < ApplicationRecord
validates :name, presence: true
validates :barcode, uniqueness: { conditions: -> { where.not(barcode: nil) } }

enum :avatar_provider, [ :gravatar, :webfinger ]

scope :order_by_name_asc, -> {
order(arel_table['name'].lower.asc)
}

before_save do |user|
if user.barcode == ''
user.barcode = nil
end
end


after_save do |user|
Audit.create! difference: user.balance - user.balance_before_last_save, drink: @purchased_drink, user: user.audit? ? user : nil
end
Expand Down
1 change: 1 addition & 0 deletions app/views/users/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
= f.input :active
= f.input :audit, hint: 'This will create detailed logs about what you buy and deposit. If you uncheck this box, all records will be deleted.'
= f.input :redirect, hint: "Redirect after buying a drink?"
= f.input :barcode, hint: 'your barcode'
.form-actions
= f.button :submit, class: 'btn-primary'

Expand Down
6 changes: 6 additions & 0 deletions app/views/users/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
%li.nav-item
= link_to 'new user', [:new, :user], class: "nav-link"

.row
%form{:method => "GET"}
%small
Select a user by scanning a barcode:
%input{:name => "barcode", :autofocus => true, :inputMode => "none"}

.user-filter
.btn-group
%a.btn{href: "#0", class: "user-filter-0"} #
Expand Down
6 changes: 6 additions & 0 deletions db/migrate/20251228140735_add_barcode_to_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddBarcodeToUser < ActiveRecord::Migration[8.0]
def change
add_column :users, :barcode, :string, null: true
add_index :users, :barcode, unique: true, null: true
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[8.0].define(version: 2023_06_30_204356) do
ActiveRecord::Schema[8.0].define(version: 2025_12_28_140735) do
create_table "audits", force: :cascade do |t|
t.datetime "created_at", precision: nil
t.decimal "difference", precision: 20, scale: 2, default: "0.0"
Expand Down Expand Up @@ -46,5 +46,7 @@
t.boolean "audit", default: false
t.boolean "redirect", default: true
t.integer "avatar_provider", default: 0
t.string "barcode"
t.index ["barcode"], name: "index_users_on_barcode", unique: true
end
end
12 changes: 12 additions & 0 deletions test/unit/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,16 @@ class UserTest < ActiveSupport::TestCase
assert_respond_to u, :audit, "audit missing"
assert_respond_to u, :redirect, "redirect missing"
end

test "two users should't have the same barcode" do
user = User.new
user.name = "test"
user.barcode = "testbc"
user.save

user2 = User.new
user2.name = "test2"
user2.barcode = "testbc"
assert_equal user2.save, false, "two users with the same barcode could be created"
end
end