I discovered that you can add these modules under /lib with constants in them and call these constants like so : module AdminConstants email =''admin-J0of1frlU80@public.gmane.org'' phone ="123456789" end and then access them using AdminConstants::email sometimes I can put in a require "adminConstants" in order to access them. I am thinknig of putting them all over the place. Whereever there is any value hardcoded. Can I use this in development.rb? How about if I wanted different set of constants for different environments? like x=1;y=2 in development but x=2,y=3 in production? Can I have the right files load during startup? Is it possible to initialize the constants in the lib modules with values from the database? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Why not add these to a YAML config file? Check out this Railscast http://railscasts.com/episodes/85 Best. Mike On Jun 23, 2008, at 5:21 PM, Ather Shiraz wrote:> > I discovered that you can add these modules under /lib with > constants in > them and call these constants like so : > > module AdminConstants > > email =''admin-J0of1frlU80@public.gmane.org'' > phone ="123456789" > > end > > > and then access them using AdminConstants::email > > sometimes I can put in a require "adminConstants" in order to access > them. I am thinknig of putting them all over the place. Whereever > there > is any value hardcoded. Can I use this in development.rb? > > How about if I wanted different set of constants for different > environments? > > like x=1;y=2 in development but x=2,y=3 in production? > > Can I have the right files load during startup? Is it possible to > initialize the constants in the lib modules with values from the > database? > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ryan Bigg (Radar)
2008-Jun-23 22:12 UTC
Re: How to Have Constants Available Throughout App?
You could put the constants in config/environment.rb: if RAILS_ENV == "development" EMAIL = "admin-J0of1frlU80@public.gmane.org" PASSWORD = "1234567890" else EMAIL = "admin-J0of1frlU80@public.gmane.org" PASSWORD = "0987654321" end And then just access them as EMAIL and PASSWORD in your application. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ather Shiraz wrote:> > How about if I wanted different set of constants for different > environments? > > like x=1;y=2 in development but x=2,y=3 in production? > > Can I have the right files load during startup? Is it possible to > initialize the constants in the lib modules with values from the > database?Here''s what I do: I have a file config/app_config.rb in which I have all of my settings in a hash. It looks like this (formatting might be messed up some): ======================================== =begin NOTE: Changes to these settings will not take effect until the server is bounced. Description of configuration settings admin_email: where all of the administrative emails go load_google_analytics: disable/enable Google Analytics =end # SHARED contains settings that are shared across both environments and are unlikely to # be different. A good example of this is the google_urchin_path. When loading the urchin.js # from google''s server, it would be the same for both environments (whether it should be loaded # in development is another matter). However, if a SHARED value needs to be overridden, just add # the :key => value pair to the appropriate environment. SHARED = { :local_urchin_path => nil, :google_urchin_path => ''http://www.google-analytics.com/urchin.js'', :use_local_urchin => false, :default_time_zone => ''Central Time (US & Canada)'' } CONFIG = { :development => { :admin_email => ''dev_admin_email-oHC15RC7JGQqcZcGjlUOXw@public.gmane.org'', :load_google_analytics => false }, :production => { :admin_email => ''live_admin_email-oHC15RC7JGQqcZcGjlUOXw@public.gmane.org'', :load_google_analytics => true } } # write the specific environment configuration to a single hash that can be # accessed easier in the application. With this, we can do # APP_CONFIG[:admin_email] # instead of # CONFIG[RAILS_ENV.to_sym][:admin_email] APP_CONFIG = SHARED.merge(CONFIG[RAILS_ENV.to_sym]) =========================================== In config/environment.rb, I do require ''app_config'' which means the constant hash APP_CONFIG will be available anywhere I need it. So I can do stuff like class AdminMailer < ActionMailer::Base def contact_us_email(params) @subject = "Contact Us: #{params[:contact_subject].strip}" @recipients = APP_CONFIG[:admin_email] @cc = params[:contact_email].strip if params[:contact_copy_self] @from = "#{params[:contact_name].strip} <#{params[:contact_email].strip}>" @sent_on = TimeZone.new(APP_CONFIG[:default_time_zone]).now @headers = {} @body = params[:contact_body].strip end end This approach works extremely well for me. Peace, Phillip -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---