モジュール名がHOGE::Generators::ConfigGenerator
の時自動的に小文字変換されるので、
$ rails g h_o_g_e:config
となってしまう。
なので、namespaceを指定してやる必要がある。
module HOGE
module Generators
class ConfigGenerator < Rails::Generators::Base
source_root File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
namespace "hoge:config" # <- THIS!!
def copy_config_file
template 'hoge_config.rb', 'config/initializers/hoge_config.rb'
end
end
end
end
この時、namespaceがフォルダ名にも影響してくるので、フォルダ名は
lib/generators/hoge/templates/hoge_config.rb
でなければならない。
色々試している時に、フォルダ名を
lib/generators/h_o_g_e/templates/config_generator.rb
としていたために、エラーが発生しハマった。source_root
で指定してるパスからテンプレートも拾ってくれると思ってたんだが…