Module: Blueprints::Blueprintable::ClassMethods

Defined in:
lib/blueprints/core_ext.rb

Instance Method Summary collapse

Instance Method Details

#blueprint(name_or_attrs, attrs = {}) ⇒ Object

Two forms of this method can be used. First one is typically used inside blueprint block. Essentially it does same as create!, except it does bypass attr_protected and attr_accessible. It accepts only a hash or attributes, same as create! does.

blueprint :post => [:user, :board] do
  @user.posts.blueprint(:title => 'first post', :text => 'My first post')
end

The second form is used when you want to define new blueprint. It takes first argument as name of blueprint and second one as hash of attributes. As you cannot use instance variables outside of blueprint block, you need to prefix them with colon. So the example above could be rewritten like this:

Post.blueprint(:post, :title => 'first post', :text => 'My first post', :user => d(:user)).depends_on(:board)


14
15
16
17
18
19
20
21
22
23
24
# File 'lib/blueprints/core_ext.rb', line 14

def blueprint(name_or_attrs, attrs = {})
  if Blueprints::FileContext.current
    define_blueprint(name_or_attrs, attrs)
  else
    if name_or_attrs.is_a?(Array)
      name_or_attrs.collect { |attrs| blueprint(attrs) }
    else
      blueprint_object(name_or_attrs)
    end
  end
end