forked from gotealeaf/myflix
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement recent videos on the category model
- Loading branch information
1 parent
e0d6bb7
commit 466f579
Showing
3 changed files
with
44 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |