How to properly define constants in helpers?

34 views
Skip to first unread message

Michael Schuerig

unread,
Aug 30, 2010, 10:44:59 AM8/30/10
to rubyonra...@googlegroups.com

Let's say I have a helper module like this

module ThingsHelper
DEFAULT_OPTIONS = { ... }.freeze

def my_helper(options = {})
options.reverse_merge!(DEFAULT_OPTIONS)
...
end
end

Then, in development environment, I get

.../app/helpers/things_helper.rb:2: warning: already initialized
constant DEFAULT_OPTIONS

Is there anything I can do about this warning?

Michael

--
Michael Schuerig
mailto:mic...@schuerig.de
http://www.schuerig.de/michael/

richardsugg

unread,
Aug 30, 2010, 11:14:46 AM8/30/10
to Ruby on Rails: Talk
I think you are having the same problem as I at
http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/b2ff5ff2b9e009d7
. I bet your helper is getting included twice.

On Aug 30, 10:44 am, Michael Schuerig <mich...@schuerig.de> wrote:
> Let's say I have a helper module like this
>
> module ThingsHelper
>   DEFAULT_OPTIONS = { ... }.freeze
>
>   def my_helper(options = {})
>     options.reverse_merge!(DEFAULT_OPTIONS)
>     ...
>   end
> end
>
> Then, in development environment, I get
>
> .../app/helpers/things_helper.rb:2: warning: already initialized
> constant DEFAULT_OPTIONS
>
> Is there anything I can do about this warning?
>
> Michael
>
> --
> Michael Schuerig
> mailto:mich...@schuerig.dehttp://www.schuerig.de/michael/

Michael Schuerig

unread,
Aug 30, 2010, 12:40:21 PM8/30/10
to rubyonra...@googlegroups.com
On Monday 30 August 2010, richardsugg wrote:
> I think you are having the same problem as I at
> http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/
> b2ff5ff2b9e009d7 . I bet your helper is getting included twice.

I'm not sure. I'm *never* including it explicitly. As it is a helper, it
is loaded automatically.

However, there is one special thing. I'm defining the helper in question
in an engine and to have it included automatically by helper :all, I add
the path engine helper path in an initializer block of the engine

initializer 'make MyEngine helpers automatically loadable',
:before => "action_controller.set_configs" do |app|
paths.app.helpers.each do |path|
app.paths.app.helpers << path
end
end

and unfortunately, that in some way appears to cause the warning.

Michael

--
Michael Schuerig
mailto:mic...@schuerig.de
http://www.schuerig.de/michael/

Reply all
Reply to author
Forward
0 new messages