Module: FixtureFactory::Methods

Defined in:
lib/fixture_factory/methods.rb

Instance Method Summary collapse

Instance Method Details

#attributes_for(name, overrides = {}) ⇒ Object

Generates a hash of attributes given a factory name and an optional hash of override attributes.

Example

attributes_for(:user) attributes_for(:blog, title: ‘Riding Rails’) attributes_for(:comment, content: ‘Hello’, approved: false) attributes_for(:post, comments_attributes: [attributes_for(:comment)])



14
15
16
17
18
# File 'lib/fixture_factory/methods.rb', line 14

def attributes_for(name, overrides = {})
  FixtureFactory.attributes_for(
    name, overrides: overrides, context: self, scope: self.class
  )
end

#attributes_for_list(name, count, overrides = {}) ⇒ Object

Generates an array of hash attributes given a factory name, a count, and an optional hash of override attributes.

Example

attributes_for_list(:user, 5) attributes_for_list(:blog, 3, title: ‘Riding Rails’) attributes_for_list(:comment, 50, content: ‘Hello’, approved: false) attributes_for_list(:post, 3, comments_attributes: attributes_for_list(:comment, 1))



29
30
31
# File 'lib/fixture_factory/methods.rb', line 29

def attributes_for_list(name, count, overrides = {})
  count.times.map { attributes_for(name, overrides) }
end

#build(name, overrides = {}) ⇒ Object

Generates an instance of a model given a factory name and an optional hash of override attributes.

Example

build(:user) build(:blog, title: ‘Riding Rails’) build(:comment, content: ‘Hello’, approved: false) build(:post, comments: [build(:comment)])



42
43
44
45
46
# File 'lib/fixture_factory/methods.rb', line 42

def build(name, overrides = {})
  FixtureFactory.build(
    name, overrides: overrides, context: self, scope: self.class
  )
end

#build_list(name, count, overrides = {}) ⇒ Object

Generates an array of model instances given a factory name and an optional hash of override attributes.

Example

build_list(:user, 5) build_list(:blog, 3, title: ‘Riding Rails’) build_list(:comment, 50, content: ‘Hello’, approved: false) build_list(:post, 3, comments: build_list(:comment, 1))



57
58
59
# File 'lib/fixture_factory/methods.rb', line 57

def build_list(name, count, overrides = {})
  count.times.map { build(name, overrides) }
end

#create(name, overrides = {}) ⇒ Object

Generates a persisted model instance given a factory name and an optional hash of override attributes.

Example

create(:user) create(:blog, title: ‘Riding Rails’) create(:comment, content: ‘Hello’, approved: false) create(:post, comments: [create(:comment)])



70
71
72
73
74
# File 'lib/fixture_factory/methods.rb', line 70

def create(name, overrides = {})
  FixtureFactory.create(
    name, overrides: overrides, context: self, scope: self.class
  )
end

#create_list(name, count, overrides = {}) ⇒ Object

Generates an array of persisted model instances given a factory name and an optional hash of override attributes.

Example

create_list(:user, 5) create_list(:blog, 3, title: ‘Riding Rails’) create_list(:comment, 50, content: ‘Hello’, approved: false) create_list(:post, 3, comments: create_list(:comment, 1))



85
86
87
# File 'lib/fixture_factory/methods.rb', line 85

def create_list(name, count, overrides = {})
  count.times.map { create(name, overrides) }
end