Created
February 29, 2012 16:54
-
-
Save jwo/1942427 to your computer and use it in GitHub Desktop.
DRY Mongoid modules
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
#http://jessewolgamott.com/blog2012/02/29/the-one-where-we-dry-up-mongoid-and-rspec-using-shared-examples-and-modules/ | |
require 'spec_helper' | |
module Contactable | |
def self.included(receiver) | |
receiver.class_eval do | |
field :first_name, type: String | |
field :last_name, type: String | |
field :phone_number, type: String | |
field :address_line_1, type: String | |
field :city, type: String | |
field :state, type: String | |
field :postal_code, type: String | |
field :email, type: String | |
validates_presence_of :first_name | |
validates_presence_of :last_name | |
end | |
end | |
end | |
class Doctor | |
include Mongoid::Document | |
include Mongoid::Timestamps | |
include Contactable | |
end | |
shared_examples_for Contactable do | |
it { should have_fields(:first_name, :last_name) } | |
it { should have_fields(:phone_number, :email, :address_line_1, :city, :state, :postal_code) } | |
it { should validate_presence_of(:first_name) } | |
it { should validate_presence_of(:last_name) } | |
end | |
describe Doctor do | |
it_behaves_like Contactable | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment