I know the Rails::Initializer class process the initialization for apps. But
i found the method is under the protection. That is,
Rails::Initializer.run do |config|
end
when i invoke the run method, it then do process method which is the
actually  initializing method.
I want to know how can i extend the method without changing
railties/lib/initializer.rb, in other words, how to extend it?
My requirement is to initialize ActionMailer::Base.smtp_settings. Though
there''s a built-in way to do it, i prefer the way performed as
database.yml,
which can put development, production and test environment all in a file.
For example:
development:
  server: 127.0.0.1
  port: 25
  domain: localhost
production:
  server: 127.0.0.1
  port: 25
  domain: localhost
Recently, I use this way.
module Rails
  class Initializer
    def initialize_smtp
      if configuration.frameworks.include?(:action_mailer) && RAILS_ENV
!''test''
        ActionMailer::Base.smtp_settings
(configuration.smtp_configuration[RAILS_ENV]
|| {}).symbolize_keys
      end
    end
  end
  class Configuration
    attr_accessor :smtp_configuration_file
    def smtp_configuration
      self.smtp_configuration_file ||= default_smtp_configuration_file
      YAML::load(ERB.new(IO.read(smtp_configuration_file)).result)
    end
    private
    def default_smtp_configuration_file
      File.join("#{RAILS_ROOT}", "config",
"mailer.yml")
    end
  end
end
But following this, I only can initialize the mail system after the
initialization, as:
initializer = Rails::Initializer.run do |config|
end
initializer.initialize_smtp
It works, but not perfect really.
Does this a way to directly do the work when rails initialize itself? Thank
you for any advice.
--~--~---------~--~----~------------~-------~--~----~
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
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Ye Dingding
2007-Sep-09  08:59 UTC
Re: How to add a initialize method when rails app starts?
Does anybody have any advice? Or i have to rewrite process method to fit the requirement. On 9/6/07, Ye Dingding <yedingding-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I know the Rails::Initializer class process the initialization for apps. > But i found the method is under the protection. That is, > > Rails::Initializer.run do |config| > end > > when i invoke the run method, it then do process method which is the > actually initializing method. > > I want to know how can i extend the method without changing > railties/lib/initializer.rb, in other words, how to extend it? > > My requirement is to initialize ActionMailer::Base.smtp_settings. Though > there''s a built-in way to do it, i prefer the way performed as > database.yml, which can put development, production and test environment > all in a file. > > For example: > > development: > server: 127.0.0.1 > port: 25 > domain: localhost > > production: > server: 127.0.0.1 > port: 25 > domain: localhost > > > Recently, I use this way. > > module Rails > class Initializer > def initialize_smtp > if configuration.frameworks.include ?(:action_mailer) && RAILS_ENV > != ''test'' > ActionMailer::Base.smtp_settings = ( > configuration.smtp_configuration[RAILS_ENV] || {}).symbolize_keys > end > end > end > > class Configuration > attr_accessor :smtp_configuration_file > > def smtp_configuration > self.smtp_configuration_file ||= default_smtp_configuration_file > YAML::load(ERB.new(IO.read(smtp_configuration_file)).result) > end > > private > def default_smtp_configuration_file > File.join("#{RAILS_ROOT}", "config", "mailer.yml") > end > end > end > > But following this, I only can initialize the mail system after the > initialization, as: > > initializer = Rails::Initializer.run do |config| > end > initializer.initialize_smtp > > It works, but not perfect really. > > Does this a way to directly do the work when rails initialize itself? > Thank you for any advice. >--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
How about solving it with "after_initialize" callback? http://pastie.caboo.se/95439 --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---