Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #144 from stravid/chore/store-current-run
Pull Request: chore/store-current-run
- Loading branch information
Showing
11 changed files
with
155 additions
and
7 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
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,15 @@ | ||
class Api::RunsController < ApplicationController | ||
def create | ||
return head :bad_request if params[:run].nil? || current_user.nil? | ||
|
||
return head :bad_request if Track.find_by_id(params[:run][:track_id]).nil? | ||
|
||
run = current_user.runs.new params[:run] | ||
|
||
if run.save | ||
render :json => run, :status => :created | ||
else | ||
head :error | ||
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
class Run < ActiveRecord::Base | ||
belongs_to :user | ||
belongs_to :track | ||
|
||
validates :time, :numericality => { :only_integer => true, :greater_than => 0 } | ||
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,3 @@ | ||
class RunSerializer < ActiveModel::Serializer | ||
attributes :id, :time, :track_id, :user_id | ||
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,62 @@ | ||
require 'spec_helper' | ||
|
||
describe Api::RunsController do | ||
|
||
describe '#create' do | ||
|
||
let(:track) { FactoryGirl.create :track } | ||
let(:user) { FactoryGirl.create :user } | ||
|
||
it 'should return a bad request error code when params hash does not contain a `run` key' do | ||
post :create, :random_key => 'woot' | ||
|
||
response.should be_bad_request | ||
end | ||
|
||
it 'should return a bad request error code when there is no current user' do | ||
post :create, :run => { :time => 14442, :track_id => track.id } | ||
|
||
response.should be_bad_request | ||
end | ||
|
||
it 'should create a run with passed parameters' do | ||
sign_in user | ||
time = 14442 | ||
post :create, :run => { :time => time, :track_id => track.id } | ||
|
||
response.code.should eq '201' | ||
Run.last.time.should eq time | ||
Run.last.track_id.should eq track.id | ||
Run.last.user_id.should eq user.id | ||
end | ||
|
||
it 'should serialize created run and return it as JSON' do | ||
sign_in user | ||
post :create, :run => { :time => 1337, :track_id => track.id } | ||
|
||
created_run = Run.last | ||
|
||
serializer = RunSerializer.new created_run, :root => 'run' | ||
serialized_run = serializer.as_json | ||
|
||
response.body.should == serialized_run.to_json | ||
end | ||
|
||
it 'should return a bad request error code if the track_id is invalid' do | ||
sign_in user | ||
|
||
post :create, :run => { :time => 1337, :track_id => Track.count + 1 } | ||
|
||
response.should be_bad_request | ||
end | ||
|
||
it 'should return a server error when creating a run fails' do | ||
sign_in user | ||
|
||
post :create, :run => { :time => -42, :track_id => track.id } | ||
|
||
response.should be_error | ||
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 |
---|---|---|
|
@@ -11,5 +11,4 @@ | |
sequence :invalid_raphael_path do |n| | ||
"string#{n}" | ||
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 |
---|---|---|
@@ -1,6 +1,5 @@ | ||
# Read about factories at https://github.com/thoughtbot/factory_girl | ||
|
||
FactoryGirl.define do | ||
factory :run do | ||
time { Random.rand 10000 } | ||
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
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