From 46abe07268cc9e71b653c9baeca279c44fece303 Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Wed, 27 Jul 2011 21:42:33 +0000 Subject: [PATCH] Add latest added tracks list on the home page --- app/controllers/home_controller.rb | 1 + app/models/track.rb | 4 ++++ app/views/home/index.html.haml | 3 +++ features/home.feature | 9 +++++++++ features/step_definitions/tracks_step.rb | 6 ++++++ spec/controllers/home_controller_spec.rb | 7 +++++++ spec/models/track_spec.rb | 8 ++++++++ spec/views/home/index.html.haml_spec.rb | 8 ++++++++ 8 files changed, 46 insertions(+) diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index d12ca98..872e743 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -1,5 +1,6 @@ class HomeController < ApplicationController def index @playlists = Playlist.all + @tracks = Track.latest end end diff --git a/app/models/track.rb b/app/models/track.rb index 389ee57..cce1e7f 100644 --- a/app/models/track.rb +++ b/app/models/track.rb @@ -15,4 +15,8 @@ class Track < ActiveRecord::Base end save! end + + def self.latest + Track.order('created_at DESC') + end end diff --git a/app/views/home/index.html.haml b/app/views/home/index.html.haml index 51d5736..1ba2424 100644 --- a/app/views/home/index.html.haml +++ b/app/views/home/index.html.haml @@ -2,3 +2,6 @@ %ul - @playlists.each do |p| %li= p.name +%ul + - @tracks.each do |t| + %li= t.name diff --git a/features/home.feature b/features/home.feature index 9f29745..4d49101 100644 --- a/features/home.feature +++ b/features/home.feature @@ -8,3 +8,12 @@ Feature: Home Given a playlist named "Electro" When I am on the home page Then I should see "Electro" within "ul>li" + + Scenario: Last tracks added + Given the following tracks: + | name | created_at | + | Mega song 1 | 2011-07-27 19:13:42 | + | Mega song 2 | 2011-07-27 19:58:57 | + When I am on the home page + Then I should see "Mega song 2" within "ul>li:first-child" + And I should see "Mega song 1" within "ul>li:first-child+li" diff --git a/features/step_definitions/tracks_step.rb b/features/step_definitions/tracks_step.rb index f8edd6c..ffd722f 100644 --- a/features/step_definitions/tracks_step.rb +++ b/features/step_definitions/tracks_step.rb @@ -4,6 +4,12 @@ Given /^a track named "([^"]*)"$/ do |name| @track.save_with_file(file, 'audio/mpeg') end +Given /^the following tracks:$/ do |table| + table.hashes.each do |h| + Factory.create(:track, h) + end +end + Then /^I should see an audio player$/ do page.should have_xpath '//audio' end diff --git a/spec/controllers/home_controller_spec.rb b/spec/controllers/home_controller_spec.rb index 12cf055..c5026da 100644 --- a/spec/controllers/home_controller_spec.rb +++ b/spec/controllers/home_controller_spec.rb @@ -7,5 +7,12 @@ describe HomeController do get :index assigns[:playlists].should == [playlist] end + + it 'assigns latest tracks as @tracks' do + track1 = Factory.create(:track, :created_at => '2011-07-27 19:13:42') + track2 = Factory.create(:track, :created_at => '2011-07-27 19:58:57') + get :index + assigns[:tracks].should == Track.latest + end end end diff --git a/spec/models/track_spec.rb b/spec/models/track_spec.rb index 82e2019..6e5732c 100644 --- a/spec/models/track_spec.rb +++ b/spec/models/track_spec.rb @@ -56,4 +56,12 @@ describe Track do `rm -f #{Rails.root}/data/tracks/*` end end + + describe '.latest' do + it 'returns latest tracks in descending creation date order' do + track1 = Factory.create(:track, :created_at => '2011-07-27 19:13:42') + track2 = Factory.create(:track, :created_at => '2011-07-27 19:58:57') + Track.latest.should == [track2, track1] + end + end end diff --git a/spec/views/home/index.html.haml_spec.rb b/spec/views/home/index.html.haml_spec.rb index 944fddd..b19f80f 100644 --- a/spec/views/home/index.html.haml_spec.rb +++ b/spec/views/home/index.html.haml_spec.rb @@ -5,6 +5,9 @@ describe 'home/index.html.haml' do assign :playlists, [ mock_model('Playlist', :name => 'Electro') ] + assign :tracks, [ + mock_model('Track', :name => 'Mega song') + ] end it 'displays a list of playlists' do @@ -16,4 +19,9 @@ describe 'home/index.html.haml' do render rendered.should have_selector('a', :text => 'Add a track') end + + it 'displays a list of tracks' do + render + rendered.should have_selector('ul>li', :text => 'Mega song') + end end