class FactoryBot::Generators::ModelGenerator

Public Instance Methods

create_fixture_file() click to toggle source
# File lib/generators/factory_bot/model/model_generator.rb, line 28
def create_fixture_file
  if File.exist?(factories_file)
    insert_factory_into_existing_file
  else
    create_factory_file
  end
end

Private Instance Methods

create_factory_file() click to toggle source
# File lib/generators/factory_bot/model/model_generator.rb, line 50
def create_factory_file
  file = File.join(options[:dir], "#{filename}.rb")
  template "factories.erb", file
end
factories_file() click to toggle source
# File lib/generators/factory_bot/model/model_generator.rb, line 38
def factories_file
  options[:dir] + ".rb"
end
factory_attributes() click to toggle source
# File lib/generators/factory_bot/model/model_generator.rb, line 64
def factory_attributes
  attributes.map do |attribute|
    "#{attribute.name} { #{attribute.default.inspect} }"
  end.join("\n")
end
factory_bot_options() click to toggle source
# File lib/generators/factory_bot/model/model_generator.rb, line 83
def factory_bot_options
  generators.options[:factory_bot] || {}
end
factory_definition() click to toggle source
# File lib/generators/factory_bot/model/model_generator.rb, line 55
      def factory_definition
        <<~RUBY
            factory :#{singular_table_name}#{explicit_class_option} do
          #{factory_attributes.gsub(/^/, '    ')}
            end

        RUBY
      end
filename() click to toggle source
# File lib/generators/factory_bot/model/model_generator.rb, line 70
def filename
  if factory_bot_options[:filename_proc].present?
    factory_bot_options[:filename_proc].call(table_name)
  else
    name = File.join(class_path, plural_name)
    [name, filename_suffix].compact.join("_")
  end
end
filename_suffix() click to toggle source
# File lib/generators/factory_bot/model/model_generator.rb, line 79
def filename_suffix
  factory_bot_options[:suffix] || options[:suffix]
end
generators() click to toggle source
# File lib/generators/factory_bot/model/model_generator.rb, line 87
def generators
  FactoryBotRails::Railtie.config.app_generators
end
insert_factory_into_existing_file() click to toggle source
# File lib/generators/factory_bot/model/model_generator.rb, line 42
def insert_factory_into_existing_file
  insert_into_file(
    factories_file,
    factory_definition,
    after: "FactoryBot.define do\n",
  )
end