This project is archived and is in readonly mode.

#1277 ✓resolved
Nathan de Vries

Ensure ActionController::UrlWriter#default_url_options are unique across classes

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

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.

Comments and changes to this ticket

  • Pratik

    Pratik January 19th, 2009 @ 08:13 PM

    • 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”
  • Repository

    Repository January 28th, 2009 @ 08:01 PM

    • 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 state:resolved]

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

  • Nathan de Vries
  • Paco Benavent

    Paco Benavent March 24th, 2009 @ 02:35 PM

    • 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?

  • findchris

    findchris March 24th, 2009 @ 09:29 PM

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

    +1

  • Paco Benavent

    Paco Benavent March 25th, 2009 @ 11:29 AM

    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.

  • Nathan de Vries

    Nathan de Vries March 26th, 2009 @ 04:41 AM

    @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.

  • csnk

    csnk May 18th, 2011 @ 08:18 AM

    • Importance changed from “” to “”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments

Referenced by

Pages