Best Way to Create Custom Config Options For My Rails App

Best way to create custom config options for my Rails app?

For general application configuration that doesn't need to be stored in a database table, I like to create a config.yml file within the config directory. For your example, it might look like this:

defaults: &defaults
audiocast_uri_format: http://blablalba/blabbitybla/yadda

development:
<<: *defaults

test:
<<: *defaults

production:
<<: *defaults

This configuration file gets loaded from a custom initializer in config/initializers:

# Rails 2
APP_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/config.yml")[RAILS_ENV]

# Rails 3+
APP_CONFIG = YAML.load_file(Rails.root.join('config/config.yml'))[Rails.env]

If you're using Rails 3, ensure you don't accidentally add a leading slash to your relative config path.

You can then retrieve the value using:

uri_format = APP_CONFIG['audiocast_uri_format']

See this Railscast for full details.

How to define custom configuration variables in Rails?

Update 1

Very recommended: I'm going with Rails Config gem nowadays for the fine grained control it provides.

Update2

If you want a quick solution, then check Jack Pratt's answer below.

Although my original answer below still works, this answer is now outdated. I recommend looking at updates 1 and 2.

Original Answer:

For a quick solution, watching the "YAML Configuration File" screen cast by Ryan Bates should be very helpful.

In summary:

# config/initializers/load_config.rb
APP_CONFIG = YAML.load_file("#{Rails.root}/config/config.yml")[Rails.env]

# application.rb
if APP_CONFIG['perform_authentication']
# Do stuff
end

Custom rails configuration section

What you're looking for is ActiveSupport::OrderedOptions.

It is used by rails internally to set config namespace. It allows to do something like this, in config/application.rb :

module MyApp
class Application < Rails::Application
# ...
config.foo = ActiveSupport::OrderedOptions.new
config.foo.bar = :bar
end
end

You can then retrieve it the usual way :

Rails.configuration.foo.bar

If you want this to be environment specific, you also can use it in config/environments/*.rb rather than config/application.rb.

What is the best way to store app specific configuration in rails?

I was helping a friend set up the solution mentioned by Ricardo yesterday. We hacked it a bit by loading the YAML file with something similar to this (going from memory here):

require 'ostruct'
require 'yaml'
require 'erb'
#config = OpenStruct.new(YAML.load_file("#{RAILS_ROOT}/config/config.yml"))
config = OpenStruct.new(YAML.load(ERB.new(File.read("#{RAILS_ROOT}/config/config.yml")).result))
env_config = config.send(RAILS_ENV)
config.common.update(env_config) unless env_config.nil?
::AppConfig = OpenStruct.new(config.common)

This allowed him to embed Ruby code in the config, like in Rhtml:

development:
path_to_something: <%= RAILS_ROOT %>/config/something.yml

Rails 3 Application/User Settings best practice?

As always right after asking for help my search bears fruit. Found Georg Ledermann's fork of rails-settings that looks like it will do just what I need.

How do a config file and generator for my rails gem with yaml

There is nice documentation in Rails guides on how to create a generator, Here is the link

Also Here is a link that illustrates how to create a config file with yaml

Best way to manage Rails application configuration settings persisted in a database?

Not the prettiest solution I'm sure, but one thing we've done for our internal apps is create a 'config' field that is simply a text field containing a serialized hash. Then we deserialize on load from the db.

What's the best way to store global application settings in a Rails application?

For both cases database. You're going to be using the same structures for multiple people/products so it makes sense. Also it allows you to change things without restarting the server.

I've handled it this way in the past:
For settings specific to the user, I've created a UserSettings model/table, that has a one-to-one to relationship with a user. The reasoning for this is that the majority of my operations involving users do no not require these settings to be loaded, so they're only included on user loads from the database when I need them.

When I do this, I'll usually group my column names, so that I can write helpers that dynamically create based on the names. Meaning that I won't have to modify my views to incorporate new settings unless I add one with a different naming scheme.

For the settings specific to a product, well that depends on how you are doing things. And there are a couple of ways to interpret your question.

The way I read it is that you want to decide on a product level. What settings users can overriding or disabling a user's setting. And possibly define some product specific settings.

I would use a one-to-many product to setting relationship. The setting table would be something simplistic (product_id, setting_name, setting_default_value, allow_user_change)

This does a number of things. It lets you have a variable list of settings for different products (Perfect for the case where you're offering many different products instead of varying tiers of access to services). It also lets you define what settings a user can/can't change and give values for that product type. That can be changed from an administrator view without restarting the application. It's also not tied to user settings, to the point where if a user doesn't have a setting listed in the product_settings there will be no problems.

The downside is you will have multiple common settings in this table. I would move settings that every product will have a different value to a field in the product table.

You will also have to write validations to ensure that a user does not change a setting their product says they can't. You will also have to write helper methods to merge settings from the product and user sides.

how to create custom data config to load services host in rails?

Create the file you need in your config directory, for example my_config.yml and populate it with the options you require per environment:

development:
debug_enabled: true

test:
debug_enabled: false

production:
debug_enabled: false

Then create a file called load_config.rb in your config/initializers directory with this in it:

MY_CONFIG = YAML.load_file("#{Rails.root}/config/my_config.yml")[Rails.env]

Then you can use those settings in your applications like so:

if MY_CONFIG[:debug_enabled]
# ... do something special ...
end


Related Topics



Leave a reply



Submit