Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add subscription model

  • Loading branch information...
commit a9bce0e4c115d060d65449414a4e0c141d516814 1 parent b617186
@ddollar ddollar authored
View
2  app/models/rubygem.rb
@@ -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")
View
6 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
View
4 app/models/user.rb
@@ -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
View
13 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
View
9 db/schema.rb
@@ -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
@@ -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
View
4 test/factories/subscription.rb
@@ -0,0 +1,4 @@
+Factory.define :subscription do |subscription|
+ subscription.association(:rubygem)
+ subscription.association(:user)
+end
View
13 test/unit/rubygem_test.rb
@@ -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
View
20 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
View
15 test/unit/user_test.rb
@@ -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
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.