How do I improve this RSpec code in rspec way? - ruby

I'm new to Ruby and RSpec. I come from Java background that's why my test really looks like junit code. I'm trying to learn more about RSpec but I don't quite understand subject, let, !let. So, if anybody can guide me to clean up this code, I'd be really appreciated.
I have sinatra, RSpec which it's doing Sign-in with Twitter.
get '/login/twitter' do
begin
request_token = TwitterService.new.authentication_request_token
session[:request_token_twitter] = request_token
redirect request_token.authorize_url
rescue Exception => e
logger.error(e.message)
redirect '/'
end
end
get '/login/twitter/success' do
request_token = session[:request_token_twitter]
twitter_service = TwitterService.new
access_token = twitter_service.authorize(request_token, params[:oauth_verifier])
begin
twitter_user_info = twitter_service.verify_credentials
twitter_id = twitter_user_info["id"]
response.set_cookie("auth_token", :value => twitter_id, :path => '/')
response.set_cookie(#social_flag, :value => "t", :path => '/')
expected_user = #user_manager.find_by_id(twitter_id.to_s)
if expected_user.is_null?
twitter_user = User.new(twitter_id, access_token.token, access_token.secret, "t")
twitter_user.save
logger.info("Saving ...")
logger.info("Twitter ID #{twitter_id}")
redirect '/signup'
else
expected_user.token = access_token.token
expected_user.secret = access_token.secret
expected_user.update
logger.info("Updating token and secret ...")
logger.info("Twitter ID #{twitter_id}")
end
rescue Exception => e
logger.error(e.message)
logger.error("There's something wrong with Twitter and user cannot log in")
redirect '/'
end
redirect '/t'
end
And here's my RSpec. I know it's really ugly.
describe "Twitter route" do
include TwitterOAuth
def app
Sinatra::Application
end
context "/login/twitter" do
it "should redirect to twitter authorized url" do
request_token = OpenStruct.new
request_token.authorize_url = "http://api.twitter.com/oauth/authenticate?oauth_token"
TwitterService.any_instance.stub(:authentication_request_token).and_return(request_token)
get '/login/twitter'
last_response.header["Location"].should include "http://api.twitter.com/oauth/authenticate?oauth_token"
last_response.status.should eql 302
session[:request_token_twitter].authorize_url.should == "http://api.twitter.com/oauth/authenticate?oauth_token"
end
it "should redirect back to home page if error occurs" do
TwitterService.any_instance.stub(:authentication_request_token).and_raise("Unauthorized")
get '/login/twitter'
last_response.header["Location"].should include "http://example.org/"
last_response.status.should eql 302
session[:request_token_twitter].should eql nil
end
it "should save a user after a success callback from twitter" do
user_manager = UserManager.new
access_token = OpenStruct.new
access_token.token = "token"
access_token.secret = "secret"
TwitterService.any_instance.stub(:authorize).with(anything(), anything()).and_return(access_token)
TwitterService.any_instance.stub(:verify_credentials).and_return({"id" => "id1"})
get '/login/twitter/success'
last_response.header["Location"].should include "/signup"
rack_mock_session.cookie_jar["auth_token"].should eql "id1"
rack_mock_session.cookie_jar["s_flag"].should eql "t"
last_response.status.should eql 302
user_manager = UserManager.new
expected_user = user_manager.find_by_id("id1")
expected_user.id.should eql "id1"
expected_user.token.should eql "token"
expected_user.secret.should eql "secret"
end
it "should update user token and secret if the user already exists" do
User.new("id1", "token", "secret", "t").save
access_token = OpenStruct.new
access_token.token = "token1"
access_token.secret = "secret1"
TwitterService.any_instance.stub(:authorize).with(anything(), anything()).and_return(access_token)
TwitterService.any_instance.stub(:verify_credentials).and_return({"id" => "id1"})
get '/login/twitter/success'
last_response.header["Location"].should include "/t"
rack_mock_session.cookie_jar["auth_token"].should eql "id1"
rack_mock_session.cookie_jar["s_flag"].should eql "t"
last_response.status.should eql 302
user_manager = UserManager.new
expected_user = user_manager.find_by_id("id1")
expected_user.id.should eql "id1"
expected_user.token.should eql "token1"
expected_user.secret.should eql "secret1"
end
it "should redirect back to the home page" do
access_token = OpenStruct.new
access_token.token = "token1"
access_token.secret = "secret1"
TwitterService.any_instance.stub(:authorize).with(anything(), anything()).and_return(access_token)
TwitterService.any_instance.stub(:verify_credentials).and_raise
get '/login/twitter/success'
last_response.header["Location"].should include "http://example.org/"
end
end
end
Any improvement I'd be grateful not just the code. May be if I miss something obvious.
Thanks a lot guys.

Okay, a lot happening here!
First off, you should try to stick to one test per example. Your examples are currently testing a whole bunch of behavior, which means that your tests are rather all-or-nothing, and may make it unclear what specifically breaks if you've broken something.
First off, I'm going to add a new matcher. You'd usually put this in somewhere like spec/support/matchers.rb or something. It's just going to extend rspec so that we can test that a response was a redirect, and that the redirect goes to a given location:
RSpec::Matchers.define :redirect_to do |expected|
match do |actual|
actual.should be_redirect
actual.location.should include expected
end
end
Now, onto the code!
The unannotated source is here: https://gist.github.com/cheald/5908093 - that will probably be less annoying to read :)
let defines a method that will run exactly once per example, no matter how many times it's invoked. This lets us have a "variable" that is defined at example-time, which lets us override it in nested examples. Here, I have access_token defined up top, but we'll let another access_token in a deeper example. This suite doesn't really show this off too well, but this lets you do nice things where something from one let is referenced in another. Imagine, if you will, that we have
let(:user) { user_manager.find(access_token.id) }
This will use the deepest-nested user_manager and deepest-nested access_token without having to redeclare user in each nested scope. Handy!
let blocks aren't invoked until they're used (as opposed to let! blocks, which are always invoked when declared)
describe "Twitter route" do
include TwitterOAuth
let(:app) { Sinatra::Application }
let(:request_token) { double("request_token", authorize_url: "http://api.twitter.com/oauth/authenticate?oauth_token") }
let(:access_token) { double("token", token: "token", secret: "secret") }
let(:user_manager) { UserManager.new }
You'll notice that I've broken your tests up into nested contexts, to group similar behavior. That is, all the tests that should pass with an authorized token get nested under the authorized token context, and our before block sets up the context so that all examples in this context get a valid token.
We also go ahead and do the get in the before block, so we can just test the results directly.
context "/login/twitter" do
context "with an authorized token" do
before do
TwitterService.any_instance.stub(:authentication_request_token).and_return(request_token)
TwitterService.any_instance.stub(:authorize).with(anything(), anything()).and_return(access_token)
TwitterService.any_instance.stub(:verify_credentials).and_return({"id" => "id1"})
get '/login/twitter'
end
You see here that I'm using our new matcher. It lets us check for a redirect to a given URL in one test.
it "should redirect to twitter authorized url" do
last_response.should redirect_to "http://api.twitter.com/oauth/authenticate?oauth_token"
end
it "should set a the request token in the session" do
session[:request_token_twitter].authorize_url.should == "http://api.twitter.com/oauth/authenticate?oauth_token"
end
context "after a success callback" do
let(:user) { user_manager.find_by_id("id1") }
context "when there is not an existing user" do
before do
get '/login/twitter/success'
end
it "should redirect to /signup" do
last_response.should redirect_to "/signup"
end
it "should set an auth_token cookie" do
rack_mock_session.cookie_jar["auth_token"].should == "id1"
end
it "should set an s_flag cookie" do
rack_mock_session.cookie_jar["s_flag"].should == "t"
end
Here you'll see subject. It just defines what the variable subject returns, and makes its blocks operate on it. In this case, the subject is the User record. Since subject is the user record, I can use the more concise form to check its attributes.
context "the authenticated user" do
subject { user }
its(:id) { should == "id1" }
its(:token) { should == "token" }
its(:secret) { should == "secret" }
end
end
You'll see here that I provide a new definition for access_token. When these examples run, the before block way up at the top (that sets up the "authorized token") will use this access_token rather than the one defined way up there. This lets us override the variables used to set up the context with variables specific to this particular context.
context "when there is an existing user" do
let(:access_token) { double("token", token: "newtoken", secret: "newsecret") }
before do
User.new("id1", "oldtoken", "oldsecret", "t").save
get '/login/twitter/success'
end
it "should set an auth_token cookie" do
rack_mock_session.cookie_jar["auth_token"].should == "id1"
end
it "should set an s_flag cookie" do
rack_mock_session.cookie_jar["s_flag"].should == "t"
end
it "should redirect to /t" do
last_response.should redirect_to "/t"
end
context "the authenticated user" do
subject { user }
its(:id) { should == "id1" }
its(:token) { should == "newtoken" }
its(:secret) { should == "newsecret" }
end
end
end
end
context "with an invalid token" do
before do
TwitterService.any_instance.stub(:authentication_request_token).and_raise("Unauthorized")
get '/login/twitter'
end
it "should redirect back to home page if error occurs" do
last_response.should redirect_to "http://example.org/"
end
it "should not set a session value" do
session[:request_token_twitter].should be_nil
end
end
end
end

Related

How to write rspec for logic and write mock STDIN

I am a very new coder and trying to write rspec for a class that test the conditional statement/logic. I started sudo coding for it but I was told to make mock STDIN which I don't know how to. Can someone please write the rspec for the class or give me a few idea how to create a mock STDIN. I need help writing rspec for the conditional statement/logic, if some can please just write the test for one of the context then I can do rest based on that.
require 'rails_helper'
module BAB::ACA
RSpec.describe partfinder do
describe '#find_part_id' do
let(:face) { create(:face) }
subject { described_class.find_part_id(face) }
context 'When bab con already exists' do
context 'when there are more than one part ids' do
#create part ids
context 'when user input matches an existing id' do
#mock STDIN that matches an existing, subject should equal that id
end
context 'when user input does not match an existing id' do
# mock STDIN that does match existing id, should return failure message
end
end
context 'when there is only one bab part id' do
# subject should equal the one that already exists
end
end
context 'when av con does not yet exist' do
# mock STDIN and make sure subject equals what you mocked
end
end
end
module BAB::ACA
class partfinder
def self.find_part_id(face)
av_con = BAB::Child:Fail.find_by(
face: face
reg: BAB:Child.find_reg
)
if av_con
look_id(face, av_con)
end
else
puts "What is #{face.name} BAB part id? must be 6"
STDIN.gets.chomp
end
end
def self.look_id(face, av_con)
if av_con.part_ids.length > 1
ask_for_id(face, av_con)
else
av.con.part_ids.first
end
end
def self.ask_for_id(face, av_con)
puts "What is #{face.name} BAB part id? "
bab_part_id = STDIN.gets.chomp
unless av.con.part_ids.include?(bab_part_id)
fail 'Entered id doesn't match'
end
bab_part_id
end
end
end
You can use method stubs.
In this case you want to stub STDIN.gets.chomp, so you'd do something like this:
describe '#find_part_id' do
before do
allow(STDIN.gets).to receive(:chomp).and_return(stdin_input)
end
let(:stdin_input) { 'user input from stdin' }
let(:face) { create(:face) }
subject { described_class.find_part_id(face) }
context 'When bab con already exists' do
context 'when there are more than one part ids' do
it 'some test' do
# your test here
end
end
# more contexts...
context 'a context that needs a different stdin_input' do
let(:stdin_input) { 'some different user input from stdin' }
it 'another test' do
# your test here
end
end
end
end
Where stdin_input is the string you want the user to enter for your tests.

RSpec how to stub out yield and have it not hit ensure

I have an around action_action called set_current_user
def set_current_user
CurrentUser.set(current_user) do
yield
end
end
In the CurrentUser singleton
def set(user)
self.user = user
yield
ensure
self.user = nil
end
I cannot figure out how to stub out the yield and the not have the ensure part of the method called
Ideally I would like to do something like
it 'sets the user' do
subject.set(user)
expect(subject.user).to eql user
end
Two errors I am getting
No block is given
When I do pass a block self.user = nil gets called
Thanks in advance
A few things to point out that might help:
ensure is reserved for block of codes that you want to run no matter what happens, hence the reason why your self.user will always be nil. I think what you want is to assign user to nil if there's an exception. In this case, you should be using rescue instead.
def set(user)
self.user = user
yield
rescue => e
self.user = nil
end
As for the unit test, what you want is to be testing only the .set method in the CurrentUser class. Assuming you have everything hooked up correctly in your around filter, here's a sample that might work for you:
describe CurrentUser do
describe '.set' do
let(:current_user) { create(:user) }
subject do
CurrentUser.set(current_user) {}
end
it 'sets the user' do
subject
expect(CurrentUser.user).to eq(current_user)
end
end
end
Hope this helps!
I am not sure what you intend to accomplish with this as it appears you just want to make sure that user is set in the block and unset afterwards. If this is the case then the following should work fine
class CurrentUser
attr_accessor :user
def set(user)
self.user = user
yield
ensure
self.user = nil
end
end
describe '.set' do
subject { CurrentUser.new }
let(:user) { OpenStruct.new(id: 1) }
it 'sets user for the block only' do
subject.set(user) do
expect(subject.user).to eq(user)
end
expect(subject.user).to be_nil
end
end
This will check that inside the block (where yield is called) that subject.user is equal to user and that afterwards subject.user is nil.
Output:
.set
sets user for the block only
Finished in 0.03504 seconds (files took 0.14009 seconds to load)
1 example, 0 failures
I failed to mention I need to clear out the user after every request.
This is what I came up with. Its kinda crazy to put the expectation inside of the lambda but does ensure the user is set prior to the request being processed and clears it after
describe '.set' do
subject { described_class }
let(:user) { OpenStruct.new(id: 1) }
let(:user_expectation) { lambda{ expect(subject.user).to eql user } }
it 'sets the user prior to the block being processed' do
subject.set(user) { user_expectation.call }
end
context 'after the block has been processed' do
# This makes sure the user is always cleared after a request
# even if there is an error and sidekiq will never have access to it.
before do
subject.set(user) { lambda{} }
end
it 'clears out the user' do
expect(subject.user).to eql nil
end
end
end

How to display payment gateway response messages in view - Rails 4, Active Merchant

I'm using Active Merchant with Stripe as the payment gateway. Everything works fine except that i don't know how to go about getting the gateway response error messages from Stripe (when a card is declined, invalid etc) to display on the checkout page to the user. I can get a StandardError to be raised that redirects to an error page with the response message but that's it.
ORDER MODEL
class Order < ActiveRecord::Base
has_many :order_products
has_many :products, through: :order_products
attr_accessor :card_number, :security_code, :card_expires_on
validate :validate_card, :on => :create
def validate_card
unless credit_card.valid?
credit_card.errors.full_messages.each do |message|
errors[:base] << message
end
end
end
def purchase(basket)
response = GATEWAY.purchase(Product.total_basket_price(basket)*100, credit_card, purchase_options)
unless response.success?
raise StandardError, response.message
end
end
def credit_card
#credit_card ||= ActiveMerchant::Billing::CreditCard.new(
:number => card_number,
:first_name => first_name,
:last_name => last_name,
:verification_value => security_code,
:month => card_expires_on.month,
:year => card_expires_on.year
)
end
def purchase_options
{
:billing_address => {
:address1 => address_1,
:address2 => address_2,
:city => city,
:country => country_code,
:zip => postal_code
}
}
end
end
ORDERS CONTROLLER
class OrdersController < ApplicationController
def create
#order = Order.new(order_params)
# #product = basket.find(params[:product_id])
basket.each do |item_id|
#order.order_products.build(product: Product.find(item_id))
end
if #order.save
if #order.purchase(basket)
render "show"
else
render "failure"
end
else
render "new"
end
end
Can anyone lend a hand, please??
Many Thanks
Easy peasy!
This is a simple matter of control flow. In Ruby, as in most languages, exceptions interrupt the normal program flow. As your code is written now, #purchase is raising an exception when it fails.
That's fine and a perfectly valid design decision. But the code interacting with #purchase is this:
if #order.purchase(basket)
render "show"
else
render "failure"
end
That code has no exception handling, so any exception will be caught by Rails, program flow will halt, and you'll get either a detailed error page (in development mode) or a generic 500 error page (in production mode).
Since you profess to be new to Ruby and Rails, a little code substitution might make this clearer:
# If #purchase is successful, it evaluates to true.
if true
render "show" # 'show' view is rendered as expected. Flow stops.
else
render "failure"
end
# If #purchase fails, it raises an exception.
if raise StandardError, response.message
# ^^^ Exception is raised, flow stops here.
render "show" # This code is never reached.
else # This code is never reached.
render "failure" # This code is never reached.
end
As I implied in the beginning, though, it's an easy fix once you know what the issue is. You can simply handle the exception with rescue. Where you currently have an if/else block, you can swap in an if block and a rescue block:
if #order.purchase(basket)
render 'show'
end
rescue => e
render 'failure'
There's room for improvement here depending on your needs—since you're raising and rescuing StandardError, for example, your can't easily distinguish between a network failure and a declined card—but it'll get you moving again.
After a lot of fiddling and help, the working solution was to search for an error key within the response params hash and if an error was present add the message to the object errors. Not particularly elegant but it now does what i want.
ORDER MODEL
def purchase(basket)
response = GATEWAY.purchase(Product.total_basket_price(basket)*100, credit_card, purchase_options)
if response.params.key?('error')
self.errors.add :base, response.message
false
else
true
end
end
ORDERS CONTROLLER
Also switched the order of the if statements in the controller so that def purchase(basket) runs first before the order is saved, allowing the error message(s) from the response to be caught and displayed.
if #order.purchase(basket)
if #order.save
render "show"
else
render "new"
end
else
render "new"
end
VIEW
<%= if #order.errors.any?
#order.errors[:base].to_sentence
end%>

How do I use omniauth in rspec for sinatra?

Shortened version:
Using the omniauth gem for sinatra, I can't get rspec log in to work and keep my session for subsequent requests.
Based on suggestions from http://benprew.posterous.com/testing-sessions-with-sinatra, and turning off sessions, I've isolated the problem to this:
app.send(:set, :sessions, false) # From http://benprew.posterous.com/testing-sessions-with-sinatra
get '/auth/google_oauth2/callback', nil, {"omniauth.auth" => OmniAuth.config.mock_auth[:google_oauth2] }
# last_request.session => {"uid"=>"222222222222222222222", :flash=>{:success=>"Welcome"}}
# last_response.body => ""
follow_redirect!
# last_request.session => {:flash=>{}}
# last_response.body => Html for the homepage, which is what I want
How do I get rspec to follow the redirect and retain the session variables? Is this possible in Sinatra?
From http://benprew.posterous.com/testing-sessions-with-sinatra, it seems like I'd have to send the session variables on each get/post request that I require login for, but this wouldn't work in the case of redirects.
The details:
I'm trying to use the omniauth gem in sinatra with the following setup:
spec_helper.rb
ENV['RACK_ENV'] = 'test'
# Include web.rb file
require_relative '../web'
# Include factories.rb file
require_relative '../test/factories.rb'
require 'rspec'
require 'rack/test'
require 'factory_girl'
require 'ruby-debug'
# Include Rack::Test in all rspec tests
RSpec.configure do |conf|
conf.include Rack::Test::Methods
conf.mock_with :rspec
end
web_spec.rb
describe "Authentication:" do
before do
OmniAuth.config.test_mode = true
OmniAuth.config.add_mock(:google_oauth2, {
:uid => '222222222222222222222',
:info => {
:email => "someone#example.com",
:name => 'Someone'
}
})
end
describe "Logging in as a new user" do
it "should work" do
get '/auth/google_oauth2/'
last_response.body.should include("Welcome")
end
end
end
When trying to authenticate, I get a <h1>Not Found</h1> response. What am I missing?
On the Integration testing page of the omniauth docs, it mentions adding two environment variables:
before do
request.env["devise.mapping"] = Devise.mappings[:user]
request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:twitter]
end
But seems to be for rails only, as I added
request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:google_oauth2]
to my before block in my spec and I get this error:
Failure/Error: request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:google_oauth2]
ArgumentError:
wrong number of arguments (0 for 1)
Edit:
Calling get with
get '/auth/google_oauth2/', nil, {"omniauth.auth" => OmniAuth.config.mock_auth[:google_oauth2]}
seems to give me last_request.env["omniauth.auth"] equal to
{"provider"=>"google_oauth2", "uid"=>"222222222222222222222", "info"=>{"email"=>"someone#example.com", "name"=>"Someone"}}
which seems right, but last_response.body still returns
<h1>Not Found</h1>
A partial answer...
The callback url works better, with the added request environment variables:
get '/auth/google_oauth2/callback', nil, {"omniauth.auth" => OmniAuth.config.mock_auth[:google_oauth2]}
follow_redirect!
last_response.body.should include("Welcome")
However, this doesn't work with sessions after the redirect, which is required for my app to know someone is logged in. Updated the question to reflect this.
Using this gist (originating from https://stackoverflow.com/a/3892401/111884) to store session data, I got my tests to store the session, allowing me to pass the session to further requests.
There might be an easier way though.
Setup code:
# Omniauth settings
OmniAuth.config.test_mode = true
OmniAuth.config.add_mock(:google_oauth2, {
:uid => '222222222222222222222',
:info => {
:email => "someone#example.com",
:name => 'Someone'
}
})
# Based on https://gist.github.com/375973 (from https://stackoverflow.com/a/3892401/111884)
class SessionData
def initialize(cookies)
#cookies = cookies
#data = cookies['rack.session']
if #data
#data = #data.unpack("m*").first
#data = Marshal.load(#data)
else
#data = {}
end
end
def [](key)
#data[key]
end
def []=(key, value)
#data[key] = value
session_data = Marshal.dump(#data)
session_data = [session_data].pack("m*")
#cookies.merge("rack.session=#{Rack::Utils.escape(session_data)}", URI.parse("//example.org//"))
raise "session variable not set" unless #cookies['rack.session'] == session_data
end
end
def login!(session)
get '/auth/google_oauth2/callback', nil, { "omniauth.auth" => OmniAuth.config.mock_auth[:google_oauth2] }
session['uid'] = last_request.session['uid']
# Logged in user should have the same uid as login credentials
session['uid'].should == OmniAuth.config.mock_auth[:google_oauth2]['uid']
end
# Based on Rack::Test::Session::follow_redirect!
def follow_redirect_with_session_login!(session)
unless last_response.redirect?
raise Error.new("Last response was not a redirect. Cannot follow_redirect!")
end
get(last_response["Location"], {}, { "HTTP_REFERER" => last_request.url, "rack.session" => {"uid" => session['uid']} })
end
def get_with_session_login(path)
get path, nil, {"rack.session" => {"uid" => session['uid']}}
end
Sample rspec code:
describe "Authentication:" do
def session
SessionData.new(rack_test_session.instance_variable_get(:#rack_mock_session).cookie_jar)
end
describe "Logging in as a new user" do
it "should create a new account with the user's name" do
login!(session)
last_request.session[:flash][:success].should include("Welcome")
get_with_session_login "/"
follow_redirect_with_session_login!(session)
last_response.body.should include("Someone")
end
end
end

Accessing Sinatra scope from another class

I'm running a Sinatra application with a few extra classes pulled in for creating a User and a few others on the fly (no DB, it feeds in from a web service). I'm trying send out a flash notice (using https://github.com/nakajima/rack-flash) from within my User model but can't figure out how to get access to the flash method/variable because I'm out of scope.
Something like:
class User
def something
if true
flash[:notice] = 'Good job'
else
# nope
end
end
end
Which gets required into the Sinatra app by a simple require 'models/user'
This is an XY Problem[1]. Sinatra is responsible for sending out flash messages, not your User objects, so the code for setting the flash should be in your Sinatra app, not in your User class.
[1] http://www.perlmonks.org/index.pl?node_id=542341
You should not ask your User (model) to talk to the UI (view). That's bad/not MVC-clean. That's what a controller is for.
You can use either return values, exceptions, or throw/catch (which is not exception handling) to pass information from your model to your controller. For example, using return values:
post "/create_user" do
flash[:notice] = case User.something
when User then "User Created!"
when :nono then "That's not allowed"
when :later then "User queued to be created later."
end
end
class User
def self.something
if authorized
if can_create_now
new(...)
else
queue_create(...)
:later
end
else
:nono
end
end
end
Since I mentioned them above, following are examples using throw/catch and begin/rescue (exceptions). As the advisability of using either of these constructs is questionable, let us take a moment of silence to ponder if this is a good idea.
Here is an example using throw/catch:
post "/create_user" do
result = catch(:msg){ User.something }
flash[:notice] = case
when :nono then "That's not allowed"
when :later then "User queued to be created later."
else "User Created!"
end
end
class User
def self.something
throw :msg, :nono unless authorized
if can_create_now
new(...)
else
queue_create(...)
throw :msg, :later
end
end
end
Finally, here's an example using exceptions, though I'm not convinced that this will be appropriate for all (non-disastrous) cases where you want to flash unique messages to the user:
post "/create_user" do
flash[:notice] = "User Created!" # Assume all good
begin
User.something
rescue User::Trouble=>e
flash[:notice] = case e
when Unauthorized then "That's not allowed"
when DelayedCreate then "User queued to be created later."
else "Uh...Something bad happened."
end
end
end
class User
class Trouble < RuntimeError; end
class Unauthorized < Trouble; end
class DelayedCreate < Trouble; end
def self.something
raise Unauthorized unless authorized
if can_create_now
new(...)
else
queue_create(...)
raise DelayedCreate
end
end
end
Exceptions let you pass an additional data along (e.g. raise Unauthorized.new "No such account", or any custom properties you want to add to your class), and so may be more useful (when appropriate). Just remember to pass semantic results from your model to your controller, and let it translate them into user-facing messages.

Resources