Skip to content

Instantly share code, notes, and snippets.

@tomafro
Forked from mattwynne/user_helper.rb
Created December 8, 2010 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomafro/733809 to your computer and use it in GitHub Desktop.
Save tomafro/733809 to your computer and use it in GitHub Desktop.
module UserHelper
attr_accessor :current_user
class Credentials < Struct.new(:email, :password)
def self.parse(arg)
return arg if arg.is_a?(Credentials)
new(arg)
end
def initialize(credentials)
self.email, self.password = credentials.split('/')
end
end
def sign_up_with(credentials)
credentials = Credentials.parse(credentials)
self.current_user = Factory(:user,
:email => credentials.email,
:password => credentials.password,
:password_confirmation => credentials.password)
# hack the credentials into the user object so we can retrieve them later
current_user.class_eval do
attr_accessor :credentials
def password
credentials.password
end
end
current_user.credentials = credentials
end
def sign_in
sign_in_with current_user.credentials
end
def sign_in_with(credentials)
credentials = Credentials.parse(credentials)
visit new_user_session_path
fill_in 'Email', :with => credentials.email
fill_in 'Password', :with => credentials.password
click_button 'Sign in'
end
end
World(UserHelper)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment