Skip to content

Commit

Permalink
Add video reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-huang committed Feb 9, 2014
1 parent eb3a3f5 commit 8b6b9be
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 3 deletions.
1 change: 1 addition & 0 deletions app/controllers/videos_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def index

def show
@video = Video.find(params[:id])
@reviews = @video.reviews
end

def search
Expand Down
4 changes: 4 additions & 0 deletions app/models/review.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Review < ActiveRecord::Base
belongs_to :video
belongs_to :user
end
1 change: 1 addition & 0 deletions app/models/video.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Video < ActiveRecord::Base
belongs_to :category
has_many :reviews
validates_presence_of :title, :description

def self.search_by_title(search_term)
Expand Down
16 changes: 15 additions & 1 deletion app/views/videos/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,18 @@
%p #{@video.description}
.actions
%a.btn.btn-primary(href="") Watch Now
%a.btn.btn-default(href="") + My Queue
%a.btn.btn-default(href="") + My Queue
%section.reviews.container
.row
.col-sm-10.col-sm-offset-1
%header
%h3 User Reviews (#{@reviews.count})
%ul
- @reviews.each do |review|
%article.review
%li.row
.col-sm-2
%span Rating: #{review.rating} / 5
%p by <a href="">#{review.user.full_name}</a>
.col-sm-8
%p= review.content
10 changes: 10 additions & 0 deletions db/migrate/20140209071251_create_reviews.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateReviews < ActiveRecord::Migration
def change
create_table :reviews do |t|
t.integer :user_id
t.integer :video_id
t.integer :rating
t.text :content
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,14 +11,21 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20140118082301) do
ActiveRecord::Schema.define(version: 20140209071251) do

create_table "categories", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "reviews", force: true do |t|
t.integer "user_id"
t.integer "video_id"
t.integer "rating"
t.text "content"
end

create_table "users", force: true do |t|
t.string "email"
t.string "password_digest"
Expand Down
7 changes: 6 additions & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
Video.create(title: 'Inception', description: "Dom Cobb is a skilled thief, the absolute best in the dangerous art of extraction, stealing valuable secrets from deep within the subconscious during the dream state, when the mind is at its most vulnerable.",
small_cover: '/tmp/inception.jpg', big_cover: '/tmp/inception_large.jpg', category: drama)

Video.create(title: 'Rush', description: "Set against the sexy, glamorous golden age of Formula 1 racing in the 1970s, the film is based on the true story of a great sporting rivalry between handsome English playboy James Hunt (Hemsworth), and his methodical, brilliant opponent, Austrian driver Niki Lauda (Bruhl).",
rush = Video.create(title: 'Rush', description: "Set against the sexy, glamorous golden age of Formula 1 racing in the 1970s, the film is based on the true story of a great sporting rivalry between handsome English playboy James Hunt (Hemsworth), and his methodical, brilliant opponent, Austrian driver Niki Lauda (Bruhl).",
small_cover: '/tmp/rush.jpg', big_cover: '/tmp/rush_large.jpg', category: action)

Video.create(title: 'The Dark Knight', description: "Batman raises the stakes in his war on crime. With the help of Lieutenant Jim Gordon and District Attorney Harvey Dent, Batman sets out to dismantle the remaining criminal organizations that plague the city streets.",
Expand Down Expand Up @@ -48,3 +48,8 @@

Video.create(title: 'Gravity', description: "Dr. Ryan Stone (Sandra Bullock) is a brilliant medical engineer on her first shuttle mission, with veteran astronaut Matt Kowalsky (George Clooney) in command of his last flight before retiring. But on a seemingly routine spacewalk, disaster strikes.",
small_cover: '/tmp/gravity.jpg', big_cover: '/tmp/gravity_large.jpg', category: sci_fi)

michael = User.create(email: "michael@gmail.com", password: "password", full_name: "Michael Huang")

Review.create(user: michael, video: rush, rating: 4, content: "This is a awesome movie!")
Review.create(user: michael, video: rush, rating: 2, content: "This is a horrible movie...")
9 changes: 9 additions & 0 deletions spec/controllers/videos_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
expect(assigns(:video)).to eq(video)
end

it "sets @reviews for authenticated users" do
session[:user_id] = Fabricate(:user).id
video = Fabricate(:video)
review1 = Fabricate(:review, video: video)
review2 = Fabricate(:review, video: video)
get :show, id: video.id
expect(assigns(:reviews)).to match_array ([review1, review2])
end

it "redirects the user to the sign in page for unauthenticated users" do
video = Fabricate(:video)
get :show, id: video.id
Expand Down
4 changes: 4 additions & 0 deletions spec/fabricators/review_fabricator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fabricator(:review) do
rating { (1..5).to_a.sample }
content { Faker::Lorem.paragraph(3) }
end

0 comments on commit 8b6b9be

Please sign in to comment.