Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

68 lines (56 sloc) 1.548 kb
require 'test_helper'
require 'active_record'
require 'logger'
silence_warnings do
ActiveRecord::Migration.verbose = false
ActiveRecord::Base.logger = Logger.new(nil)
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
end
ActiveRecord::Base.connection.instance_eval do
create_table :users do |t|
t.string :sex
t.string :role
t.string :name
end
end
class User < ActiveRecord::Base
include Enumerize
enumerize :sex, :in => [:male, :female]
enumerize :role, :in => [:user, :admin], :default => :user
end
describe Enumerize::ActiveRecord do
it 'sets nil if invalid value is passed' do
user = User.new
user.sex = :invalid
user.sex.must_equal nil
end
it 'saves value' do
User.delete_all
user = User.new
user.sex = :female
user.save!
user.sex.must_equal 'female'
end
it 'loads value' do
User.delete_all
User.create!(:sex => :male)
store_translations(:en, :enumerize => {:sex => {:male => 'Male'}}) do
user = User.first
user.sex.must_equal 'male'
user.sex_text.must_equal 'Male'
end
end
it 'has default value' do
User.new.role.must_equal 'user'
User.new.attributes['role'].must_equal 'user'
end
it 'validates inclusion' do
user = User.new
user.role = 'wrong'
user.wont_be :valid?
end
it "uses persisted value for validation if it hasn't been set" do
user = User.create! :sex => :male
User.find(user).read_attribute_for_validation(:sex).must_equal 'male'
end
end
Jump to Line
Something went wrong with that request. Please try again.