add karma column to users table
This commit is contained in:
parent
db80320897
commit
3757b279ff
|
@ -8,15 +8,10 @@ class UsersController < ApplicationController
|
||||||
@title = "Users"
|
@title = "Users"
|
||||||
|
|
||||||
parents = {}
|
parents = {}
|
||||||
karmas = {}
|
|
||||||
User.all.each do |u|
|
User.all.each do |u|
|
||||||
(parents[u.invited_by_user_id.to_i] ||= []).push u
|
(parents[u.invited_by_user_id.to_i] ||= []).push u
|
||||||
end
|
end
|
||||||
|
|
||||||
Keystore.where("`key` like 'user:%:karma'").each do |k|
|
|
||||||
karmas[k.key[/\d+/].to_i] = k.value
|
|
||||||
end
|
|
||||||
|
|
||||||
@tree = []
|
@tree = []
|
||||||
recursor = lambda{|user,level|
|
recursor = lambda{|user,level|
|
||||||
if user
|
if user
|
||||||
|
@ -24,7 +19,7 @@ class UsersController < ApplicationController
|
||||||
:level => level,
|
:level => level,
|
||||||
:user_id => user.id,
|
:user_id => user.id,
|
||||||
:username => user.username,
|
:username => user.username,
|
||||||
:karma => karmas[user.id].to_i,
|
:karma => user.karma,
|
||||||
:is_moderator => user.is_moderator?,
|
:is_moderator => user.is_moderator?,
|
||||||
:is_admin => user.is_admin?,
|
:is_admin => user.is_admin?,
|
||||||
:created => user.created_at,
|
:created => user.created_at,
|
||||||
|
|
|
@ -107,10 +107,6 @@ class User < ActiveRecord::Base
|
||||||
PasswordReset.password_reset_link(self, ip).deliver
|
PasswordReset.password_reset_link(self, ip).deliver
|
||||||
end
|
end
|
||||||
|
|
||||||
def karma
|
|
||||||
Keystore.value_for("user:#{self.id}:karma").to_i
|
|
||||||
end
|
|
||||||
|
|
||||||
def linkified_about
|
def linkified_about
|
||||||
# most users are probably mentioning "@username" to mean a twitter url, not
|
# most users are probably mentioning "@username" to mean a twitter url, not
|
||||||
# a link to a profile on this site
|
# a link to a profile on this site
|
||||||
|
|
|
@ -131,8 +131,7 @@ class Vote < ActiveRecord::Base
|
||||||
if v.comment_id
|
if v.comment_id
|
||||||
c = Comment.find(v.comment_id)
|
c = Comment.find(v.comment_id)
|
||||||
if c.user_id != user_id
|
if c.user_id != user_id
|
||||||
Keystore.increment_value_for("user:#{c.user_id}:karma",
|
User.update_counters user_id, :karma => upvote - downvote
|
||||||
upvote - downvote)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
c.give_upvote_or_downvote_and_recalculate_confidence!(upvote,
|
c.give_upvote_or_downvote_and_recalculate_confidence!(upvote,
|
||||||
|
@ -140,8 +139,7 @@ class Vote < ActiveRecord::Base
|
||||||
else
|
else
|
||||||
s = Story.find(v.story_id)
|
s = Story.find(v.story_id)
|
||||||
if s.user_id != user_id
|
if s.user_id != user_id
|
||||||
Keystore.increment_value_for("user:#{s.user_id}:karma",
|
User.update_counters s.user_id, :karma => upvote - downvote
|
||||||
upvote - downvote)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
s.give_upvote_or_downvote_and_recalculate_hotness!(upvote, downvote)
|
s.give_upvote_or_downvote_and_recalculate_hotness!(upvote, downvote)
|
||||||
|
|
23
db/migrate/20140101202252_add_karma_to_users.rb
Normal file
23
db/migrate/20140101202252_add_karma_to_users.rb
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
class AddKarmaToUsers < ActiveRecord::Migration
|
||||||
|
def up
|
||||||
|
add_column :users, :karma, :integer, :default => 0, :null => false
|
||||||
|
|
||||||
|
Keystore.transaction do
|
||||||
|
User.lock(true).select(:id).each do |u|
|
||||||
|
u.update_column :karma, Keystore.value_for("user:#{u.id}:karma").to_i
|
||||||
|
end
|
||||||
|
|
||||||
|
Keystore.where(Keystore.arel_table[:key].matches("user:%:karma")).delete_all
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def down
|
||||||
|
Keystore.transaction do
|
||||||
|
User.select(:id, :karma).each do |u|
|
||||||
|
Keystore.put("user:#{u.id}:karma", u.karma)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
remove_column :users, :karma
|
||||||
|
end
|
||||||
|
end
|
|
@ -11,7 +11,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema.define(version: 20131228175805) do
|
ActiveRecord::Schema.define(version: 20140101202252) do
|
||||||
|
|
||||||
create_table "comments", force: true do |t|
|
create_table "comments", force: true do |t|
|
||||||
t.datetime "created_at", null: false
|
t.datetime "created_at", null: false
|
||||||
|
@ -157,6 +157,7 @@ ActiveRecord::Schema.define(version: 20131228175805) do
|
||||||
t.string "rss_token", limit: 75
|
t.string "rss_token", limit: 75
|
||||||
t.string "mailing_list_token", limit: 75
|
t.string "mailing_list_token", limit: 75
|
||||||
t.boolean "mailing_list_enabled", default: false
|
t.boolean "mailing_list_enabled", default: false
|
||||||
|
t.integer "karma", default: 0, null: false
|
||||||
end
|
end
|
||||||
|
|
||||||
add_index "users", ["mailing_list_enabled"], name: "mailing_list_enabled", using: :btree
|
add_index "users", ["mailing_list_enabled"], name: "mailing_list_enabled", using: :btree
|
||||||
|
|
Loading…
Reference in a new issue