Skip to content

Instantly share code, notes, and snippets.

@wojtekmach
Created April 3, 2012 22:49
Show Gist options
  • Select an option

  • Save wojtekmach/2296081 to your computer and use it in GitHub Desktop.

Select an option

Save wojtekmach/2296081 to your computer and use it in GitHub Desktop.
ValidAttribute macros
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
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
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