ruby - Create Generic Module in rails -
i'm newbie rails. have created reports module particular project. now, want make generic across project reports gem. question not how create & use gem. questions "how make generic reports lib". eg. have helper module in reports,
module libquery module helper include queryconstants(which dynamic - based on project) #methods end end
end
my approach: each project include libquery::helper , include own constants file.
module projectx module query module helper include libquery::helper #nothing - inherit helper methods in libquery end end end
but i'm wondering if that's elegant way of doing things ? or better way it?
first of all, modules must capitalized:
module mymodulename
second, use lib
it's best include in autoload_paths
(in application.rb
file) this
config.autoload_paths += %w(#{rails.root}/lib/my_shared_libs)
this means rails load automatically, , you'll have available 'out of box'.
third, external modules shouldn't depend on project-based modules , classes, since whole point make them reusable.
so boils down this:
#/lib/my_shared_libs/fun_things.rb module funthings ... code def laugh puts 'haha' end end #/models/user.rb class user include funthings end user.new.laugh # => 'haha'
Comments
Post a Comment