Last active
December 10, 2015 04:28
-
-
Save deepak/4380666 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BooleanValidator < ActiveModel::EachValidator | |
def validate_each(record, attribute, value) | |
unless is_a_boolean? value | |
record.errors[attribute] << (options[:message] || "is not valid") | |
end | |
end | |
def is_a_boolean? value | |
!!value == value | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'active_model' | |
require_relative '../../app/validators/boolean_validator' | |
class Person | |
attr_accessor :foobar | |
include ActiveModel::Validations | |
validates :foobar, :boolean => true | |
end | |
describe BooleanValidator do | |
subject { Person.new } | |
it "is valid if attribute is true" do | |
subject.foobar = true | |
expect(subject.valid?).to eq true | |
end | |
it "is valid if attribute is false" do | |
subject.foobar = false | |
expect(subject.valid?).to eq true | |
end | |
it "is not valid if attribute is a string" do | |
subject.foobar = "hello" | |
expect(subject.valid?).to eq false | |
end | |
it "is not valid if attribute is a string having a number" do | |
subject.foobar = "1" | |
expect(subject.valid?).to eq false | |
subject.foobar = "a1" | |
expect(subject.valid?).to eq false | |
end | |
it "is not valid if attribute is an integer" do | |
subject.foobar = 1 | |
expect(subject.valid?).to eq false | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://github.com/thoughtbot/factory_girl/wiki/Testing-all-Factories-(with-RSpec) | |
require 'spec_helper' | |
describe 'validate FactoryGirl factories' do | |
FactoryGirl.factories.each do |factory| | |
context "with factory for :#{factory.name}" do | |
subject { FactoryGirl.build(factory.name) } | |
it "is valid" do | |
subject.should be_valid, subject.errors.full_messages | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_dependency 'boolean_validator' | |
class Loan < ActiveRecord::Base | |
validates :borrower_male, boolean: true, presence: true | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FactoryGirl.define do | |
factory :loan do | |
borrower_male false | |
end | |
end |
fixed. check the webapp project
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
factory_spec.rb fails even though boolean_validator_spec.rb is a success. why ?