diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 6524e14e..f9489ac3 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -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 @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index f2250ec8..ec733604 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -2,6 +2,7 @@ class User < ApplicationRecord validates :name, presence: true + validates :barcode, uniqueness: { conditions: -> { where.not(barcode: nil) } } enum :avatar_provider, [ :gravatar, :webfinger ] @@ -9,6 +10,13 @@ class User < ApplicationRecord 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 diff --git a/app/views/users/_form.html.haml b/app/views/users/_form.html.haml index 367e9989..941cc318 100644 --- a/app/views/users/_form.html.haml +++ b/app/views/users/_form.html.haml @@ -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' diff --git a/app/views/users/index.html.haml b/app/views/users/index.html.haml index 823f6e87..3c7794ab 100644 --- a/app/views/users/index.html.haml +++ b/app/views/users/index.html.haml @@ -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"} # diff --git a/db/migrate/20251228140735_add_barcode_to_user.rb b/db/migrate/20251228140735_add_barcode_to_user.rb new file mode 100644 index 00000000..81433b02 --- /dev/null +++ b/db/migrate/20251228140735_add_barcode_to_user.rb @@ -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 + end +end diff --git a/db/schema.rb b/db/schema.rb index 14100824..0047e90f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" @@ -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 diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index 4a2463a9..21a22ae0 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -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