Skip to content

Commit

Permalink
Added current_user highlight in scores. Added rank to user page and r…
Browse files Browse the repository at this point in the history
…ake task for cron to update it.
  • Loading branch information
makaroni4 authored and David Davis committed Apr 4, 2012
1 parent d86f869 commit 95e950a
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 22 deletions.
7 changes: 4 additions & 3 deletions app/assets/stylesheets/application.css.scss
Expand Up @@ -10,6 +10,7 @@
html, body {height: 100%;} html, body {height: 100%;}


$width: 950px; $width: 950px;
$green: #CFC;


body { body {
padding: 0 0 0; padding: 0 0 0;
Expand Down Expand Up @@ -66,7 +67,7 @@ div {
#notice { #notice {
border: 1px solid green; border: 1px solid green;
color: green; color: green;
background-color: #CCFFCC; } background-color: $green; }


#error, #warning, #alert { #error, #warning, #alert {
border: 1px solid #B11000; border: 1px solid #B11000;
Expand Down Expand Up @@ -125,9 +126,9 @@ table {


border-collapse: collapse; border-collapse: collapse;
width: 100%; width: 100%;
tr.solved { tr.solved, .self {
//color: green; //color: green;
background-color: #CCFFCC !important; } background-color: $green !important; }
td, th { td, th {
border: 1px solid #ccc; border: 1px solid #ccc;
padding: 4px 6px; padding: 4px 6px;
Expand Down
7 changes: 3 additions & 4 deletions app/controllers/users_controller.rb
Expand Up @@ -2,11 +2,10 @@ class UsersController < ApplicationController
before_filter :restrict_to_admin, only: [:edit,:update,:destroy] before_filter :restrict_to_admin, only: [:edit,:update,:destroy]


def index def index
@users = User.all.desc(:score).desc(:solution_count).page(params[:page] || 1) @users = User.asc(:rank).page(params[:page] || 1)
end end


def show def show
@user = User.where(:id => params[:id]) @user = User.find(params[:id])
end end

end
end
18 changes: 18 additions & 0 deletions app/helpers/application_helper.rb
Expand Up @@ -41,4 +41,22 @@ def gravatar_image(email, size = 24, css = {})
css = {class: "gravatar"}.merge(css) css = {class: "gravatar"}.merge(css)
image_tag gravatar_url(email, options), css image_tag gravatar_url(email, options), css
end 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 end
6 changes: 1 addition & 5 deletions app/models/solution.rb
Expand Up @@ -86,11 +86,7 @@ def create_upvote_for_solution
end end


def update_user_solution_count def update_user_solution_count
# TODO: find all the solutions and update the user's solution count? self.user.update_solution_count
if user_id && (updating_user = User.find(self.user_id))
updating_user.solution_count = updating_user.solutions.count
updating_user.save
end
end end


end end
19 changes: 11 additions & 8 deletions app/models/user.rb
Expand Up @@ -8,8 +8,9 @@ class User


field :username field :username
field :email field :email
field :score, type: Integer field :score, type: Integer, :default => 0
field :solution_count, type: Integer field :solution_count, type: Integer
field :rank, type: Integer
field :admin, type: Boolean field :admin, type: Boolean


## Devise fields ## Devise fields
Expand Down Expand Up @@ -54,7 +55,7 @@ class User
attr_accessor :users_followed attr_accessor :users_followed
#attr_protected :provider, :uid, :name, :email #attr_protected :provider, :uid, :name, :email


after_create :initialize_score before_create :initialize_rank


track_history :on => [:username, :email, :admin], track_history :on => [:username, :email, :admin],
:track_create => true, :track_create => true,
Expand All @@ -67,7 +68,11 @@ def solved_problems
def solved_problem_scores def solved_problem_scores
solved_problems solved_problems
end end


def scores_page
(rank.to_i / User.default_per_page.to_f).ceil
end

def to_s def to_s
username username
end end
Expand Down Expand Up @@ -122,9 +127,7 @@ def update_solution_count
end end


protected protected

def initialize_rank
def initialize_score self.rank = User.count + 1
self.score = 0 end
self.save
end
end end
1 change: 1 addition & 0 deletions app/views/devise/registrations/edit.html.erb
@@ -1,3 +1,4 @@
<%= user_score(current_user) %>
<h1>Edit <%= resource_name.to_s.humanize %></h1> <h1>Edit <%= resource_name.to_s.humanize %></h1>


<p class="profile-avatar"><b>Profile Image</b><br/> <p class="profile-avatar"><b>Profile Image</b><br/>
Expand Down
4 changes: 2 additions & 2 deletions app/views/users/index.html.erb
Expand Up @@ -12,8 +12,8 @@
</tr> </tr>


<% @users.each_with_index do |user, i| %> <% @users.each_with_index do |user, i| %>
<tr class="user-record" id="<%= user.id %>"> <tr class="user-record <%= 'self' if user == current_user %>" id="<%= user.id %>">
<td><%= i+1 + offset %></td> <td><%= user.rank || 'undefined' %></td>
<td class="user"><%= gravatar_image(user.email, 24, style: "margin-right: 0.5em") %><%= user.username %></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.solution_count || 0 %></td>
<td><%= user.score || 0 %></td> <td><%= user.score || 0 %></td>
Expand Down
8 changes: 8 additions & 0 deletions lib/tasks/user_rank.rake
@@ -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.