Permalink
Browse files

Added current_user highlight in scores. Added rank to user page and r…

…ake task for cron to update it.
  • Loading branch information...
1 parent d86f869 commit 95e950acf6a73d0ab1ca2484f5ba1d29e96f8c53 @makaroni4 makaroni4 committed with David Davis Apr 1, 2012
@@ -10,6 +10,7 @@
html, body {height: 100%;}
$width: 950px;
+$green: #CFC;
body {
padding: 0 0 0;
@@ -66,7 +67,7 @@ div {
#notice {
border: 1px solid green;
color: green;
- background-color: #CCFFCC; }
+ background-color: $green; }
#error, #warning, #alert {
border: 1px solid #B11000;
@@ -125,9 +126,9 @@ table {
border-collapse: collapse;
width: 100%;
- tr.solved {
+ tr.solved, .self {
//color: green;
- background-color: #CCFFCC !important; }
+ background-color: $green !important; }
td, th {
border: 1px solid #ccc;
padding: 4px 6px;
@@ -2,11 +2,10 @@ class UsersController < ApplicationController
before_filter :restrict_to_admin, only: [:edit,:update,:destroy]
def index
- @users = User.all.desc(:score).desc(:solution_count).page(params[:page] || 1)
+ @users = User.asc(:rank).page(params[:page] || 1)
end
def show
- @user = User.where(:id => params[:id])
+ @user = User.find(params[:id])
end
-
-end
+end
@@ -41,4 +41,22 @@ def gravatar_image(email, size = 24, css = {})
css = {class: "gravatar"}.merge(css)
image_tag gravatar_url(email, options), css
end
+
+ def user_score(user)
+ output = ''
+ output << content_tag(:h1, 'Your score')
+ output << bold_paragraph(link_to "Rank: #{user.rank.nil? ? 'undefined' : user.rank}", users_path(:page => user.scores_page))
+ output << bold_paragraph(link_to "Score: #{user.score}", users_path(:page => user.scores_page))
+ output << bold_paragraph(link_to "Problems solved: #{user.solution_count} / #{Problem.approved.count}", problems_path)
+ output.html_safe
+ end
+
+ def bold_paragraph(text)
+ content_tag :p do
+ content_tag :b do
+ text
+ end
+ end
+ end
+
end
View
@@ -86,11 +86,7 @@ def create_upvote_for_solution
end
def update_user_solution_count
- # TODO: find all the solutions and update the user's solution count?
- if user_id && (updating_user = User.find(self.user_id))
- updating_user.solution_count = updating_user.solutions.count
- updating_user.save
- end
+ self.user.update_solution_count
end
end
View
@@ -8,8 +8,9 @@ class User
field :username
field :email
- field :score, type: Integer
+ field :score, type: Integer, :default => 0
field :solution_count, type: Integer
+ field :rank, type: Integer
field :admin, type: Boolean
## Devise fields
@@ -54,7 +55,7 @@ class User
attr_accessor :users_followed
#attr_protected :provider, :uid, :name, :email
- after_create :initialize_score
+ before_create :initialize_rank
track_history :on => [:username, :email, :admin],
:track_create => true,
@@ -67,7 +68,11 @@ def solved_problems
def solved_problem_scores
solved_problems
end
-
+
+ def scores_page
+ (rank.to_i / User.default_per_page.to_f).ceil
+ end
+
def to_s
username
end
@@ -122,9 +127,7 @@ def update_solution_count
end
protected
-
- def initialize_score
- self.score = 0
- self.save
- end
+ def initialize_rank
+ self.rank = User.count + 1
+ end
end
@@ -1,3 +1,4 @@
+<%= user_score(current_user) %>
<h1>Edit <%= resource_name.to_s.humanize %></h1>
<p class="profile-avatar"><b>Profile Image</b><br/>
@@ -12,8 +12,8 @@
</tr>
<% @users.each_with_index do |user, i| %>
- <tr class="user-record" id="<%= user.id %>">
- <td><%= i+1 + offset %></td>
+ <tr class="user-record <%= 'self' if user == current_user %>" id="<%= user.id %>">
+ <td><%= user.rank || 'undefined' %></td>
<td class="user"><%= gravatar_image(user.email, 24, style: "margin-right: 0.5em") %><%= user.username %></td>
<td><%= user.solution_count || 0 %></td>
<td><%= user.score || 0 %></td>
View
@@ -0,0 +1,8 @@
+namespace :user_rank do
+ # add rake task and execute is with Cron every 10 minutes for example
+ task :build => :environment do
+ User.desc(:score).asc(:solution_count).each_with_index do |user, position|
+ user.update_attribute(:rank, position + 1)
+ end
+ end
+end

0 comments on commit 95e950a

Please sign in to comment.