I''m wanted to make a "global" module that I could include in my application for some shared code between them. I''ve been making several camping apps and wanted them all to share a helper/partial that contained the navigation. I ran into a lot of difficulty when trying to include it, the primary problem being "methodUndefined" errors. I did finally get it to work, but I''m not sure why. Here''s the relevant bits: my_app.rb MyApp::Helpers include Global::Helpers def month_name ...helper code end end MyApp::Views def layout ...some markaby :navigation #throws an UndefinedMethod error if not a symbol ...some more markaby end end global.rb module Global module Helpers def navigation ...what i need end end end If I list the instance methods in irb, both "navigation" and "month_name" shows up. However, I have to use use :navigation as a symbol in my view as opposed to just using month_name for the other. Both are defined as instance methods. What gives with the symbol usage?
I was wrong. A symbol didn''t work, it just didn''t throw an error.>From the code below, can anyone see why I wouldn''t be able tonamespace a Global::Helpers module and include it in MyApp::Helpers and have the methods available? This seems to be the way that Camping::BasicAuth works, are views/helpers different? ---------- Forwarded message ---------- From: Joshua Schairbaum <joshua.schairbaum at gmail.com> Date: May 7, 2007 2:31 PM Subject: Module Madness To: camping-list at rubyforge.org I''m wanted to make a "global" module that I could include in my application for some shared code between them. I''ve been making several camping apps and wanted them all to share a helper/partial that contained the navigation. I ran into a lot of difficulty when trying to include it, the primary problem being "methodUndefined" errors. I did finally get it to work, but I''m not sure why. Here''s the relevant bits: my_app.rb MyApp::Helpers include Global::Helpers def month_name ...helper code end end MyApp::Views def layout ...some markaby :navigation #throws an UndefinedMethod error if not a symbol ...some more markaby end end global.rb module Global module Helpers def navigation ...what i need end end end If I list the instance methods in irb, both "navigation" and "month_name" shows up. However, I have to use use :navigation as a symbol in my view as opposed to just using month_name for the other. Both are defined as instance methods. What gives with the symbol usage?
On Tue, May 08, 2007 at 12:42:55PM -0400, Joshua Schairbaum wrote:> I was wrong. A symbol didn''t work, it just didn''t throw an error. > >From the code below, can anyone see why I wouldn''t be able to > namespace a Global::Helpers module and include it in MyApp::Helpers > and have the methods available? > > This seems to be the way that Camping::BasicAuth works, are > views/helpers different?As far as I know helpers are methods in the MyApp module. I don''t know if Camping uses or ever has used MyApp::Helpers. -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: not available Url : http://rubyforge.org/pipermail/camping-list/attachments/20070508/6b5f1bab/attachment.bin
On May 7, 2007, at 1:31 PM, Joshua Schairbaum wrote:> I''m wanted to make a "global" module that I could include in my > application for some shared code between them. I''ve been making > several camping apps and wanted them all to share a helper/partial > that contained the navigation.I think the root of the "problem" is the way the app modules are loaded into the Markaby rendering context: module Views; include Controllers, Helpers end class Mab < Markaby::Builder include Views def tag!(*g,&b) h=g[-1] [:href,:action,:src].each{|a|(h[a]=self/h[a])rescue 0} super end end Here''s a slightly unnerving hack which seems to solve the problem. You pass it the base module (or symbol of) your Camping app and one or more shared helper modules, and it''ll inject them into your Camping app. (Obviously the symbol parameters won''t work if you have nested modules...you''ll have to pass in the module constant instead...the symbols are just syntactic sugar to echo Camping.goes().) class TentSteak def self.bootstrap(mod_or_sym, *appmods) @@rootmod = mod_or_sym.instance_of?(Module) ? mod_or_sym : const_get(mod_or_sym.to_s) inject appmods end def self.inject(*mod_or_syms) @@rootmod.module_eval "class Mab < Markaby::Builder; include # {mod_or_syms.join('', '')}; end" end def self.view_method_defined?(meth) @@rootmod::Mab.method_defined?(meth) end end Usage is like this: Camping.goes :MyApp TentSteak.bootstrap :MyApp, :MyHelperModule, :MyOtherHelpers I''d love to hear if there''s a better way to do this injection. Seems pretty hacky. Incidentally, TentSteak is a Camping helper library I''ve been working on recently. I''ll upload it to rubyforge as soon as I get the ugly corners chiseled off and properly tested/ documented.... John -- "The Internet is not something you just dump something on. It''s not a big truck. It''s a series of tubes." --Senator Ted Stevens, R-AK John R. Sheets http://bark.metacasa.net -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/camping-list/attachments/20070508/3067553e/attachment.html