From 715aca819f26b044aac550b80a9a24ce7f2221d7 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Wed, 9 Mar 2022 10:34:36 +0100 Subject: [PATCH] Reputation refactor --- src/const.rb | 3 +++ src/db_models.rb | 17 +++++++++++++---- src/lib/database.rb | 4 ++-- src/views/user/profile.slim | 2 +- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/const.rb b/src/const.rb index 5d18ed2..10d8b70 100644 --- a/src/const.rb +++ b/src/const.rb @@ -2,6 +2,9 @@ BAD_REP = -1 NEUTRAL_REP = 0 GOOD_REP = 1 +MIN_REP = -100 +MAX_REP = 100 + PERM_LEVELS = { post: 0, # allows the user to post auctions rmpost: 1, # allows the user to remove other peoples auctions diff --git a/src/db_models.rb b/src/db_models.rb index a80283f..0c6c491 100644 --- a/src/db_models.rb +++ b/src/db_models.rb @@ -1,6 +1,6 @@ # User table model class User < EntityModel - attr_reader :email, :name, :bio_text, :balance, :avatar_url, :reputation, :pw_hash + attr_reader :email, :name, :bio_text, :balance, :avatar_url, :pw_hash def initialize(user_info) super user_info @@ -19,7 +19,6 @@ class User < EntityModel def role user_roles = roles - p user_roles if user_roles.length > 0 then role = user_roles.max_by { |role| role.flags } return role.name @@ -42,9 +41,19 @@ class User < EntityModel md_parser.render @bio_text end + def reputation + rep = self.get "reputation", "id = ?", @id + rep && rep.to_i + end + def reputation_text - sign = @reputation > 0 ? "+" : "" - return "#{sign}#{@reputation}" + sign = reputation > 0 ? "+" : "" + return "#{sign}#{reputation}" + end + + def reputation= val + val = val.clamp MIN_REP, MAX_REP + self.update({reputation: val}, "id = ?", @id) end # Find user by ID, returns a user object diff --git a/src/lib/database.rb b/src/lib/database.rb index 21a96a1..3e7a5bc 100644 --- a/src/lib/database.rb +++ b/src/lib/database.rb @@ -51,11 +51,11 @@ class EntityModel self.query q, *args # execute query end - def self.update(data, filter="") # Updates the table with specified data hash + def self.update(data, filter="", *args) # Updates the table with specified data hash q = "UPDATE #{self.name} SET #{self.gen_update_query(data.keys)}" q = apply_filter(q, filter) - self.query(q, *data.values ) + self.query(q, *data.values, *args) end def self.insert(data) # Inserts new data into the table diff --git a/src/views/user/profile.slim b/src/views/user/profile.slim index b012cf8..bdb595e 100644 --- a/src/views/user/profile.slim +++ b/src/views/user/profile.slim @@ -14,7 +14,7 @@ a.button href="mailto:#{user.email}" li Email a.button href="/profile/#{user.id}/rep" - li Write review + li Review user - else a.button href="/settings" li Edit profile