Skip to content

Commit

Permalink
implement recent videos on the category model
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-huang committed Jan 17, 2014
1 parent e0d6bb7 commit 466f579
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
8 changes: 6 additions & 2 deletions app/models/category.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
class Category < ActiveRecord::Base
has_many :videos, ->{ order :title }
end
has_many :videos, ->{ order "created_at DESC" }

def recent_videos
videos.first(6)
end
end
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
get 'ui(/:action)', controller: 'ui'

get '/home', to: 'videos#home'

resources :videos do
collection do
post :search
end
end

resources :categories
end
38 changes: 36 additions & 2 deletions spec/models/category_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
require 'spec_helper'

describe Category do
it {should have_many(:videos)}
end
it { should have_many(:videos) }

describe "#recent_videos" do
it "returns the videos in the reverse chonical order by created at" do
comedies = Category.create(name: "Comedies")
yes_man = Video.create(title: "Yes Man", description: "amused movie", category: comedies)
the_mask = Video.create(title: "The Mask", description: "funny movie", category: comedies, created_at: 1.day.ago)
expect(comedies.recent_videos).to eq([yes_man, the_mask])
end

it "returns all the videos if there are less than 6 videos" do
comedies = Category.create(name: "Comedies")
yes_man = Video.create(title: "Yes Man", description: "amused movie", category: comedies)
the_mask = Video.create(title: "The Mask", description: "funny movie", category: comedies, created_at: 1.day.ago)
expect(comedies.recent_videos.count).to eq(2)
end

it "returns 6 videos if there are more than 6 videos" do
comedies = Category.create(name: "Comedies")
7.times{ Video.create(title: "Yes Man", description: "amused movie", category: comedies)}
expect(comedies.recent_videos.count).to eq(6)
end

it "returns the most recent 6 videos" do
comedies = Category.create(name: "Comedies")
6.times{ Video.create(title: "Yes Man", description: "amused movie", category: comedies)}
the_mask = Video.create(title: "The Mask", description: "funny movie", category: comedies, created_at: 1.day.ago)
expect(comedies.recent_videos).not_to include(the_mask)
end

it "returns an empty array if the category doesn't have any videos" do
comedies = Category.create(name: "Comedies")
expect(comedies.recent_videos).to eq([])
end
end
end

0 comments on commit 466f579

Please sign in to comment.