+1 to this idea in general.
On 06/06/2006, at 7:59 AM, Julian 'Julik' Tarkhanov wrote:
Ok, I think I'll tackle it the following way.
1. specifically for ActionMailer
ActionMailer::Base.current_relative_controller =
current_controller (as a before_filter for the ActionController::Base)
and forward the url_for directly to the instance of it
Seems like a proper fit for Railties as a mixin for AM.
If I call my ActionMailer from a unit test (or runner script, or the
console, etc.) then there is no controller. Would url_for in the
ActionMailer then fail? (I've attached the code I currently use
below, and it suffers from this problem.)
2. for all the other cases that might need the infrastructure
ActionController::Routing::Routes.default_request_parameters =
{:host => 'bla'}
and retrieve it through something like
ActionController::Routing::Routes.default_url_for(*params)
This is sensible. url_for should really be defined as part of the
routing infrastructure with some configurable values for host, etc.
ActionController and ActionMailer should then have url_for instance
methods that call out to the url_for in routing. In the case of
ActionController, it would pass its own values for host, protocol,
etc. In the case of ActionMailer, it would use the defaults from the
config. (You could, of course, manually pass your own values in if
you need to generate them dynamically, as would be the case if you
need to generate links to more than one host.)
If my somewhat limited understanding of the internals of Rails
routing means I've suggested something dumb above, please feel free
to straighten me out. :)
Pete Yandell
http://9cays.com/
This is the code I currently use to solve this problem. In a
before_filter I do UrlGenerator.controller = self, and then I can
call UrlGenerator.url_for from my mailer to generate URLs. (I pinched
this idea from an open source Rails project somewhere, but I don't
recall where. My apologies to the original author.)
class UrlGenerator
@@controller ||= nil
def self.controller=(controller)
@@controller = controller
end
def self.url_for(options)
@@controller.url_for(options) if @@controller
end
end
_______________________________________________
Rails-core mailing list
Rails-core@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails-core