Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base: 294dcaf06a
...
compare: 8a15da4bf4
  • 5 commits
  • 13 files changed
  • 0 commit comments
  • 2 contributors
View
12 app/controllers/users_controller.rb
@@ -1,6 +1,5 @@
class UsersController < ApplicationController
- # GET /users
- # GET /users.json
+
def index
per = params[:per].present? ? params[:per].to_i : 25
@users = User.order("rank ASC, total_downloads DESC")
@@ -22,4 +21,13 @@ def index
format.json { render json: @users }
end
end
+
+ def show
+ @user = User.find params[:id]
+
+ respond_to do |format|
+ format.html
+ format.json { render json: @user }
+ end
+ end
end
View
4 app/models/fetcher.rb
@@ -12,7 +12,9 @@ def initialize(profile_id)
# UpdateUser - runs update_from_ruby_gems for the profile_id
def self.fetch_and_save!(profile_id)
user = User.find_or_initialize_by_profile_id(profile_id)
- user.update_from_rubygems
+ if user.total_downloads > 0
+ user.update_from_rubygems
+ end
end
def self.crawl_numeric(start_num, range)
View
5 app/models/rank_history.rb
@@ -0,0 +1,5 @@
+class RankHistory < ActiveRecord::Base
+ belongs_to :user
+
+ attr_accessible :rank
+end
View
17 app/models/user.rb
@@ -3,6 +3,7 @@ class User < ActiveRecord::Base
has_many :memberships
has_many :teams, through: :memberships
+ has_many :rank_histories
validates :handle, presence: true
validates :email, presence: true
@@ -26,11 +27,19 @@ def self.find_page_by_handle(handle, per)
def self.refresh_rank
self.transaction do
- self.update_all({ rank: nil })
- self.order("total_downloads DESC").each_with_index do |user, index|
- self.where(id: user.id).update_all(rank: index + 1)
- end
+ # SQL-FOO - NOTE this will only work on postgres!!
+ sql = "update users
+ set rank = d_rnk, updated_at = now()
+ from (SELECT id,row_number() OVER (ORDER BY total_downloads DESC) as d_rnk FROM users) as ranked
+ where ranked.id = users.id;"
+ ActiveRecord::Base.connection.execute(sql)
end
end
+ after_save :create_rank_history
+ def create_rank_history
+ if rank_changed?
+ self.rank_histories.create!(rank: self.rank)
+ end
+ end
end
View
2  app/views/teams/show.html.haml
@@ -4,7 +4,7 @@
- if @team.url.present?
%small
%i.icon-globe
- = link_to @team.url
+ = link_to @team.url, @team.url, target: "_blank"
%table.table.table-striped.table-bordered.table-condensed
%thead
View
18 app/views/users/show.html.haml
@@ -0,0 +1,18 @@
+.page-header
+ %h1
+ = gravatar_image_tag @user.email, alt: @user.handle, gravatar: { size: 50 }
+ #{@user.handle}'s Ranking History
+ %small
+ %i.icon-globe
+ = link_to rubygems_url(@user.profile_id), target: "_blank" do
+ Go to rubygems.org
+
+%table.table.table-striped
+ %thead
+ %th When
+ %th Rank
+
+ %tbody
+ = content_tag_for :tr, @user.rank_histories.order('created_at DESC') do |history|
+ %td= l history.created_at, format: :short
+ %td= history.rank
View
18 config/database.yml
@@ -4,20 +4,22 @@
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
development:
- adapter: sqlite3
- database: db/development.sqlite3
- pool: 5
- timeout: 5000
+ adapter: postgresql
+ database: rubygems-leader_development
+ encoding: unicode
+ username: postgres
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
- adapter: sqlite3
- database: db/test.sqlite3
- pool: 5
- timeout: 5000
+ adapter: postgresql
+ database: rubygems-leader_test
+ encoding: unicode
+ username: postgres
+
+# this does not matter - it's Heroku so it is ALWAYS postgres
production:
adapter: sqlite3
database: db/production.sqlite3
View
2  config/routes.rb
@@ -3,7 +3,7 @@
resources :memberships
end
- resources :users, only: :index
+ resources :users, only: %w(index show)
root to: "users#index"
View
11 db/migrate/20120723114007_create_rank_histories.rb
@@ -0,0 +1,11 @@
+class CreateRankHistories < ActiveRecord::Migration
+ def change
+ create_table :rank_histories do |t|
+ t.references :user
+ t.integer :rank
+
+ t.timestamps
+ end
+ add_index :rank_histories, :user_id
+ end
+end
View
11 db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120723100746) do
+ActiveRecord::Schema.define(:version => 20120723114007) do
create_table "memberships", :force => true do |t|
t.integer "team_id"
@@ -24,6 +24,15 @@
add_index "memberships", ["team_id"], :name => "index_memberships_on_team_id"
add_index "memberships", ["user_id"], :name => "index_memberships_on_user_id"
+ create_table "rank_histories", :force => true do |t|
+ t.integer "user_id"
+ t.integer "rank"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+ add_index "rank_histories", ["user_id"], :name => "index_rank_histories_on_user_id"
+
create_table "scheduled_updates", :force => true do |t|
t.integer "profile_id"
t.string "status"
View
11 lib/tasks/rubygems-leader.rake
@@ -0,0 +1,11 @@
+namespace :data do
+ desc "remove users with 0 total downloads"
+ task cleanup: :environment do
+ RAILS_ENV ||= :development
+ config = ActiveRecord::Base.configurations
+ ActiveRecord::Base.establish_connection(config[RAILS_ENV])
+ ActiveRecord::Base.transaction do
+ ActiveRecord::Base.connection.execute("delete from users where total_downloads = 0")
+ end
+ end
+end
View
4 spec/models/rank_history_spec.rb
@@ -0,0 +1,4 @@
+require 'spec_helper'
+
+describe RankHistory do
+end
View
8 spec/models/user_spec.rb
@@ -24,19 +24,19 @@
context 'class methods' do
before :each do
- @user = FactoryGirl.create(:user, rank: 19)
+ @user = FactoryGirl.create(:user, rank: 4, total_downloads: 100)
+ FactoryGirl.create(:user, rank: 1, total_downloads: 1)
end
it "returns the page for the user based on ranking and assumes that ranking will never be sparse" do
- User.find_page_by_handle(@user.handle, 2).should be(10)
+ User.find_page_by_handle(@user.handle, 2).should be(2)
end
it "refreshes the ranking" do
User.refresh_rank
- User.first.rank.should be(1)
+ User.find(@user.id).rank.should eq(1)
end
end
-
end

No commit comments for this range

Something went wrong with that request. Please try again.