Module: Blueprints::Extensions::Blueprintable::ClassMethods

Defined in:
lib/blueprints/extensions.rb

Instance Method Summary collapse

Instance Method Details

#blueprint(attributes) ⇒ Object #blueprint(name, attributes = {}) ⇒ Blueprints::Blueprint

Overloads:

  • #blueprint(attributes) ⇒ Object

    Does same as create! method except that it also bypasses attr_protected and attr_accessible. Typically used in blueprint block.

    Examples:

    Create post for user

    @user.posts.blueprint(:title => 'first post', :text => 'My first post')

    Parameters:

    • attributes (Hash, Array<Hash>)

      Attributes used to create objects.

    Returns:

    • Created object(s).

  • #blueprint(name, attributes = {}) ⇒ Blueprints::Blueprint

    Defines new blueprint that creates an object with attributes passed.

    Examples:

    Create blueprint named :post.

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

    Parameters:

    • name (String, Symbol, Hash)

      Name of blueprint.

    • attributes (Hash) (defaults to: {})

      Attributes hash.

    Returns:



33
34
35
36
37
38
39
40
41
# File 'lib/blueprints/extensions.rb', line 33

def blueprint(*args)
  if Blueprints::Context.current
    attrs = args.extract_options!
    define_blueprint(args.first || Blueprints::Buildable.infer_name(attrs) || name.underscore, attrs)
  else
    objects = args.collect { |attrs| blueprint_object(attrs) }
    args.size == 1 ? objects.first : objects
  end
end