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
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:
Creating a new file: lib/faker/company.rb.
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
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)
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.rbin yourconfig/initializersfolder and add something like this to it:This will prepend the module and you can call it like this:
Faker::Store.typeThen in the folder
config/localscreate a file calleden.ymland add this:You may also want to set default yml to load by adding this:
To:
config/environments/development.rbandconfig/environments/test.rbIt will look there first and if does not find anything it will go to the original default.
Cheers馃檪