Sha256: 57d10e598f4c609b5952ec61ec5fe130d704ae7155abdccf67c678560011b73f

Contents?: true

Size: 1.99 KB

Versions: 1

Compression:

Stored size: 1.99 KB

Contents

require 'test_helper'

class UsersController
  include ContextValidations::Controller

  def create
    validations
  end

  def other_create
    validations(:create)
  end

  def update
    validations
  end

  def base_validations
    validates :first_name, :presence => true
  end

  def create_validations
    validates :password, :presence => true
  end
end

class ProtectedController
  include ContextValidations::Controller

  def create
    validations(:create)
  end

  protected

  def base_validations
    validates :first_name, :presence => true
  end

  def create_validations
    validates :password, :presence => true
  end
end

class PrivateController
  include ContextValidations::Controller

  def create
    validations(:create)
  end

  private

  def base_validations
    validates :first_name, :presence => true
  end

  def create_validations
    validates :password, :presence => true
  end
end


describe 'Controller' do
  context 'Public validations' do
    before do
      @controller = UsersController.new
    end

    if RUBY_VERSION >= '2'
      it 'combines base and create validations for create action, context is implied' do
        @controller.create.length.must_equal 2
      end
    end

    it 'combines base and create validations for other create action, context is forced' do
      @controller.other_create.length.must_equal 2
    end

    it 'uses base validations when context validations are not set for update action' do
      @controller.update.length.must_equal 1
    end
  end

  context 'Protected validations' do
    before do
      @controller = ProtectedController.new
    end

    it 'combines base and create validations for other create action, context is forced' do
      @controller.create.length.must_equal 2
    end
  end

  context 'Private validations' do
    before do
      @controller = PrivateController.new
    end

    it 'combines base and create validations for other create action, context is forced' do
      @controller.create.length.must_equal 2
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
context_validations-0.0.3 test/controller_test.rb