scott
August 3, 2006, 4:53pm
#1
Hi,
I’m looking for a way to store common settings, such as the
application_name or email address from value in a file.
I then want to be able to call these settings in my controllers and
models?
Is there any easy way of doing this? and where would you recommend I
save the file ?
Thanks
scott
scott
August 3, 2006, 5:43pm
#2
Put the file in the lib directory and then just require it where you
need it. You could also make it a Module which may be loaded for you
and then you can just include it.
lib/application-info.rb
module ApplicationInfo
attr_reader :application_name, :application_email
@application_name = “My Cool App”
@application_email = “[email protected] ”
end
class SomethingController < AC::Base
include ApplicationInfo
# now your actions and views have access to
# @application_name and @application_email
end
scott
August 4, 2006, 12:42am
#4
Hi Scott,
I would go for something much simpler, in environnment.rb:
CONFIG = {
:email => ‘[email protected] ’,
}
Then, calling from everywhere in the app: CONFIG[:email]
Jean-Etienne
scott
August 4, 2006, 4:07am
#5
I just stick my common settings into environment.rb, such as:
SUPPORT_EMAIL = “[email protected] ”
then I can use “mail_to(SUPPORT_EMAIL)” inside my app
Mike
scott
August 4, 2006, 2:56am
#6
There are numerous plugins for this. Naively I wrote this code that I
put in
/lib. You can get to it from anywhere and it provides a straightforward
array-like interface. E.g.,
Options[:email_from] = ‘[email protected] ’
Options[:smtp_server] = ‘smtp.mydomain.com ’
HTH
Provide a container for global option storage
Both getter/setter and [] access methods are
provided.
class Options
@@option_hash = {}
def self.set(option_name, value)
option_name = option_name.to_s
@@option_hash [option_name] = value
end
def self.get(option_name)
option_name = option_name.to_s
@@option_hash [option_name]
end
def self.[]=(option_name, value)
option_name = option_name.to_s
self.set(option_name, value)
end
def self.
option_name = option_name.to_s
self.get(option_name)
end
def self.has_key?(key)
key = key.to_s
@@option_hash.has_key ?(key)
end
def self.key?(key)
self.has_key?(key)
end
end
View this message in context:
http://www.nabble.com/Store-common-settings-in-one-file--tf2046127.html#a5643220
Sent from the RubyOnRails Users forum at Nabble.com .