Class: FactoryGirlWeb::Fixture

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
app/models/factory_girl_web/fixture.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#amountObject

Returns the value of attribute amount.



5
6
7
# File 'app/models/factory_girl_web/fixture.rb', line 5

def amount
  @amount
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'app/models/factory_girl_web/fixture.rb', line 5

def name
  @name
end

#traitsObject

Returns the value of attribute traits.



5
6
7
# File 'app/models/factory_girl_web/fixture.rb', line 5

def traits
  @traits
end

Class Method Details

.allObject



38
39
40
# File 'app/models/factory_girl_web/fixture.rb', line 38

def all
  factories.keys.map { |name| new(name: name, amount: 1) }
end

.factoriesObject



42
43
44
45
46
47
48
# File 'app/models/factory_girl_web/fixture.rb', line 42

def factories
  @factories ||= begin
    FactoryGirl.factories.sort_by(&:name).each_with_object(HashWithIndifferentAccess.new) do |factory, h|
      h[factory.name] = factory.defined_traits.map(&:name)
    end
  end
end

Instance Method Details

#createObject



14
15
16
17
18
19
20
# File 'app/models/factory_girl_web/fixture.rb', line 14

def create
  return unless valid?

  ActiveRecord::Base.transaction do
    FactoryGirl.create_list(name.to_sym, amount.to_i, *formatted_traits)
  end
end

#defined_traitsObject



26
27
28
# File 'app/models/factory_girl_web/fixture.rb', line 26

def defined_traits
  factories[name] || []
end

#name_i18nObject



22
23
24
# File 'app/models/factory_girl_web/fixture.rb', line 22

def name_i18n
  name.to_s.classify.safe_constantize.try(:model_name).try(:human) || name
end