Permalink
Browse files

added mailers and reverse relationship

  • Loading branch information...
1 parent 57a740c commit 460b706f7b95f006563cee1a1b4abaf887d63fbc @drapergeek committed Mar 13, 2012
View
8 :w
@@ -0,0 +1,8 @@
+class FollowingMailer < ActionMailer::Base
+ default from: "from@example.com"
+
+ def followed_notification(follower, followed_user)
+ @follower = follower
+ mail(:to=>followed_user.email, :subject=>"#{@follower.email} is now following you")
+ end
+end
@@ -3,6 +3,8 @@ class FollowingRelationshipsController < ApplicationController
def create
user = User.find(params[:user_id])
current_user.follow(user)
+ FollowingMailer.followed_notification(current_user, user).deliver
redirect_to user, notice: "Now following"
end
+
end
@@ -1,5 +1,2 @@
module UsersHelper
- def can_follow(user)
- current_user != user && !current_user.followed_users.include?(user)
- end
end
@@ -0,0 +1,8 @@
+class FollowingMailer < ActionMailer::Base
+ default from: "from@example.com"
+
+ def followed_notification(follower, followed_user)
+ @follower = follower
+ mail(:to=>followed_user.email, :subject=>"#{@follower.email} is now following you")
+ end
+end
View
@@ -2,16 +2,31 @@ class User < ActiveRecord::Base
include Clearance::User
has_many :shouts
- has_many :following_relationships, foreign_key: "following_user_id"
- has_many :followed_users, through: :following_relationships
+ has_many :followed_user_relationships, foreign_key: "following_user_id", class_name: "FollowingRelationship"
+ has_many :followed_users, through: :followed_user_relationships
+
+ has_many :following_user_relationships, foreign_key: "followed_user_id", class_name: "FollowingRelationship"
+ has_many :followers, through: :following_user_relationships, source: :following_user
def create_shout(medium)
new_shout = shouts.create(medium: medium)
end
def follow(followed_user)
- following_relationships.create(followed_user: followed_user)
+ followed_users << followed_user
+ end
+
+ def can_follow?(user)
+ !self_and_followed_user_ids.include?(user.id)
end
+ def can_unfollow?(user)
+ followed_user_ids.include?(user.id)
+ end
+
+ private
+ def self_and_followed_user_ids
+ [id] + followed_user_ids
+ end
end
@@ -0,0 +1 @@
+<%= link_to "View Their Shouts", user_url(@follower) %>
@@ -0,0 +1,14 @@
+require 'spec_helper'
+
+describe FollowingMailer do
+
+ it "sends a #followed_notification" do
+ follower = create(:user)
+ followed_user = create(:user)
+ mail = FollowingMailer.followed_notification(follower, followed_user)
+ mail.subject.should == "#{follower.email} is now following you"
+ mail.body.should =~ /#{user_url(follower)}/
+ mail.to.should == [followed_user.email]
+
+ end
+end
View
@@ -16,4 +16,24 @@
follower.follow(followed_user)
follower.followed_users.should == [followed_user]
end
+
+ it "knows if it #can_follow? a user" do
+ follower = create(:user)
+ followed_user = create(:user)
+ unfollowed_user = create(:user)
+ follower.follow(followed_user)
+ follower.can_follow?(follower).should be_false
+ follower.can_follow?(followed_user).should be_false
+ follower.can_follow?(unfollowed_user).should be_true
+ end
+
+ it "knows if it #can_unfollow? a user" do
+ follower = create(:user)
+ followed_user = create(:user)
+ unfollowed_user = create(:user)
+ follower.follow(followed_user)
+ follower.can_unfollow?(follower).should be_false
+ follower.can_unfollow?(followed_user).should be_true
+ follower.can_unfollow?(unfollowed_user).should be_false
+ end
end
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class FollowingMailerTest < ActionMailer::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end

0 comments on commit 460b706

Please sign in to comment.