/
blueprint.rb
40 lines (37 loc) · 1.15 KB
/
blueprint.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
module FactoryGirl
module Syntax
# Extends ActiveRecord::Base to provide a make class method, which is an
# alternate syntax for defining factories.
#
# Usage:
#
# require 'factory_girl/syntax/blueprint'
#
# User.blueprint do
# name { 'Billy Bob' }
# email { 'billy@bob.example.com' }
# end
#
# FactoryGirl.create(:user, name: 'Johnny')
#
# This syntax was derived from Pete Yandell's machinist.
# @api private
module Blueprint
module ActiveRecord
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def blueprint(&block)
ActiveSupport::Deprecation.warn 'Model.blueprint is deprecated; use the FactoryGirl.define syntax instead', caller
instance = Factory.new(name.underscore, class: self)
proxy = FactoryGirl::DefinitionProxy.new(instance)
proxy.instance_eval(&block)
FactoryGirl.register_factory(instance)
end
end
end
end
end
end
ActiveRecord::Base.send(:include, FactoryGirl::Syntax::Blueprint::ActiveRecord)