Sha256: 46eb38def3a51f0926fb396a5ad64da6d32b9df5f64d9a7bf15b620b2e4ebf82

Contents?: true

Size: 1.86 KB

Versions: 2

Compression:

Stored size: 1.86 KB

Contents

require 'helper'
require 'mocha'

setup_active_record

class User < ActiveRecord::Base
  include HasConstant
end

class ActiveModelTest < Test::Unit::TestCase
  context 'Instance' do
    should 'save values as integers' do
      User.has_constant :salutations, %w(Mr Mrs)
      u = User.new(:salutation => 'Mr')
      u.save!
      assert_equal 'Mr', u.salutation
      assert_equal 0, u.attributes['salutation']
    end

    should 'default values to translated values list' do
      I18n.stubs(:t).returns(['a', 'b'])
      User.has_constant :salutations
      assert_equal ['a', 'b'], User.salutations
    end

    context 'accessor' do
      setup do
        User.has_constant :titles, ['Mr', 'Mrs'], :accessor => :salutation
        @user = User.new(:salutation => 'Mr')
      end

      should 'work store the values in the correct field ' do
        @user.save!
        assert_equal 0, @user.attributes['salutation']
      end

      should 'use the accessor to get values' do
        assert_equal 'Mr', @user.salutation
      end

      should 'use constant name for values list' do
        assert_equal %w(Mr Mrs), User.titles
      end
    end
  end

  context 'scopes' do
    setup do
      User.has_constant :salutations, %w(Mr Mrs)
      @man = User.create!(:salutation => 'Mr')
      @woman = User.create!(:salutation => 'Mrs')
    end

    should 'provide by_constant scope' do
      assert_equal 1, User.by_constant('salutation', 'Mr').count
      assert_equal @man, User.by_constant('salutation', 'Mr').first
    end

    should 'provide singular_is scope' do
      assert_equal 1, User.salutation_is('Mr').count
      assert_equal @man, User.salutation_is('Mr').first
    end

    should 'provide singular_is_not scope' do
      assert_equal 1, User.salutation_is_not('Mr').count
      assert_equal @woman, User.salutation_is_not('Mr').first
    end
  end
end if defined?(ActiveRecord)

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
has_constant-0.5.1 test/unit/orm/active_model_test.rb
has_constant-0.5.0 test/unit/orm/active_model_test.rb