Skip to content

Instantly share code, notes, and snippets.

@PirosB3
Created August 3, 2011 14:14
Show Gist options
  • Save PirosB3/1122739 to your computer and use it in GitHub Desktop.
Save PirosB3/1122739 to your computer and use it in GitHub Desktop.
Functional test failing on post :create! TypeError: expected numeric or date
1) UsersController create action should destroy session if is present
Failure/Error: post :create
TypeError:
expected numeric or date
# ./app/models/user.rb:48:in `user_bigger_than_18'
# ./app/controllers/users_controller.rb:11:in `create'
# ./spec/controllers/users_controller_spec.rb:67:in `block (2 levels) in <top (required)>'
### USER MODEL
class User < ActiveRecord::Base
# new columns need to be added here to be writable through mass assignment
attr_accessible :name, :surname, :email, :password, :password_confirmation, :gender,:cooking_passion_description, :cookous_inspiration, :current_activity, :date_of_birth,:favourite_dishes, :people_i_enjoy, :personal_interests, :personal_statement,:visible_current_activity, :visible_date_of_birth, :visible_lives_in, :confirmation_code, :confirmed
attr_accessor :password
before_save :prepare_password
before_create :prepare_confirmation
validates_presence_of :name, :surname, :gender, :date_of_birth
validates_uniqueness_of :email, :allow_blank => true
validates_format_of :name, :surname, :with => /^[a-zA-Z]+$/, :allow_blank => true, :message => "should only contain letters"
validates_format_of :email, :with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
validates_presence_of :password, :on => :create
validates_confirmation_of :password
validates_length_of :password, :minimum => 4, :allow_blank => true
validates_inclusion_of :gender, :in => %w( male female unknown ), :on => :create, :message => "Please select valid gender"
validate :user_bigger_than_18
# login can be either username or email address
def self.authenticate(login, pass)
user = find_by_email(login)
return user if user && user.password_hash == user.encrypt_password(pass) && user.confirmed
end
def self.confirm_user_with_confirmation_code(confirmation)
u = User.find_by_confirmation_code(confirmation)
return false unless u
u.update_attributes :confirmed => true, :confirmation_code => nil
u.save
return u
end
def full_name
return false unless self.name && self.surname
return self.name + " " + self.surname
end
def encrypt_password(pass)
BCrypt::Engine.hash_secret(pass, password_salt)
end
private
def user_bigger_than_18
days_difference = (Date.today - self.date_of_birth).to_i
if (days_difference/365 < 18)
errors.add(:date_of_birth, "You must be over 18 to be able to use Cookous")
end
end
def prepare_confirmation
unless confirmed == true
self.confirmed = false
self.confirmation_code = ActiveSupport::SecureRandom.hex(16)
end
end
def prepare_password
unless password.blank?
self.password_salt = BCrypt::Engine.generate_salt
self.password_hash = encrypt_password(password)
end
end
end
it "create action should destroy session if is present" do
session['user_id'] = 128903
post :create
session['user_id'].should be_nil
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment