Skip to content

Instantly share code, notes, and snippets.

@jwo
Created February 29, 2012 16:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwo/1942427 to your computer and use it in GitHub Desktop.
Save jwo/1942427 to your computer and use it in GitHub Desktop.
DRY Mongoid modules
#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