Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
149 additions
and
11 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 |
---|---|---|
|
@@ -23,5 +23,5 @@ group :test do | |
end | ||
|
||
gem 'thin' | ||
gem 'kaminari' | ||
gem 'will_paginate', '~> 3.0' | ||
gem 'rabl' |
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,10 +1,32 @@ | ||
class SongsController < ApplicationController | ||
# GET /songs | ||
def songs | ||
def index | ||
@songs = Song.paginate(:per_page => 50, :page => params[:page]) | ||
|
||
respond_to do |format| | ||
format.html | ||
end | ||
end | ||
|
||
# GET /songs/on_sale | ||
def on_sale | ||
@songs = Song.on_sale.paginate(:per_page => 50, :page => params[:page]) | ||
|
||
respond_to do |format| | ||
format.html | ||
end | ||
end | ||
|
||
# POST /songs | ||
def create | ||
@song = Song.new(title: params[:title], artist: params[:artist], album: params[:album], price_in_cents: params[:price]) | ||
|
||
respond_to do |format| | ||
if @song.save | ||
format.html { render 'songs/song' } | ||
else | ||
format.html { render json: { errors: @song.errors }, status: :unprocessable_entity } | ||
end | ||
end | ||
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
attributes :id, :title, :artist, :album, :price |
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,9 @@ | ||
object false | ||
|
||
child (@songs) do | ||
extends "songs/song" | ||
node(:pagination) do | ||
pagination_hash(@songs) | ||
end | ||
|
||
child @songs, object_root: false do | ||
extends "songs/base" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
object false | ||
|
||
node(:pagination) do | ||
pagination_hash(@songs) | ||
end | ||
|
||
child @songs, object_root: false do | ||
extends "songs/base" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
attributes :id, :title, :artist, :album, :price | ||
object @song => "" | ||
|
||
extends "songs/base" |
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 |
---|---|---|
@@ -0,0 +1,102 @@ | ||
require 'spec_helper' | ||
|
||
describe 'Songs API' do | ||
context "failed request" do | ||
subject { JSON.parse(last_response.body)['errors'] } | ||
|
||
context "title, artist, album and price are not provided" do | ||
it "responds with an error" do | ||
post '/songs' | ||
should have_key('title') | ||
should have_key('artist') | ||
should have_key('album') | ||
should have_key('price_in_cents') | ||
end | ||
end | ||
|
||
context "name is duplicated" do | ||
it "respond with an error" do | ||
song = FactoryGirl.create(:song) | ||
post "/songs", { title: song.title, album: song.album, artist: song.artist, price: 100 } | ||
should have_key('title') | ||
end | ||
end | ||
end | ||
|
||
context "successful request" do | ||
subject { JSON.parse(last_response.body) } | ||
|
||
it "returns a song record" do | ||
post "/songs", { title: "Awesome Song", artist: "Berna", album: "Top Hits", price: 100 } | ||
song = Song.last | ||
|
||
subject['id'].should == song.id | ||
subject['title'].should == song.title | ||
subject['artist'].should == song.artist | ||
subject['album'].should == song.album | ||
subject['price'].should == song.price | ||
end | ||
|
||
context "returns a list of songs in the server" do | ||
before do | ||
Song.destroy_all | ||
100.times.each do | ||
song = FactoryGirl.create :song | ||
end | ||
end | ||
|
||
let(:url) { "/songs" } | ||
|
||
it "should paginate the first 50 songs" do | ||
get url | ||
subject.should have_key("pagination") | ||
subject["pagination"]["total"].should == 100 | ||
subject["pagination"]["total_pages"].should == 2 | ||
subject["pagination"]["current_page"].should == 1 | ||
subject.should have_key("songs") | ||
subject["songs"].count.should eql(50) | ||
end | ||
|
||
it "should paginate the second 50 songs" do | ||
get url, { page: 2 } | ||
subject.should have_key("pagination") | ||
subject["pagination"]["total"].should == 100 | ||
subject["pagination"]["total_pages"].should == 2 | ||
subject["pagination"]["current_page"].should == 2 | ||
subject.should have_key("songs") | ||
subject["songs"].count.should eql(50) | ||
end | ||
end | ||
|
||
context "returns a list of songs on sale" do | ||
before do | ||
Song.destroy_all | ||
100.times.each do | ||
song = FactoryGirl.create :song, price_in_cents: 100 | ||
end | ||
end | ||
|
||
let(:url) { "/songs/on_sale" } | ||
|
||
it "should paginate the first 50 songs on sale" do | ||
get url | ||
subject.should have_key("pagination") | ||
subject["pagination"]["total"].should == 100 | ||
subject["pagination"]["total_pages"].should == 2 | ||
subject["pagination"]["current_page"].should == 1 | ||
subject.should have_key("songs") | ||
subject["songs"].count.should eql(50) | ||
end | ||
|
||
it "should paginate the second 50 songs on sale" do | ||
get url, { page: 2 } | ||
subject.should have_key("pagination") | ||
subject["pagination"]["total"].should == 100 | ||
subject["pagination"]["total_pages"].should == 2 | ||
subject["pagination"]["current_page"].should == 2 | ||
subject.should have_key("songs") | ||
subject["songs"].count.should eql(50) | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
describe Song do | ||
before do | ||
Bar.delete_all | ||
Song.delete_all | ||
end | ||
|
||
it "must have a title" do | ||
|