Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Ensure ActionController::UrlWriter#default_url_options are unique across classes

#1277

In a Rails project I'm working on, we use both ActionMailer and ActiveSMS. When sending out emails, the URLs point to our HTML website (www.example.com), and when we send out SMS they point to our XHTML-MP website (m.example.com). To do this, we do this:


class MyMailer < ActionMailer::Base
  # I know that this include isn't necessary, but it's there for clarity
  include ActionController::UrlWriter
  default_url_options[:host] = 'www.example.com'
end

class MyMessenger < ActiveSMS
  include ActionController::UrlWriter
  default_url_options[:host] = 'm.example.com'
end

Without this patch, setting default_url_options in MyMessenger causes the settings in MyMailer to be overridden. Whichever class referenced last, wins.

I don't think that should happen.

Reported by Nathan de Vries · October 27th, 2008 @ 12:29 PM

State: resolved
Milestone: none
Assigned to: Pratik Pratik
Importance: none

Activity

  1. Pratik
    Pratik
    • Milestone cleared.
    • Assigned user set to Pratik
    • Title changed from [PATCH] Ensure ActionController::UrlWriter#default_url_options are unique across classes to Ensure ActionController::UrlWriter#default_url_options are unique across classes

    January 19th, 2009 @ 08:13 PM

  2. Repository
    Repository
    • State changed from new to resolved

    (from [32eeb3e5211a4a7bfc7a1d0aa0cab1486bed3581]) Ensure that when UrlWriter is included in multiple classes, the default_url_options of one don't affect the other. [#1277 Ensure ActionController::UrlWriter#default_url_options are unique across classes state:resolved]

    Signed-off-by: Pratik Naik pratiknaik@gmail.com http://github.com/rails/rails/co...

    January 28th, 2009 @ 08:01 PM

  3. Nathan de Vries
  4. Paco Benavent
    Paco Benavent
    • Tag changed from 2.2, patch, url_rewriter to 2.2, 2.3, default_url_options, patch, urlwriter, url_rewriter

    This solution breaks compatibility with previous version, where you could assign default_url_options for the whole app in a file in config/initializers, for example:

    config/initializers/mailer_opts.rb:

    
       ActionController::UrlWriter::default_url_options[:host] = 'localhost'
       ActionController::UrlWriter::default_url_options[:port] = 3000
    

    In 2.3 this raises the following error:

    undefined method `default_url_options' for ActionController::UrlWriter:Module (NoMethodError)
    
    

    is there any alternative solution?

    March 24th, 2009 @ 02:35 PM

  5. findchris
    findchris

    Same exact problem as Paco was encountered when upgrading to Rails 2.3.

    +1

    March 24th, 2009 @ 09:29 PM

  6. Paco Benavent
    Paco Benavent

    After inspecting Rails 2.3.2 source code, it seems that UrlWriter is only included in class ActionMailer::Base. So I suppose that changing the previous example to:

    
    ActionMailer::Base.default_url_options[:host] = 'localhost'
    ActionMailer::Base.default_url_options[:port] = 3000
    

    or alternatively, in config/environment.rb:

    
    config.action_mailer.default_url_options = { :host => 'localhost', :port => 3000 }
    

    it's enough if you are using it only for mailers.

    But, looking at Nathan's example, if you want the same options both for mailer and sms you have to declare it twice. Not very DRY.

    March 25th, 2009 @ 11:29 AM

  7. Nathan de Vries
    Nathan de Vries

    @Paco Benavent: I think that's a non-issue, given that you are configuring two completely independent classes. The only other option is to modify the behavior of mattr_accessor such that the values are distinct for the original module, and each class the module is mixed into.

    March 26th, 2009 @ 04:41 AM