ruby - Rails Railtie initializer does not run -
i'm developing gem use in rails 4 application include bit of code run initializer host application. adding class module const_set
, code isn't ever being run. also, when put gem in app's gemfile , start either rails console or server, classes expect defined gem not. if plain require 'my_gem'
console, classes expect defined defined, , if run initialization code manually, no errors thrown. know must missing trivial, can't figure out is.
here mygem/lib/mygem.rb
:
require "mygem/railtie" if defined?(rails) require "mygem/version" module mygem class myclass def self.my_method(argument1, argument2, argument3) argument1 == argument2 + argument3 end end end
here mygem/lib/mygem/railtie.rb
:
module mygem class railtie < rails::railtie initializer "mygem.define_subclass" previouslydefinedappmodule.const_set "myclass", mygem::myclass end end end
after putting gem in app's gemfile , start console, mygem
module defined, mygem::myclass
not defined, however. also, far can tell, no initializer code gem ever run (it fail since class wasn't defined). when require 'mygem'
, mygem::myclass
defined. i've taken @ number of rails plugins , railtie tutorials , add gem gemfile , should work. can spot i'm doing wrong?
i've figured out after posted. gem name my-gem
, module's file name my_gem.rb
. in host app's gemfile have line gem my-gem
, load gem need require 'my_gem'
. so, in order fix this, need replace line in host app's gemfile gem my-gem, require: "my_gem"
(or fix confusing , haphazard naming scheme :).
Comments
Post a Comment