Question

I want to extend the faker gem for rails to also generate other random date (in my case computer game names)

#lib/extended_faker.rb
require 'faker'
require 'extended_faker/game'

#lib/extended_faker/game.rb
Module Faker
    class Game < Faker::Base
        class << self
            def name
                fetch('game.name')
            end
        end
    end
end

#config/locals/faker_en.yml
en:
  faker:
    game:
      name: ["a", "b", "c"]

#config/application.rb
...
config.autoload_paths += Dir["#{config.root}/lib/**/"]
...

then when i run it in a rails console i get the following

Loading development environment (Rails 3.2.9)
1.9.3p194 :001 > Faker::Game.name
LoadError: Expected /home/enermis/School/Projects/IG/test/lib/extended_faker/game.rb to define Game
    from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:503:in `load_missing_constant'
    from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:192:in `block in const_missing'
    from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:190:in `each'
    from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:190:in `const_missing'
    from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:514:in `load_missing_constant'
    from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:192:in `block in const_missing'
    from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:190:in `each'
    from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:190:in `const_missing'
    from (irb):1
    from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.9/lib/rails/commands/console.rb:47:in `start'
    from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.9/lib/rails/commands/console.rb:8:in `start'
    from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.9/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

When i change the lib/extended_faker/game.rb file to this

require 'faker'
require 'extended_faker/game'
include 'extended_faker/item'
include 'extended_faker/team'

i get weird behavior in the console

Loading development environment (Rails 3.2.9)
1.9.3p194 :001 > Faker::Game.name
 => "b" 
1.9.3p194 :002 > Faker::Game.name
NameError: uninitialized constant Faker::Game
    from (irb):2
    from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.9/lib/rails/commands/console.rb:47:in `start'
    from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.9/lib/rails/commands/console.rb:8:in `start'
    from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.9/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

The weird thing i don't understand is that the first time i run the generator, i get a valid result, but the second time around i get an error...

What am i missing?

Was it helpful?

Solution

you shoud put it in
lib/faker/game.rb
starting in the lib directory, rails convention is outermost module name -> in are folder names. Then the actual class/module name is the file name, underscored to the camelcase.
Another example

module Foo
  module Bar
    class BazParty
      def self.hello
        puts "hello"
      end
    end
  end
end

would go in lib/foo/bar/baz_party.rb

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top