From fed22b92dd594209d4d4217bd4b5e90408d23de9 Mon Sep 17 00:00:00 2001 From: Jared Pace Date: Fri, 13 Aug 2010 10:57:42 -0400 Subject: [PATCH] Allow users to watch projects and have projects through those watchers --- app/models/user.rb | 19 ++++++++++++++++++- app/models/watcher.rb | 9 ++++++++- spec/controllers/apps_controller_spec.rb | 2 +- spec/models/user_spec.rb | 29 +++++++++++++++++++++++++++++ spec/models/watcher_spec.rb | 17 ++++++++++++++--- 5 files changed, 70 insertions(+), 6 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index b469252..1962320 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -8,7 +8,24 @@ class User field :name field :admin, :type => Boolean, :default => false + + after_destroy :destroy_watchers validates_presence_of :name - + + # Mongoid doesn't seem to currently support + # referencing embedded documents + def watchers + App.all.map(&:watchers).flatten.select {|w| w.user_id == id} + end + + def apps + App.where('watchers.user_id' => id) + end + + protected + + def destroy_watchers + watchers.each(&:destroy) + end end diff --git a/app/models/watcher.rb b/app/models/watcher.rb index c5b5f00..9081156 100644 --- a/app/models/watcher.rb +++ b/app/models/watcher.rb @@ -5,7 +5,14 @@ class Watcher field :email embedded_in :app, :inverse_of => :watchers + referenced_in :user - validates_presence_of :email + validate :ensure_user_or_email + + protected + + def ensure_user_or_email + errors.add(:base, "You must specify either a user or an email address") unless user.present? || email.present? + end end diff --git a/spec/controllers/apps_controller_spec.rb b/spec/controllers/apps_controller_spec.rb index 855a6fd..a5129ca 100644 --- a/spec/controllers/apps_controller_spec.rb +++ b/spec/controllers/apps_controller_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe AppsController, :focused => true do +describe AppsController do it_requires_authentication it_requires_admin_privileges :for => {:new => :get, :edit => :get, :create => :post, :update => :put, :destroy => :delete} diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 789208d..3722456 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -10,4 +10,33 @@ describe User do end end + context 'Watchers' do + + it 'has many watchers' do + user = Factory(:user) + watcher = Factory(:watcher, :user => user, :email => nil) + user.watchers.should_not be_empty + user.watchers.should include(watcher) + end + + it "destroys any related watchers when it is destroyed" do + user = Factory(:user) + app = Factory(:app) + watcher = Factory(:watcher, :app => app, :user => user, :email => nil) + user.watchers.should_not be_empty + user.destroy + app.reload.watchers.should_not include(watcher) + end + + it "has many apps through watchers" do + user = Factory(:user) + watched_app = Factory(:app) + unwatched_app = Factory(:app) + watcher = Factory(:watcher, :app => watched_app, :user => user, :email => nil) + user.apps.all.should include(watched_app) + user.apps.all.should_not include(unwatched_app) + end + + end + end diff --git a/spec/models/watcher_spec.rb b/spec/models/watcher_spec.rb index 7b507c6..b980d0e 100644 --- a/spec/models/watcher_spec.rb +++ b/spec/models/watcher_spec.rb @@ -3,11 +3,22 @@ require 'spec_helper' describe Watcher do context 'validations' do - it 'requires an email address' do - watcher = Factory.build(:watcher, :email => nil) + it 'requires an email address or an associated user' do + watcher = Factory.build(:watcher, :email => nil, :user => nil) watcher.should_not be_valid - watcher.errors[:email].should include("can't be blank") + watcher.errors[:base].should include("You must specify either a user or an email address") + + watcher.email = 'watcher@example.com' + watcher.should be_valid + + watcher.email = nil + watcher.should_not be_valid + + watcher.user = Factory(:user) + watcher.should be_valid end + + end end -- libgit2 0.21.2