Skip to content

Commit

Permalink
Simple tests for relationships, following and unfollowing added.
Browse files Browse the repository at this point in the history
  • Loading branch information
JessSumner committed Sep 4, 2015
1 parent be13d2d commit 2f8e925
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 11 deletions.
4 changes: 4 additions & 0 deletions app/models/relationship.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
class Relationship < ActiveRecord::Base
belongs_to :follower, class_name: "User"
belongs_to :followed, class_name: "User"
validates :follower_id, presence: true
validates :followed_id, presence: true
end
19 changes: 19 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
class User < ActiveRecord::Base
has_many :microposts, dependent: :destroy
has_many :active_relationships, class_name: "Relationship",
foreign_key: "follower_id",
dependent: :destroy
has_many :following, through: :active_relationships, source: :followed
attr_accessor :remember_token, :activation_token, :reset_token
before_save :downcase_email
before_create :create_activation_digest
Expand Down Expand Up @@ -76,6 +80,21 @@ def feed
Micropost.where("user_id = ?", id)
end

# Follows a user.
def follow(other_user)
active_relationships.create(followed_id: other_user.id)
end

# Unfollows a user.
def unfollow(other_user)
active_relationships.find_by(followed_id: other_user.id).destroy
end

# Returns true if the current user is following the other user.
def following?(other_user)
following.include?(other_user)
end

private

# Converts email to all lower case.
Expand Down
13 changes: 12 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20150817184927) do
ActiveRecord::Schema.define(version: 20150903200654) do

create_table "microposts", force: :cascade do |t|
t.text "content"
Expand All @@ -24,6 +24,17 @@
add_index "microposts", ["user_id", "created_at"], name: "index_microposts_on_user_id_and_created_at"
add_index "microposts", ["user_id"], name: "index_microposts_on_user_id"

create_table "relationships", force: :cascade do |t|
t.integer "follower_id"
t.integer "followed_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

add_index "relationships", ["followed_id"], name: "index_relationships_on_followed_id"
add_index "relationships", ["follower_id", "followed_id"], name: "index_relationships_on_follower_id_and_followed_id", unique: true
add_index "relationships", ["follower_id"], name: "index_relationships_on_follower_id"

create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
Expand Down
7 changes: 0 additions & 7 deletions test/fixtures/relationships.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,2 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
follower_id: 1
followed_id: 1

two:
follower_id: 1
followed_id: 1
21 changes: 18 additions & 3 deletions test/models/relationship_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
require 'test_helper'

class RelationshipTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end

def setup
@relationship = Relationship.new(follower_id: 1, followed_id: 2)
end

test "should be valid" do
assert @relationship.valid?
end

test "should require a follower_id" do
@relationship.follower_id = nil
assert @relationship.invalid?
end

test "should require a followed_id" do
@relationship.followed_id = nil
assert @relationship.invalid?
end
end
10 changes: 10 additions & 0 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,14 @@ def setup
@user.destroy
end
end

test "should follow and unfollow a user" do
testuser = users(:testuser)
archer = users(:archer)
assert_not testuser.following?(archer)
testuser.follow(archer)
assert testuser.following?(archer)
testuser.unfollow(archer)
assert_not testuser.following?(archer)
end
end

0 comments on commit 2f8e925

Please sign in to comment.