Skip to content
This repository
Browse code

Moving downloads into rubygems, making the homepage work

  • Loading branch information...
commit bf06b91c5e228831ebc26ad892206ae6117239dc 1 parent e68656c
Nick Quaranto qrush authored
5 app/controllers/home_controller.rb
... ... @@ -1,7 +1,8 @@
1 1 class HomeController < ApplicationController
2 2 def index
3 3 @count = Rubygem.count
4   - @created_gems = Rubygem.by_created_at(:desc).limited(5)
5   - @updated_gems = Rubygem.by_updated_at(:desc).limited(5)
  4 + @new = Rubygem.by_created_at(:desc).limited(5)
  5 + @updated = Version.by_created_at(:desc).limited(5)
  6 + @downloaded = Rubygem.by_downloads(:desc).limited(5)
6 7 end
7 8 end
3  app/metal/hostess.rb
@@ -21,8 +21,7 @@ class Hostess < Sinatra::Default
21 21 name = original_name[0..-2].join('-')
22 22 version = original_name[-1]
23 23 rubygem = Rubygem.find_by_name(name)
24   - version = rubygem.versions.find_by_number(version)
25   - version.increment!(:downloads)
  24 + rubygem.increment!(:downloads)
26 25 send_file(current_path)
27 26 else
28 27 halt 404
14 app/models/rubygem.rb
@@ -12,8 +12,6 @@ class Rubygem < ActiveRecord::Base
12 12 before_validation :build
13 13 after_save :store
14 14
15   - default_scope :order => 'name ASC'
16   -
17 15 def self.pull_spec(data)
18 16 Gem::Format.from_file_by_path(data.path).spec
19 17 end
@@ -26,6 +24,18 @@ def to_param
26 24 name.gsub(/[^\w_-]/, "")
27 25 end
28 26
  27 + def current_version
  28 + versions.first
  29 + end
  30 +
  31 + def with_version
  32 + "#{name} (#{current_version})"
  33 + end
  34 +
  35 + def with_downloads
  36 + "#{name} (#{downloads})"
  37 + end
  38 +
29 39 protected
30 40 def build
31 41 return unless self.spec
5 app/models/version.rb
... ... @@ -1,5 +1,10 @@
1 1 class Version < ActiveRecord::Base
  2 + include Pacecar
2 3 belongs_to :rubygem
3 4
4 5 default_scope :order => 'created_at DESC'
  6 +
  7 + def to_s
  8 + number
  9 + end
5 10 end
39 app/views/home/index.html.haml
@@ -13,27 +13,18 @@
13 13 .count= number_with_delimiter(@count)
14 14 .what gems indexed since may 2009
15 15 .clear
16   - .list.grid_4
17   - %h3 Updated Gems
18   - %ul
19   - %li jekyll (0.5.1)
20   - %li haml (0.1.2)
21   - %li roodi (1.2.3)
22   - %li beardo (0.0.1)
23   - %li jeweler (1.0.42)
24   - .list.grid_4
25   - %h3 New Gems
26   - %ul
27   - %li awesome (0.0.1)
28   - %li rstuff (1.0.0)
29   - %li dbhelper (4.1.8)
30   - %li acts_as_fu (1.0.0)
31   - %li RTrickery (0.0.0)
32   - .list.grid_4
33   - %h3 Most Downloads
34   - %ul
35   - %li rails (12,147)
36   - %li instantrails (9,234)
37   - %li rmagick (3,403)
38   - %li rake (2,302)
39   - %li net/ssh (2,128)
  16 +.list.grid_4
  17 + %h3 Updated Gems
  18 + %ul
  19 + -@updated.each do |version|
  20 + %li= link_to "#{version.rubygem} (#{version})", version.rubygem
  21 +.list.grid_4
  22 + %h3 New Gems
  23 + %ul
  24 + -@new.each do |gem|
  25 + %li= link_to gem.with_version, gem
  26 +.list.grid_4
  27 + %h3 Most Downloads
  28 + %ul
  29 + -@downloaded.each do |gem|
  30 + %li= link_to gem.with_downloads, gem
2  app/views/rubygems/show.html.haml
@@ -16,7 +16,7 @@
16 16 %p= @current_version.authors
17 17 .block.red
18 18 %h3 Downloads
19   - %p= @current_version.downloads
  19 + %p= number_with_delimiter(@gem.downloads)
20 20 .block.red
21 21 %h3 Last Update
22 22 %p= @current_version.created_at.to_date.to_formatted_s(:long)
11 db/migrate/20090530145834_move_downloads_to_rubygems.rb
... ... @@ -0,0 +1,11 @@
  1 +class MoveDownloadsToRubygems < ActiveRecord::Migration
  2 + def self.up
  3 + remove_column :versions, :downloads
  4 + add_column :rubygems, :downloads, :integer, :default => 0
  5 + end
  6 +
  7 + def self.down
  8 + add_column :versions, :downloads, :integer, :default => 0
  9 + remove_column :rubygems, :downloads
  10 + end
  11 +end
4 db/schema.rb
@@ -9,7 +9,7 @@
9 9 #
10 10 # It's strongly recommended to check this file into your version control system.
11 11
12   -ActiveRecord::Schema.define(:version => 20090527122658) do
  12 +ActiveRecord::Schema.define(:version => 20090530145834) do
13 13
14 14 create_table "dependencies", :force => true do |t|
15 15 t.string "name"
@@ -25,6 +25,7 @@
25 25 t.integer "user_id"
26 26 t.datetime "created_at"
27 27 t.datetime "updated_at"
  28 + t.integer "downloads", :default => 0
28 29 end
29 30
30 31 create_table "users", :force => true do |t|
@@ -43,7 +44,6 @@
43 44 create_table "versions", :force => true do |t|
44 45 t.string "authors"
45 46 t.text "description"
46   - t.integer "downloads", :default => 0
47 47 t.string "number"
48 48 t.integer "rubygem_id"
49 49 t.datetime "created_at"
5 public/stylesheets/application.css
@@ -170,6 +170,11 @@ h3 {
170 170 padding:0.3em;
171 171 }
172 172
  173 +.list li a {
  174 + color: inherit;
  175 + display: block;
  176 +}
  177 +
173 178 .list li:hover {
174 179 background:#600000;
175 180 cursor: pointer;
5 test/functional/home_controller_test.rb
@@ -4,8 +4,9 @@ class HomeControllerTest < ActionController::TestCase
4 4 context "on GET to index" do
5 5 setup do
6 6 mock(Rubygem).count { 1337 }
7   - mock(Rubygem).by_created_at(:desc).stub!.limited(5)
8   - mock(Rubygem).by_updated_at(:desc).stub!.limited(5)
  7 + mock(Rubygem).by_created_at(:desc).stub!.limited(5) { [] }
  8 + mock(Version).by_created_at(:desc).stub!.limited(5) { [] }
  9 + mock(Rubygem).by_downloads(:desc).stub!.limited(5) { [] }
9 10 get :index
10 11 end
11 12 should_respond_with :success
12 test/unit/rubygem_test.rb
@@ -14,6 +14,18 @@ class RubygemTest < ActiveSupport::TestCase
14 14 should "return name for #to_s" do
15 15 assert_equal @rubygem.name, @rubygem.to_s
16 16 end
  17 +
  18 + should "return latest version for #current_version" do
  19 + assert_equal @rubygem.versions.first, @rubygem.current_version
  20 + end
  21 +
  22 + should "return name with version for #with_version" do
  23 + assert_equal "#{@rubygem.name} (#{@rubygem.current_version})", @rubygem.with_version
  24 + end
  25 +
  26 + should "return name with downloads for #with_downloads" do
  27 + assert_equal "#{@rubygem.name} (#{@rubygem.downloads})", @rubygem.with_downloads
  28 + end
17 29 end
18 30
19 31 should "pull spec out of the given gem" do
10 test/unit/version_test.rb
@@ -2,4 +2,14 @@
2 2
3 3 class VersionTest < ActiveSupport::TestCase
4 4 should_belong_to :rubygem
  5 +
  6 + context "with a version" do
  7 + setup do
  8 + @version = Factory(:version)
  9 + end
  10 +
  11 + should "give number for #to_s" do
  12 + assert_equal @version.number, @version.to_s
  13 + end
  14 + end
5 15 end

0 comments on commit bf06b91

Please sign in to comment.
Something went wrong with that request. Please try again.