Faker: How do I include Faker's customisation without changing the files in the gem?

Created on 23 Mar 2016  路  8Comments  路  Source: faker-ruby/faker

According to customization section, I only need to include a YAML file in /lib/locales to add new options to Faker

en:
faker:
company:
store_type: [Collection Point, Flagship Store, Cleaning Centre]
address:
postcode_cn: [310000,329999,430000,449999,100001,102600]
province: [Zhejiang, Shanghai, Beijing, Guangzhou, Jiangsu, Fujian]
city_cn: [Hangzhou, Wenzhou, Ningbo, Fuding]
district: [Binjiang, Xiaoshan, Xihu, Xiacheng, Shangcheng]

This file is what I added, but when I use it in FactoryGirl. It still gave me the error like the following one:

NoMethodError: undefined method `district' for Faker::Address:Class

How do I include this file to Faker to make it work

Most helpful comment

This issue is still relevant... solution for 2018:

I think it's easy to just prepend it during initialization like this:
Create a file called faker.rb in your config/initializers folder and add something like this to it:

module StoreType
  class Store < Faker::Base
    class << self
      def type
        fetch('company.store_types')
      end
    end
  end
end
Faker.prepend StoreType

This will prepend the module and you can call it like this:
Faker::Store.type

Then in the folder config/locals create a file called en.yml and add this:

en:
  faker:
    company:
      store_type: [Collection Point, Flagship Store, Cleaning Centre]

You may also want to set default yml to load by adding this:

#Faker gem configuration
Faker::Config.locale = 'en'

To:
config/environments/development.rb and config/environments/test.rb

It will look there first and if does not find anything it will go to the original default.

Cheers馃檪

All 8 comments

I am using Rails 4.2

You just have to add new methods. For this example:

def store_type
  fetch('company.store_type')
end

inside lib/faker/company.rb and:

def postcode_cn
  fetch('address.postcode_cn')
end

def province
  fetch('address.province')
end

def city_cn
  fetch('address.city_cn')
end

def district
  fetch('address.district')
end

inside lib/faker/address.rb

Hi @oasisweng, is this still open or can the issue be closed?

For a complete solution, I would suggest:

  1. Creating a new file: lib/faker/company.rb.

  2. Define your file as:
    require 'faker'
    module Faker
    class Company < Base
    class << self
    def store_type; fetch('company. store_type'); end
    ...
    end
    end
    end
    And for each category define a new def category_name; end

  3. Then, if not yet configured, add these line to config/application.rb:
    class Application < Rails::Application
    config.autoload_paths << Rails.root.join("lib")
    end
    OR to add only faker folder:
    https://stackoverflow.com/a/19650564/7119365

The fetch('company. store_type') get the data defined in your i8ln file. Now all should be working fine.

This issue is still relevant... solution for 2018:

I think it's easy to just prepend it during initialization like this:
Create a file called faker.rb in your config/initializers folder and add something like this to it:

module StoreType
  class Store < Faker::Base
    class << self
      def type
        fetch('company.store_types')
      end
    end
  end
end
Faker.prepend StoreType

This will prepend the module and you can call it like this:
Faker::Store.type

Then in the folder config/locals create a file called en.yml and add this:

en:
  faker:
    company:
      store_type: [Collection Point, Flagship Store, Cleaning Centre]

You may also want to set default yml to load by adding this:

#Faker gem configuration
Faker::Config.locale = 'en'

To:
config/environments/development.rb and config/environments/test.rb

It will look there first and if does not find anything it will go to the original default.

Cheers馃檪

I think that this issue is closed

Awesome discussion. I enjoyed reading it. I'm closing this issue for now. Thanks 馃憤

How can you make this work in production when you don't install Faker? I get an error like uninitialized constant GameProperties::Faker (NameError)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tejasbubane picture tejasbubane  路  5Comments

ggrillone picture ggrillone  路  6Comments

CJAdeszko picture CJAdeszko  路  6Comments

kevgathuku picture kevgathuku  路  4Comments

tagliala picture tagliala  路  5Comments