Skip to content

Commit

Permalink
Return 400 error on unsuccessful operation
Browse files Browse the repository at this point in the history
  • Loading branch information
Bantik committed Dec 7, 2012
1 parent 503a338 commit 199aae7
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 2 deletions.
4 changes: 3 additions & 1 deletion Gemfile
Expand Up @@ -3,13 +3,15 @@ source "http://rubygems.org"
# Example:
gem "activesupport", ">= 2.3.5"
gem "activemodel"
gem "rails"

# Add dependencies to develop your gem here.
# Include everything needed to run rake, tests, features, etc.
group :development do
group :development, :test do
gem "shoulda", ">= 0"
gem "rdoc", "~> 3.12"
gem "rspec"
gem "rspec-rails"
gem "bundler"
gem "jeweler", "~> 1.8.4"
gem "simplecov"
Expand Down
70 changes: 70 additions & 0 deletions Gemfile.lock
@@ -1,23 +1,76 @@
GEM
remote: http://rubygems.org/
specs:
actionmailer (3.2.8)
actionpack (= 3.2.8)
mail (~> 2.4.4)
actionpack (3.2.8)
activemodel (= 3.2.8)
activesupport (= 3.2.8)
builder (~> 3.0.0)
erubis (~> 2.7.0)
journey (~> 1.0.4)
rack (~> 1.4.0)
rack-cache (~> 1.2)
rack-test (~> 0.6.1)
sprockets (~> 2.1.3)
activemodel (3.2.8)
activesupport (= 3.2.8)
builder (~> 3.0.0)
activerecord (3.2.8)
activemodel (= 3.2.8)
activesupport (= 3.2.8)
arel (~> 3.0.2)
tzinfo (~> 0.3.29)
activeresource (3.2.8)
activemodel (= 3.2.8)
activesupport (= 3.2.8)
activesupport (3.2.8)
i18n (~> 0.6)
multi_json (~> 1.0)
arel (3.0.2)
builder (3.0.3)
diff-lcs (1.1.3)
erubis (2.7.0)
git (1.2.5)
hike (1.2.1)
i18n (0.6.1)
jeweler (1.8.4)
bundler (~> 1.0)
git (>= 1.2.5)
rake
rdoc
journey (1.0.4)
json (1.7.5)
mail (2.4.4)
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.19)
multi_json (1.3.6)
polyglot (0.3.3)
rack (1.4.1)
rack-cache (1.2)
rack (>= 0.4)
rack-ssl (1.3.2)
rack
rack-test (0.6.2)
rack (>= 1.0)
rails (3.2.8)
actionmailer (= 3.2.8)
actionpack (= 3.2.8)
activerecord (= 3.2.8)
activeresource (= 3.2.8)
activesupport (= 3.2.8)
bundler (~> 1.0)
railties (= 3.2.8)
railties (3.2.8)
actionpack (= 3.2.8)
activesupport (= 3.2.8)
rack-ssl (~> 1.3.2)
rake (>= 0.8.7)
rdoc (~> 3.4)
thor (>= 0.14.6, < 2.0)
rake (0.9.2.2)
rdoc (3.12)
json (~> 1.4)
Expand All @@ -29,6 +82,11 @@ GEM
rspec-expectations (2.11.2)
diff-lcs (~> 1.1.3)
rspec-mocks (2.11.2)
rspec-rails (2.11.4)
actionpack (>= 3.0)
activesupport (>= 3.0)
railties (>= 3.0)
rspec (~> 2.11.0)
shoulda (3.1.1)
shoulda-context (~> 1.0)
shoulda-matchers (~> 1.2)
Expand All @@ -39,6 +97,16 @@ GEM
multi_json (~> 1.0)
simplecov-html (~> 0.5.3)
simplecov-html (0.5.3)
sprockets (2.1.3)
hike (~> 1.2)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
thor (0.16.0)
tilt (1.3.3)
treetop (1.4.12)
polyglot
polyglot (>= 0.3.1)
tzinfo (0.3.35)

PLATFORMS
ruby
Expand All @@ -48,7 +116,9 @@ DEPENDENCIES
activesupport (>= 2.3.5)
bundler
jeweler (~> 1.8.4)
rails
rdoc (~> 3.12)
rspec
rspec-rails
shoulda
simplecov
2 changes: 1 addition & 1 deletion lib/faceted/controller.rb
Expand Up @@ -9,7 +9,7 @@ def render_response(obj)
success: obj.success,
response: obj.to_hash,
errors: obj.errors
}
}, :status => obj.success ? 200 : 400
end

# For rendering a response with a multiple objects, e.g.
Expand Down
57 changes: 57 additions & 0 deletions spec/controller_spec.rb
@@ -0,0 +1,57 @@
require 'spec_helper'

class Birthplace # Mock AR model
attr_accessor :id, :city, :state
def initialize(params={}); params.each{|k,v| self.send("#{k}=",v) if self.respond_to?(k)}; end
def attributes; {:id => self.id, :city => self.city, :state => self.state}; end
def reload; self; end
end

module MyApi

class MyApi::Application < Rails::Application
end

class Birthplace
include Faceted::Presenter
presents :birthplace
field :city
field :state
end

class BirthplacesController < ActionController::Base
include Faceted::Controller
include Rails.application.routes.url_helpers
def show
@birthplace = MyApi::Birthplace.first
render_response @birthplace
end

end

end

describe MyApi::BirthplacesController, :type => :controller do

before do
MyApi::Birthplace.stub(:first) { MyApi::Birthplace.new }
MyApi::Application.routes.draw do
namespace :my_api do
resources :birthplaces
end
end
end

it 'renders with a 200 when the operation is successful' do
MyApi::Birthplace.any_instance.stub(:success) { true }
get :show, :id => 1
response.code.should == "200"
end

it 'renders with a 400 when the operation is unsuccessful' do
MyApi::Birthplace.any_instance.stub(:success) { false }
get :show, :id => 1
response.code.should == "400"
end

end
3 changes: 3 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -3,3 +3,6 @@
require 'active_support'
require 'bundler/setup'
require 'faceted'
require 'rails/all'
require 'rspec'
require 'rspec/rails'

0 comments on commit 199aae7

Please sign in to comment.