Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Week 4 part 1 #7

Merged
merged 2 commits into from
Jul 14, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/assets/stylesheets/user.sass
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ section.user
h2
font-size: 16px
font-weight: normal
button
a
float: right

section.user_reviews
Expand Down
18 changes: 18 additions & 0 deletions app/controllers/relationships_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class RelationshipsController < ApplicationController
before_filter :require_user
def index
@relationships = current_user.following_relationships
end

def destroy
relationship = Relationship.find(params[:id])
relationship.destroy if relationship.follower == current_user
redirect_to people_path
end

def create
leader = User.find(params[:leader_id])
Relationship.create(leader_id: params[:leader_id], follower: current_user) if current_user.can_follow?(leader)
redirect_to people_path
end
end
6 changes: 5 additions & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class UsersController < ApplicationController

before_filter :require_user, only: [:show]
def new
@user = User.new
end
Expand All @@ -13,6 +13,10 @@ def create
end
end

def show
@user = User.find(params[:id])
end

private

def user_params
Expand Down
4 changes: 4 additions & 0 deletions app/models/relationship.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Relationship < ActiveRecord::Base
belongs_to :follower, class_name: "User"
belongs_to :leader, class_name: "User"
end
12 changes: 11 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
class User < ActiveRecord::Base
validates_presence_of :email, :password, :full_name
validates_uniqueness_of :email
has_many :reviews
has_many :reviews, -> { order "created_at DESC" }

has_secure_password validations: false

has_many :queue_items, -> { order(:position) }

has_many :following_relationships, class_name: "Relationship", foreign_key: :follower_id
has_many :leading_relationships, class_name: "Relationship", foreign_key: :leader_id

def normalize_queue_item_positions
queue_items.each_with_index do |queue_item, index|
queue_item.update_attributes(position: index+1)
Expand All @@ -17,4 +20,11 @@ def queued_video?(video)
queue_items.map(&:video).include?(video)
end

def follows?(another_user)
following_relationships.map(&:leader).include?(another_user)
end

def can_follow?(another_user)
!(self.follows?(another_user) || self == another_user)
end
end
21 changes: 21 additions & 0 deletions app/views/relationships/index.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
%section.people
%header
%h2 People I Follow
%table.table
%thead
%tr
%th(width="30%")
%th(width="20%") Videos in Queue
%th(width="20%") Followers
%th(width="30%") Unfollow
%tbody
- @relationships.each do |relationship|
%tr
%td
%img(src="http://www.gravatar.com/avatar/#{Digest::MD5.hexdigest(relationship.leader.email.downcase)}?s=40")
= link_to relationship.leader.full_name, relationship.leader
%td.extra-padding= relationship.leader.queue_items.count
%td.extra-padding= relationship.leader.leading_relationships.count
%td.extra-padding
= link_to relationship, method: :delete do
%i.glyphicon.glyphicon-remove
6 changes: 3 additions & 3 deletions app/views/shared/_header.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
= link_to "MyFLiX"
- if current_user
%ul.col-md-4.clearfix
%li= link_to "Videos"
%li= link_to "My Queue"
%li= link_to "People"
%li= link_to "Videos", home_path
%li= link_to "My Queue", my_queue_path
%li= link_to "People", people_path
= form_tag search_videos_path, method: "get", class: 'col-md-5 navbar-form' do
.form-group
= text_field_tag 'search', params[:search], class: 'form-control', placeholder: 'Search for videos here'
Expand Down
36 changes: 36 additions & 0 deletions app/views/users/show.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
%section.user.container
.row
.col-sm-10.col-sm-offset-1
%article
%header
%img(src="http://www.gravatar.com/avatar/#{Digest::MD5.hexdigest(@user.email.downcase)}?s=40")
%h2 #{@user.full_name}'s video collections (#{@user.queue_items.count})
= link_to "Follow", relationships_path(leader_id: @user.id), class: "btn btn-default", method: :post if current_user.can_follow?(@user)
%table.table
%thead
%tr
%th(width="30%") Video Title
%th(width="15%") Genre
%tbody
- @user.queue_items.each do |queue_item|
%tr
%td
= link_to queue_item.video.title, queue_item.video
%td
= link_to queue_item.category_name, queue_item.category

%section.user_reviews.container
.row
.col-sm-10.col-sm-offset-1
%header
%h3 #{@user.full_name}'s Reviews (#{@user.reviews.count})
%ul
- @user.reviews.each do |review|
%article.review
%li.row
.col-sm-2
%p
= link_to review.video.title, review.video
%col Rating: #{review.rating} / 5
.col-sm-8
%p= review.content
4 changes: 3 additions & 1 deletion app/views/videos/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
%li.row
.col-sm-2
%span Rating: #{review.rating} / 5
%p by <a href="">#{review.user.full_name}</a>
%p
by
= link_to review.user.full_name, review.user
.col-sm-8
%p= review.content
5 changes: 5 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
end
resources :reviews, only: [:create]
end

resources :users, only: [:show]
get 'people', to: 'relationships#index'
resources :relationships, only: [:create, :destroy]

resources :categories
resources :queue_items, only: [:create, :destroy]
post 'update_queue', to: 'queue_items#update_queue'
Expand Down
8 changes: 8 additions & 0 deletions db/migrate/20140714122019_create_relationships.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class CreateRelationships < ActiveRecord::Migration
def change
create_table :relationships do |t|
t.integer :leader_id, :follower_id
t.timestamps
end
end
end
9 changes: 8 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: 20140707101635) do
ActiveRecord::Schema.define(version: 20140714122019) do

create_table "categories", force: true do |t|
t.string "name"
Expand All @@ -27,6 +27,13 @@
t.datetime "updated_at"
end

create_table "relationships", force: true do |t|
t.integer "leader_id"
t.integer "follower_id"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "reviews", force: true do |t|
t.integer "user_id"
t.integer "video_id"
Expand Down
87 changes: 87 additions & 0 deletions spec/controllers/relationships_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
require 'spec_helper'

describe RelationshipsController do
describe "GET index" do
it "sets @relationships to the current user's following relationships" do
alice = Fabricate(:user)
set_current_user(alice)
bob = Fabricate(:user)
relationship = Fabricate(:relationship, follower: alice, leader: bob)
get :index
expect(assigns(:relationships)).to eq([relationship])
end

it_behaves_like "requires sign in" do
let(:action) { get :index }
end
end

describe "DELETE destroy" do
it_behaves_like "requires sign in" do
let(:action) { delete :destroy, id: 4 }
end

it "redirects to the people page" do
alice = Fabricate(:user)
set_current_user(alice)
bob = Fabricate(:user)
relationship = Fabricate(:relationship, follower: alice, leader: bob)
delete :destroy, id: relationship
expect(response).to redirect_to people_path
end

it "deletes the relationship if the current user is the follower" do
alice = Fabricate(:user)
set_current_user(alice)
bob = Fabricate(:user)
relationship = Fabricate(:relationship, follower: alice, leader: bob)
delete :destroy, id: relationship
expect(Relationship.count).to eq(0)
end
it "does not delete the relationship if the current user is not the follower" do
alice = Fabricate(:user)
set_current_user(alice)
bob = Fabricate(:user)
charlie = Fabricate(:user)
relationship = Fabricate(:relationship, follower: charlie, leader: bob)
delete :destroy, id: relationship
expect(Relationship.count).to eq(1)
end
end

describe "POST create" do
it_behaves_like "requires sign in" do
let(:action) { post :create, leader_id: 3 }
end

it "redirects to the people page" do
alice = Fabricate(:user)
bob = Fabricate(:user)
set_current_user(alice)
post :create, leader_id: bob.id
expect(response).to redirect_to people_path
end

it "creates a relationship that the current user follows the leader" do
alice = Fabricate(:user)
bob = Fabricate(:user)
set_current_user(alice)
post :create, leader_id: bob.id
expect(alice.following_relationships.first.leader).to eq(bob)
end
it "does not create a relationship if the current user already follows the leader" do
alice = Fabricate(:user)
bob = Fabricate(:user)
set_current_user(alice)
Fabricate(:relationship, leader: bob, follower: alice)
post :create, leader_id: bob.id
expect(Relationship.count).to eq(1)
end
it "does not allow one to follow oneself" do
alice = Fabricate(:user)
set_current_user(alice)
post :create, leader_id: alice.id
expect(Relationship.count).to eq(0)
end
end
end
13 changes: 13 additions & 0 deletions spec/controllers/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,17 @@
end
end
end

describe "GET show" do
it_behaves_like "requires sign in" do
let(:action) { get :show, id: 3}
end

it "sets @user" do
set_current_user
alice = Fabricate(:user)
get :show, id: alice.id
expect(assigns(:user)).to eq(alice)
end
end
end
3 changes: 3 additions & 0 deletions spec/fabricators/relationship_fabricator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fabricator(:relationship) do

end
24 changes: 24 additions & 0 deletions spec/features/user_following_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'spec_helper'

feature 'User following' do
scenario "user follows and unfollows someone" do
alice = Fabricate(:user)
category = Fabricate(:category)
video = Fabricate(:video, category: category)
Fabricate(:review, user: alice, video: video)

sign_in
click_on_video_on_home_page(video)

click_link alice.full_name
click_link "Follow"
expect(page).to have_content(alice.full_name)

unfollow(alice)
expect(page).not_to have_content(alice.full_name)
end
end

def unfollow(user)
find("a[data-method='delete']").click
end
2 changes: 1 addition & 1 deletion spec/features/user_interacts_with_queue_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def expect_link_not_to_be_seen(link_text)

def add_video_to_queue(video)
visit home_path
find("a[href='/videos/#{video.id}']").click
click_on_video_on_home_page(video)
click_link "+ My Queue"
end

Expand Down
16 changes: 16 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
it { should validate_presence_of(:full_name) }
it { should validate_uniqueness_of(:email) }
it { should have_many(:queue_items).order(:position)}
it { should have_many(:reviews).order("created_at DESC")}

describe "#queued_video?" do
it "returns true when the user queued the video" do
Expand All @@ -20,4 +21,19 @@
user.queued_video?(video).should be false
end
end

describe "#follows?" do
it "returns true if the user has a following relationship with another user" do
alice = Fabricate(:user)
bob = Fabricate(:user)
Fabricate(:relationship, leader: bob, follower: alice)
expect(alice.follows?(bob)).to be_truthy
end
it "returns false if the user does not have a following relationship with another user" do
alice = Fabricate(:user)
bob = Fabricate(:user)
Fabricate(:relationship, leader: alice, follower: bob)
expect(alice.follows?(bob)).to be_falsey
end
end
end
3 changes: 3 additions & 0 deletions spec/support/macros.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ def sign_in(a_user=nil)
fill_in "Email Address", with: user.email
fill_in "Password", with: user.password
click_button "Sign In"
end

def click_on_video_on_home_page(video)
find("a[href='/videos/#{video.id}']").click
end