Permalink
Browse files

Added reverse follower relationships

  • Loading branch information...
adarsh committed Mar 13, 2012
1 parent c43cecb commit 1dea819c5cd67b5632a2df447f5ff5a3c58f7362
View
@@ -14,6 +14,7 @@ group :test do
gem 'rspec-rails'
gem 'factory_girl_rails'
gem 'bourne'
+ gem 'email_spec'
end
# Gems used only for assets and not required
View
@@ -48,6 +48,9 @@ GEM
diesel (0.1.5)
railties
diff-lcs (1.1.3)
+ email_spec (1.2.1)
+ mail (~> 2.2)
+ rspec (~> 2.0)
erubis (2.7.0)
execjs (1.3.0)
multi_json (~> 1.0)
@@ -155,6 +158,7 @@ DEPENDENCIES
bourne
clearance
coffee-rails (~> 3.2.1)
+ email_spec
factory_girl_rails
jquery-rails
paperclip
@@ -0,0 +1,8 @@
+class FollowingRelationshipsController < ApplicationController
+ def create
+ user = User.find(params[:user_id])
+ # current_user.following_relationships.create(followed_user: user)
+ current_user.follow(user)
+ redirect_to user, notice: "Now following"
+ end
+end
Binary file not shown.
@@ -0,0 +1,10 @@
+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
Binary file not shown.
@@ -0,0 +1,4 @@
+class FollowingRelationship < ActiveRecord::Base
+ belongs_to :following_user, class_name: "User"
+ belongs_to :followed_user, class_name: "User"
+end
View
@@ -2,7 +2,40 @@ class User < ActiveRecord::Base
include Clearance::User
has_many :shouts
+ has_many :followed_user_relationships,
+ foreign_key: "following_user_id",
+ class_name: "FollowingRelationship"
+ has_many :followed_users,
+ through: :following_relationships
+
+ has_many :following_users,
+ through: :following_relationships
+ has_many :following_user_relationships,
+ foreign_key: "followed_user_id",
+ class_name: "FollowingRelationship"
+ has_many :followers,
+ through: :following_user_relationships,
+ source: :following_user
+
+ attr_accessible :email, :password
+
def create_shout(medium)
shouts.create(medium: medium)
end
+
+ def follow(user)
+ # following_relationships.create(followed_user: user)
+ followed_users << user
+ end
+
+ def can_follow?(user)
+ # return false if self == user
+ # return false if self.followed_users.include?(user)
+ !self_and_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) %>
@@ -36,6 +36,12 @@
</div>
</div>
+ <% flash.each do |key, value| %>
+ <div class="alert alert-<%= key == :notice ? "success" : "error" %>">
+ <%= value %>
+ </div>
+ <% end %>
+
<div class="container">
<div class="content">
<div class="row">
Binary file not shown.
@@ -1,4 +1,9 @@
<h1><%= @user.email %></h1>
+
+<% if current_user.can_follow?(@user) %>
+ <%= button_to "Follow", user_following_relationships_path(@user), class: "btn btn-warning" -%>
+<% end -%>
+
<div id="shouts">
<%= render @shouts %>
</div>
View
@@ -1,7 +1,10 @@
Shouter::Application.routes.draw do
root to: "welcome#index"
resource :dashboard, only: [:show]
- resources :users, only: [:show]
+ resources :users, only: [:show] do
+ # nesting this gives /users/:user_id/following_relationships
+ resources :following_relationships, only: [:create]
+ end
resources :shouts, only: [:show]
resources :text_shouts, only: [:create]
resources :photo_shouts, only: [:create]
@@ -0,0 +1,12 @@
+class CreateFollowingRelationships < ActiveRecord::Migration
+ def change
+ create_table :following_relationships do |t|
+ t.belongs_to :following_user
+ t.belongs_to :followed_user
+
+ t.timestamps
+ end
+ add_index :following_relationships, :following_user_id
+ add_index :following_relationships, :followed_user_id
+ end
+end
View
@@ -11,7 +11,17 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120312184732) do
+ActiveRecord::Schema.define(:version => 20120313140648) do
+
+ create_table "following_relationships", :force => true do |t|
+ t.integer "following_user_id"
+ t.integer "followed_user_id"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+ add_index "following_relationships", ["followed_user_id"], :name => "index_following_relationships_on_followed_user_id"
+ add_index "following_relationships", ["following_user_id"], :name => "index_following_relationships_on_following_user_id"
create_table "photo_shouts", :force => true do |t|
t.datetime "created_at", :null => false
Binary file not shown.
@@ -0,0 +1,13 @@
+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.should have_subject("#{follower.email} is now following you")
+ mail.should deliver_to(followed_user.email)
+ mail.should have_body_text(/#{user_url(follower)}/)
+ end
+end
View
@@ -10,4 +10,23 @@
shout.persisted?.should == true
text_shout.persisted?.should == true
end
+
+ it "can #follow other users" do
+ follower = create(:user)
+ followed_user = create(:user)
+ 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
end
View
@@ -10,6 +10,9 @@
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
+ config.include(EmailSpec::Helpers)
+ config.include(EmailSpec::Matchers)
+
# ## Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
@@ -0,0 +1,9 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
+
+one:
+ following_user:
+ followed_user:
+
+two:
+ following_user:
+ followed_user:
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class FollowingMailerTest < ActionMailer::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class FollowingRelationshipTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end

0 comments on commit 1dea819

Please sign in to comment.