Created
April 3, 2012 22:49
-
-
Save wojtekmach/2296081 to your computer and use it in GitHub Desktop.
ValidAttribute macros
This file contains hidden or 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_attr' | |
| class Address | |
| include ActiveAttr::Model | |
| attribute :city | |
| attribute :country | |
| validates :city, presence: true | |
| validates :city, inclusion: { in: ["Vatican"] }, if: lambda { |address| address.country == 'Vatican' } | |
| end |
This file contains hidden or 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 'spec_helper' | |
| describe Address do | |
| should_have_valid :city, when: ['NYC'] | |
| should_not_have_valid :city, when: [nil, ''] | |
| context 'Vatican' do | |
| subject { Address.new country: 'Vatican' } | |
| should_have_valid :city, when: ['Vatican'] | |
| should_not_have_valid :city, when: ['NYC'] | |
| end | |
| end |
This file contains hidden or 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 './lib/address' | |
| require 'valid_attribute' | |
| module HaveValidMacros | |
| def should_have_valid(attribute, options) | |
| it { should have_valid(attribute).when(*options[:when]) } | |
| end | |
| def should_not_have_valid(attribute, options) | |
| it { should_not have_valid(attribute).when(*options[:when]) } | |
| end | |
| end | |
| RSpec.configure do |config| | |
| config.extend HaveValidMacros | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment