Reputation refactor

master
E. Almqvist 3 years ago
parent 9c12a8907f
commit 715aca819f
  1. 3
      src/const.rb
  2. 17
      src/db_models.rb
  3. 4
      src/lib/database.rb
  4. 2
      src/views/user/profile.slim

@ -2,6 +2,9 @@ BAD_REP = -1
NEUTRAL_REP = 0 NEUTRAL_REP = 0
GOOD_REP = 1 GOOD_REP = 1
MIN_REP = -100
MAX_REP = 100
PERM_LEVELS = { PERM_LEVELS = {
post: 0, # allows the user to post auctions post: 0, # allows the user to post auctions
rmpost: 1, # allows the user to remove other peoples auctions rmpost: 1, # allows the user to remove other peoples auctions

@ -1,6 +1,6 @@
# User table model # User table model
class User < EntityModel 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) def initialize(user_info)
super user_info super user_info
@ -19,7 +19,6 @@ class User < EntityModel
def role def role
user_roles = roles user_roles = roles
p user_roles
if user_roles.length > 0 then if user_roles.length > 0 then
role = user_roles.max_by { |role| role.flags } role = user_roles.max_by { |role| role.flags }
return role.name return role.name
@ -42,9 +41,19 @@ class User < EntityModel
md_parser.render @bio_text md_parser.render @bio_text
end end
def reputation
rep = self.get "reputation", "id = ?", @id
rep && rep.to_i
end
def reputation_text def reputation_text
sign = @reputation > 0 ? "+" : "" sign = reputation > 0 ? "+" : ""
return "#{sign}#{@reputation}" return "#{sign}#{reputation}"
end
def reputation= val
val = val.clamp MIN_REP, MAX_REP
self.update({reputation: val}, "id = ?", @id)
end end
# Find user by ID, returns a user object # Find user by ID, returns a user object

@ -51,11 +51,11 @@ class EntityModel
self.query q, *args # execute query self.query q, *args # execute query
end 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 = "UPDATE #{self.name} SET #{self.gen_update_query(data.keys)}"
q = apply_filter(q, filter) q = apply_filter(q, filter)
self.query(q, *data.values ) self.query(q, *data.values, *args)
end end
def self.insert(data) # Inserts new data into the table def self.insert(data) # Inserts new data into the table

@ -14,7 +14,7 @@
a.button href="mailto:#{user.email}" a.button href="mailto:#{user.email}"
li Email li Email
a.button href="/profile/#{user.id}/rep" a.button href="/profile/#{user.id}/rep"
li Write review li Review user
- else - else
a.button href="/settings" a.button href="/settings"
li Edit profile li Edit profile

Loading…
Cancel
Save