Skip to content

Commit

Permalink
Rework Version#updated/with_associated into Version#just_updated. Closes
Browse files Browse the repository at this point in the history
  • Loading branch information
qrush committed Mar 18, 2011
1 parent 523ecdb commit ef5e730
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 18 deletions.
2 changes: 1 addition & 1 deletion app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ def index
@downloads_count = Download.count
@latest = Rubygem.latest
@downloaded = Download.most_downloaded_today
@updated = Version.updated
@updated = Version.just_updated
end
end
22 changes: 11 additions & 11 deletions app/models/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,7 @@ def self.owned_by(user)

def self.subscribed_to_by(user)
where(:rubygem_id => user.subscribed_gem_ids).
order('created_at desc')
end

def self.with_associated
where("versions.rubygem_id IN (SELECT versions.rubygem_id FROM versions GROUP BY versions.rubygem_id HAVING COUNT(versions.id) > 1)").
includes(:rubygem).
by_built_at
by_created_at
end

def self.with_deps
Expand Down Expand Up @@ -54,6 +48,10 @@ def self.by_built_at
order("versions.built_at desc")
end

def self.by_created_at
order('created_at desc')
end

def self.with_indexed(reverse = false)
order_str = "rubygems.name asc"
order_str << ", position desc" if reverse
Expand All @@ -66,10 +64,12 @@ def self.most_recent
recent.find_by_platform('ruby') || recent.first || first
end

def self.updated(limit=5)
where("built_at <= ?", DateTime.now.utc).
with_associated.
limit(limit)
def self.just_updated
where("versions.rubygem_id IN (SELECT versions.rubygem_id FROM versions GROUP BY versions.rubygem_id HAVING COUNT(versions.id) > 1)").
joins(:rubygem).
indexed.
by_created_at.
limit(5)
end

def self.published(limit=5)
Expand Down
4 changes: 2 additions & 2 deletions test/functional/home_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class HomeControllerTest < ActionController::TestCase
stub(Rubygem).total_count { @rubygems_count }
stub(Rubygem).latest { [] }
stub(Download).most_downloaded_today { [] }
stub(Version).updated { [] }
stub(Version).just_updated { [] }
stub(Download).count { @downloads_count }
get :index
end
Expand All @@ -30,7 +30,7 @@ class HomeControllerTest < ActionController::TestCase
assert_received(Rubygem) { |subject| subject.total_count }
assert_received(Rubygem) { |subject| subject.latest }
assert_received(Download) { |subject| subject.most_downloaded_today }
assert_received(Version) { |subject| subject.updated }
assert_received(Version) { |subject| subject.just_updated }
assert_received(Download) { |subject| subject.count }
end
end
Expand Down
33 changes: 29 additions & 4 deletions test/unit/version_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,34 @@ class VersionTest < ActiveSupport::TestCase
should belong_to :rubygem
should have_many :dependencies

context "updated gems" do
setup do
Timecop.freeze
@existing_gem = Factory(:rubygem)
@second = Factory(:version, :rubygem => @existing_gem, :created_at => 1.day.ago)
@fourth = Factory(:version, :rubygem => @existing_gem, :created_at => 4.days.ago)

@another_gem = Factory(:rubygem)
@third = Factory(:version, :rubygem => @another_gem, :created_at => 3.days.ago)
@first = Factory(:version, :rubygem => @another_gem, :created_at => 1.minute.ago)
@yanked = Factory(:version, :rubygem => @another_gem, :created_at => 30.seconds.ago)
@yanked.yank!

@bad_gem = Factory(:rubygem)
@only_one = Factory(:version, :rubygem => @bad_gem, :created_at => 1.minute.ago)
end

teardown do
Timecop.return
end

should "order gems by created at and show only gems that have more than one version" do
versions = Version.just_updated
assert_equal 4, versions.size
assert_equal [@first, @second, @third, @fourth], versions
end
end

context "with a rubygem" do
setup do
@rubygem = Factory(:rubygem)
Expand Down Expand Up @@ -334,10 +362,6 @@ class VersionTest < ActiveSupport::TestCase
Factory(:ownership, :rubygem => @gem, :user => @user, :approved => true)
end

should "find versions that have other associated versions" do
assert_equal [@owned_one, @owned_two], Version.with_associated
end

should "return the owned gems from #owned_by" do
assert_contains Version.owned_by(@user).map(&:id), @owned_one.id
assert_contains Version.owned_by(@user).map(&:id), @owned_two.id
Expand Down Expand Up @@ -413,4 +437,5 @@ class VersionTest < ActiveSupport::TestCase
assert_equal @spec.date, @version.built_at
end
end

end

0 comments on commit ef5e730

Please sign in to comment.