On Apr 8, 5:06 pm, Frederick Cheung <[email protected]>
wrote:
>
> Because you're in development mode, the app's code, including
> application_helper is reloaded for each request, but your plugin's
> init.rb is only called once and never gets to do your monkey patching
> on the reloaded copies of application helper
>
> You might be able to use a to_prepare callback (these get called
> before each request in development mode) if you're going to use this a
> lot in development mode - take a peek inside action_dispatch/
> middleware/callbacks

Yes, but I *was* using a to_prepare block (w/o actually understanding
what it does, though.)

So what I now actually have in init.rb is this:

Dispatcher.to_prepare :redmine_model_dependencies do
  puts "!!! to_prepare block !!!"

  require_dependency 'application_helper'

  unless ApplicationHelper.included_modules.include?
RedminePastebin::ApplicationHelperPatch
    ApplicationHelper.send(:include,
RedminePastebin::ApplicationHelperPatch)
  end
end

If I put some logging lines into redmine's application_helper.rb, I
can get this:

module ApplicationHelper
  def self.included(base)
    puts "ApplicationHelper: INCLUDED: #{self.method_defined?
(:parse_redmine_links_with_pastes)}"
  end
  ...
  def parse_redmine_links(text, project, obj, attr, only_path,
options)
    logger.info "parse_redmine_links"
  ...


And in my patch-module I have this log line:

    def parse_redmine_links_with_pastes(text, project, obj, attr,
only_path,
                                        options)
      logger.info "parse_redmine_links_with_pastes"
     ...

Now for the first page load I get these:

ApplicationHelper: INCLUDED: false
ApplicationHelper: INCLUDED: false
!!! to_prepare block !!!
=> Call with -d to detach
=> Ctrl-C to shutdown server
!!! to_prepare block !!!
ApplicationHelper: INCLUDED: true
...
parse_redmine_links_with_pastes
...

and for the second load, these:

!!! to_prepare block !!!
ApplicationHelper: INCLUDED: true
ApplicationHelper: INCLUDED: true
ApplicationHelper: INCLUDED: true
...
parse_redmine_links

So original unmodified method is called regardless of the fact that
the new one is defined...

I guess I need to move that alias_method_chain call somewhere else so
it's called on every page load.  How do I do so?

--
Thanks,
Alex

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to