Skip to content

Commit

Permalink
add subscription model
Browse files Browse the repository at this point in the history
  • Loading branch information
ddollar committed Aug 26, 2009
1 parent b617186 commit a9bce0e
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 1 deletion.
2 changes: 2 additions & 0 deletions app/models/rubygem.rb
Expand Up @@ -3,6 +3,8 @@ class Rubygem < ActiveRecord::Base

has_many :owners, :through => :ownerships, :source => :user
has_many :ownerships
has_many :subscribers, :through => :subscriptions, :source => :user
has_many :subscriptions
has_many :versions, :dependent => :destroy, :order => "created_at desc, number desc" do
def latest
self.find(:first, :order => "updated_at desc")
Expand Down
6 changes: 6 additions & 0 deletions app/models/subscription.rb
@@ -0,0 +1,6 @@
class Subscription < ActiveRecord::Base
belongs_to :rubygem
belongs_to :user

validates_uniqueness_of :rubygem_id, :scope => :user_id
end
4 changes: 4 additions & 0 deletions app/models/user.rb
Expand Up @@ -4,7 +4,11 @@ class User < ActiveRecord::Base
has_many :rubygems, :through => :ownerships,
:order => "name ASC",
:conditions => { 'ownerships.approved' => true }
has_many :subscribed_gems, :through => :subscriptions,
:source => :rubygem,
:order => "name ASC"
has_many :ownerships
has_many :subscriptions
before_create :generate_api_key

protected
Expand Down
13 changes: 13 additions & 0 deletions db/migrate/20090825173917_create_subscriptions.rb
@@ -0,0 +1,13 @@
class CreateSubscriptions < ActiveRecord::Migration
def self.up
create_table :subscriptions do |t|
t.references :rubygem
t.references :user
t.timestamps
end
end

def self.down
drop_table :subscriptions
end
end
9 changes: 8 additions & 1 deletion db/schema.rb
Expand Up @@ -9,7 +9,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20090821044418) do
ActiveRecord::Schema.define(:version => 20090825173917) do

create_table "delayed_jobs", :force => true do |t|
t.integer "priority", :default => 0
Expand Down Expand Up @@ -72,6 +72,13 @@

add_index "rubygems", ["name"], :name => "index_rubygems_on_name"

create_table "subscriptions", :force => true do |t|
t.integer "rubygem_id"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "users", :force => true do |t|
t.string "email"
t.string "encrypted_password", :limit => 128
Expand Down
4 changes: 4 additions & 0 deletions test/factories/subscription.rb
@@ -0,0 +1,4 @@
Factory.define :subscription do |subscription|
subscription.association(:rubygem)
subscription.association(:user)
end
13 changes: 13 additions & 0 deletions test/unit/rubygem_test.rb
Expand Up @@ -123,6 +123,19 @@ class RubygemTest < ActiveSupport::TestCase
end
end

context "with subscribed users" do
setup do
@subscribed_user = Factory(:user)
@unsubscribed_user = Factory(:user)
Factory(:subscription, :rubygem => @rubygem, :user => @subscribed_user)
end

should "only fetch the subscribed users with #subscribers" do
assert_contains @rubygem.subscribers, @subscribed_user
assert_does_not_contain @rubygem.subscribers, @unsubscribed_user
end
end

should "return current version" do
assert_equal @rubygem.versions.first, @rubygem.versions.current
end
Expand Down
20 changes: 20 additions & 0 deletions test/unit/subscription_test.rb
@@ -0,0 +1,20 @@
require File.dirname(__FILE__) + '/../test_helper'

class SubscriptionTest < ActiveSupport::TestCase
should_belong_to :rubygem
should_belong_to :user

context "with a linkset" do
setup do
@subscription = Factory.create(:subscription)
end

subject { @subscription }

should_validate_uniqueness_of :rubygem_id, :scoped_to => :user_id

should "be valid with factory" do
assert_valid @subscription
end
end
end
15 changes: 15 additions & 0 deletions test/unit/user_test.rb
Expand Up @@ -3,6 +3,8 @@
class UserTest < ActiveSupport::TestCase
should_have_many :rubygems, :through => :ownerships
should_have_many :ownerships
should_have_many :subscribed_gems, :through => :subscriptions
should_have_many :subscriptions

context "with a user" do
setup do
Expand All @@ -21,5 +23,18 @@ class UserTest < ActiveSupport::TestCase

assert_equal [my_rubygem], @user.rubygems
end

context "with subscribed gems" do
setup do
@subscribed_gem = Factory(:rubygem)
@unsubscribed_gem = Factory(:rubygem)
Factory(:subscription, :user => @user, :rubygem => @subscribed_gem)
end

should "only fetch the subscribed gems with #subscribed_gems" do
assert_contains @user.subscribed_gems, @subscribed_gem
assert_does_not_contain @user.subscribed_gems, @unsubscribed_gem
end
end
end
end

0 comments on commit a9bce0e

Please sign in to comment.