Coder Social home page Coder Social logo

rubyconfig / config Goto Github PK

View Code? Open in Web Editor NEW
2.1K 31.0 231.0 657 KB

Easiest way to add multi-environment yaml settings to Rails, Sinatra, Padrino and other Ruby projects.

License: Other

Ruby 88.95% JavaScript 0.65% CSS 1.06% HTML 8.34% Dockerfile 0.92% Shell 0.08%
rails ruby sinatra padrino config configuration configuration-management environment-variables environments ruby-on-rails

config's Introduction

Config

Version Downloads Total Tests Financial Contributors on Open Collective

Summary

Config helps you easily manage environment specific settings in an easy and usable manner.

Features

  • simple YAML config files
  • config files support ERB
  • config files support inheritance and multiple environments
  • access config information via convenient object member notation
  • support for multi-level settings (Settings.group.subgroup.setting)
  • local developer settings ignored when committing the code

Compatibility

Current version supports and is tested for the following interpreters and frameworks:

  • Interpreters
  • Application frameworks
    • Rails >= 5.2
    • Padrino
    • Sinatra

For Ruby 2.0 to 2.3 or Rails 3 to 4.1 use version 1.x of this gem. For older versions of Rails or Ruby use AppConfig.

For Ruby 2.4 or 2.5 or Rails 4.2, 5.0, or 5.1 use version 3.x of this gem.

Installing

Installing on Rails

Add gem 'config' to your Gemfile and run bundle install to install it. Then run

rails g config:install

which will generate customizable config file config/initializers/config.rb and set of default settings files:

config/settings.yml
config/settings.local.yml
config/settings/development.yml
config/settings/production.yml
config/settings/test.yml

You can now edit them to adjust to your needs.

Note: By default, the config environment will match the Rails environment (Rails.env). This can be changed by setting config.environment.

Installing on Padrino

Add the gem to your Gemfile and run bundle install to install it. Then edit app.rb and register Config

register Config

Installing on Sinatra

Add the gem to your Gemfile and run bundle install to install it. Afterwards in need to register Config in your app and give it a root so it can find the config files.

set :root, File.dirname(__FILE__)
register Config

Installing on other ruby projects

Add the gem to your Gemfile and run bundle install to install it. Then initialize Config manually within your configure block.

Config.load_and_set_settings(Config.setting_files("/path/to/config_root", "your_project_environment"))

It's also possible to initialize Config manually within your configure block if you want to just give it some yml paths to load from.

Config.load_and_set_settings("/path/to/yaml1", "/path/to/yaml2", ...)

Accessing the Settings object

After installing the gem, Settings object will become available globally and by default will be compiled from the files listed below. Settings defined in files that are lower in the list override settings higher.

config/settings.yml
config/settings/#{environment}.yml
config/environments/#{environment}.yml

config/settings.local.yml
config/settings/#{environment}.local.yml
config/environments/#{environment}.local.yml

Entries can be accessed via object member notation:

Settings.my_config_entry

Nested entries are supported:

Settings.my_section.some_entry

Alternatively, you can also use the [] operator if you don't know which exact setting you need to access ahead of time.

# All the following are equivalent to Settings.my_section.some_entry
Settings.my_section[:some_entry]
Settings.my_section['some_entry']
Settings[:my_section][:some_entry]

Reloading settings

You can reload the Settings object at any time by running Settings.reload!.

Reloading settings and config files

You can also reload the Settings object from different config files at runtime.

For example, in your tests if you want to test the production settings, you can:

Rails.env = "production"
Settings.reload_from_files(
  Rails.root.join("config", "settings.yml").to_s,
  Rails.root.join("config", "settings", "#{Rails.env}.yml").to_s,
  Rails.root.join("config", "environments", "#{Rails.env}.yml").to_s
)

Environment specific config files

You can have environment specific config files. Environment specific config entries take precedence over common config entries.

Example development environment config file:

#{Rails.root}/config/environments/development.yml

Example production environment config file:

#{Rails.root}/config/environments/production.yml

Developer specific config files

If you want to have local settings, specific to your machine or development environment, you can use the following files, which are automatically .gitignore :

Rails.root.join("config", "settings.local.yml").to_s,
Rails.root.join("config", "settings", "#{Rails.env}.local.yml").to_s,
Rails.root.join("config", "environments", "#{Rails.env}.local.yml").to_s

NOTE: The file settings.local.yml will not be loaded in tests to prevent local configuration from causing flaky or non-deterministic tests. Environment-specific files (e.g. settings/test.local.yml) will still be loaded to allow test-specific credentials.

Adding sources at runtime

You can add new YAML config files at runtime. Just use:

Settings.add_source!("/path/to/source.yml")
Settings.reload!

This will use the given source.yml file and use its settings to overwrite any previous ones.

On the other hand, you can prepend a YML file to the list of configuration files:

Settings.prepend_source!("/path/to/source.yml")
Settings.reload!

This will do the same as add_source, but the given YML file will be loaded first (instead of last) and its settings will be overwritten by any other configuration file. This is especially useful if you want to define defaults.

One thing I like to do for my Rails projects is provide a local.yml config file that is .gitignored (so its independent per developer). Then I create a new initializer in config/initializers/add_local_config.rb with the contents

Settings.add_source!("#{Rails.root}/config/settings/local.yml")
Settings.reload!

Note: this is an example usage, it is easier to just use the default local files settings.local.yml, settings/#{Rails.env}.local.yml and environments/#{Rails.env}.local.yml for your developer specific settings.

You also have the option to add a raw hash as a source. One use case might be storing settings in the database or in environment variables that overwrite what is in the YML files.

Settings.add_source!({some_secret: ENV['some_secret']})
Settings.reload!

You may pass a hash to prepend_source! as well.

Embedded Ruby (ERB)

Embedded Ruby is allowed in the YAML configuration files. ERB will be evaluated at load time by default, and when the evaluate_erb_in_yaml configuration is set to true.

Consider the two following config files.

  • #{Rails.root}/config/settings.yml
size: 1
server: google.com
  • #{Rails.root}/config/environments/development.yml
size: 2
computed: <%= 1 + 2 + 3 %>
section:
  size: 3
  servers: [ {name: yahoo.com}, {name: amazon.com} ]

Notice that the environment specific config entries overwrite the common entries.

Settings.size   # => 2
Settings.server # => google.com

Notice the embedded Ruby.

Settings.computed # => 6

Notice that object member notation is maintained even in nested entries.

Settings.section.size # => 3

Notice array notation and object member notation is maintained.

Settings.section.servers[0].name # => yahoo.com
Settings.section.servers[1].name # => amazon.com

Configuration

There are multiple configuration options available, however you can customize Config only once, preferably during application initialization phase:

Config.setup do |config|
  config.const_name = 'Settings'
  # ...
end

After installing Config in Rails, you will find automatically generated file that contains default configuration located at config/initializers/config.rb.

General

  • const_name - name of the object holding your settings. Default: 'Settings'
  • evaluate_erb_in_yaml - evaluate ERB in YAML config files. Set to false if the config file contains ERB that should not be evaluated at load time. Default: true
  • file_name - name of the file to store general keys accessible in all environments. Default: 'settings' - located at config/settings.yml
  • dir_name - name of the directory to store environment-specific files. Default: 'settings' - located at config/settings/

Merge customization

  • overwrite_arrays - overwrite arrays found in previously loaded settings file. Default: true
  • merge_hash_arrays - merge hashes inside of arrays from previously loaded settings files. Makes sense only when overwrite_arrays = false. Default: false
  • knockout_prefix - ability to remove elements of the array set in earlier loaded settings file. Makes sense only when overwrite_arrays = false, otherwise array settings would be overwritten by default. Default: nil
  • merge_nil_values - nil values will overwrite an existing value when merging configs. Default: true.
# merge_nil_values is true by default
c = Config.load_files("./spec/fixtures/development.yml") # => #<Config::Options size=2, ...>
c.size # => 2
c.merge!(size: nil) => #<Config::Options size=nil, ...>
c.size # => nil
# To reject nil values when merging settings:
Config.setup do |config|
  config.merge_nil_values = false
end

c = Config.load_files("./spec/fixtures/development.yml") # => #<Config::Options size=2, ...>
c.size # => 2
c.merge!(size: nil) => #<Config::Options size=nil, ...>
c.size # => 2

Check Deep Merge for more details.

Validation

With Ruby 2.1 or newer, you can optionally define a schema or contract (added in config-2.1) using dry-rb to validate presence (and type) of specific config values. Generally speaking contracts allow to describe more complex validations with depencecies between fields.

If you provide either validation option (or both) it will automatically be used to validate your config. If validation fails it will raise a Config::Validation::Error containing information about all the mismatches between the schema and your config.

Both examples below demonstrates how to ensure that the configuration has an optional email and the youtube structure with the api_key field filled. The contract adds an additional rule.

Contract

Leverage dry-validation, you can create a contract with a params schema and rules:

class ConfigContract < Dry::Validation::Contract
  params do
    optional(:email).maybe(:str?)

    required(:youtube).schema do
      required(:api_key).filled
    end
  end

  rule(:email) do
    unless /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i.match?(value)
      key.failure('has invalid format')
    end
  end
end

Config.setup do |config|
  config.validation_contract = ConfigContract.new
end

The above example adds a rule to ensure the email is valid by matching it against the provided regular expression.

Check dry-validation for more details.

Schema

You may also specify a schema using dry-schema:

Config.setup do |config|
  # ...
  config.schema do
    optional(:email).maybe(:str?)

    required(:youtube).schema do
      required(:api_key).filled
    end
  end
end

Check dry-schema for more details.

Missing keys

For an example settings file:

size: 1
server: google.com

You can test if a value was set for a given key using key? and its alias has_key?:

Settings.key?(:path)
# => false
Settings.key?(:server)
# => true

By default, accessing to a missing key returns nil:

Settings.key?(:path)
# => false
Settings.path
# => nil

This is not "typo-safe". To solve this problem, you can configure the fail_on_missing option:

Config.setup do |config|
  config.fail_on_missing = true
  # ...
end

So it will raise a KeyError when accessing a non-existing key (similar to Hash#fetch behaviour):

Settings.path
# => raises KeyError: key not found: :path

Environment variables

See section below for more details.

Working with environment variables

To load environment variables from the ENV object, that will override any settings defined in files, set the use_env to true in your config/initializers/config.rb file:

Config.setup do |config|
  config.const_name = 'Settings'
  config.use_env = true
end

Now config would read values from the ENV object to the settings. For the example above it would look for keys starting with Settings:

ENV['Settings.section.size'] = 1
ENV['Settings.section.server'] = 'google.com'

It won't work with arrays, though.

It is considered an error to use environment variables to simultaneously assign a "flat" value and a multi-level value to a key.

# Raises an error when settings are loaded
ENV['BACKEND_DATABASE'] = 'development'
ENV['BACKEND_DATABASE_USER'] = 'postgres'

Instead, specify keys of equal depth in the environment variable names:

ENV['BACKEND_DATABASE_NAME'] = 'development'
ENV['BACKEND_DATABASE_USER'] = 'postgres'

Working with Heroku

Heroku uses ENV object to store sensitive settings. You cannot upload such files to Heroku because it's ephemeral filesystem gets recreated from the git sources on each instance refresh. To use config with Heroku just set the use_env var to true as mentioned above.

To upload your local values to Heroku you could ran bundle exec rake config:heroku.

Fine-tuning

You can customize how environment variables are processed:

  • env_prefix (default: const_name) - load only ENV variables starting with this prefix (case-sensitive)
  • env_separator (default: '.') - what string to use as level separator - default value of . works well with Heroku, but you might want to change it for example for __ to easy override settings from command line, where using dots in variable names might not be allowed (eg. Bash)
  • env_converter (default: :downcase) - how to process variables names:
    • nil - no change
    • :downcase - convert to lower case
  • env_parse_values (default: true) - try to parse values to a correct type (Boolean, Integer, Float, String)

For instance, given the following environment:

SETTINGS__SECTION__SERVER_SIZE=1
SETTINGS__SECTION__SERVER=google.com
SETTINGS__SECTION__SSL_ENABLED=false

And the following configuration:

Config.setup do |config|
  config.use_env = true
  config.env_prefix = 'SETTINGS'
  config.env_separator = '__'
  config.env_converter = :downcase
  config.env_parse_values = true
end

The following settings will be available:

Settings.section.server_size # => 1
Settings.section.server # => 'google.com'
Settings.section.ssl_enabled # => false

Working with AWS Secrets Manager

It is possible to parse variables stored in an AWS Secrets Manager Secret as if they were environment variables by using Config::Sources::EnvSource.

For example, the plaintext secret might look like this:

{
  "Settings.foo": "hello",
  "Settings.bar": "world",
}

In order to load those settings, fetch the settings from AWS Secrets Manager, parse the plaintext as JSON, pass the resulting Hash into a new EnvSource, load the new source, and reload.

# fetch secrets from AWS
client = Aws::SecretsManager::Client.new
response = client.get_secret_value(secret_id: "#{ENV['ENVIRONMENT']}/my_application")
secrets = JSON.parse(response.secret_string)

# load secrets into config
secret_source = Config::Sources::EnvSource.new(secrets)
Settings.add_source!(secret_source)
Settings.reload!

In this case, the following settings will be available:

Settings.foo # => "hello"
Settings.bar # => "world"

By default, EnvSource will use configuration for env_prefix, env_separator, env_converter, and env_parse_values, but any of these can be overridden in the constructor.

secret_source = Config::Sources::EnvSource.new(secrets,
                                               prefix: 'MyConfig',
                                               separator: '__',
                                               converter: nil,
                                               parse_values: false)

Contributing

You are very warmly welcome to help. Please follow our contribution guidelines

Any and all contributions offered in any form, past present or future are understood to be in complete agreement and acceptance with MIT license.

Running specs

Setup

bundle install
bundle exec appraisal install

List defined appraisals:

bundle exec appraisal list

Run specs for specific appraisal:

bundle exec appraisal rails-6.1 rspec

Run specs for all appraisals:

bundle exec appraisal rspec

Authors

Contributors

Code Contributors

This project exists thanks to all the people who contribute and you are very warmly welcome to help. Please follow our contribution guidelines.

Any and all contributions offered in any form, past present or future are understood to be in complete agreement and acceptance with the MIT license.

Contributors

Financial Contributors

Become a backer and support us with a small monthly donation to help us continue our activities. Thank you if you already one! 🙏

Backers

Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website.

Sponsors

License

Copyright (C) Piotr Kuczynski. Released under the MIT License.

config's People

Contributors

albertosaurus avatar amatsuda avatar atatb avatar betamatt avatar cjlarose avatar cryo28 avatar eksoverzero avatar eugenk avatar fredwu avatar hallelujah avatar hanachin avatar inouetakuya avatar jcnetdev avatar jrafanie avatar m-nakamura145 avatar masterkain avatar merbjedi avatar neilwilliams avatar ojab avatar pkuczynski avatar qnighy avatar rdodson41 avatar rdubya avatar robertcigan avatar seikichi avatar slicedpan avatar soartec-lab avatar spalladino avatar y-yagi avatar ytkg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

config's Issues

Option overwriting doesn't work (for me, at least)

Hiya,

I'm using an optional YML file with additional options as well as changes to the existing ones, which is loaded if it exists (doing so via Settings.add_source!("#{Rails.root}/config/lpl.yml") in my app's initializer). The problem is that I have the following defined in settings.yml:

lpl: 
  ..
  language_options:
    - de
    - en
  ..

while my own file has

lpl: 
  ..
  language_options:
    - de
  ..

I was expecting the result to be that Settings.lpl.language_options would hold ["de"], yet it still holds ["de", "en"]. Do I need to do anything besides

if File.exists?("#{Rails.root}/config/lpl.yml")
  Settings.add_source!("#{Rails.root}/config/lpl.yml")
  Settings.reload!
end

when loading the additional settings in my initlializer? Or should I be unloading all settings and include all the settings in lpl.yml, not just the ones that would add to/overwrite stuff from config.yml? And if so—how?

Thanks! :)

Settings not defined before rake db:test:prepepare

If I try to run rake db:test:prepare I get an error (ran it with --trace):

** Invoke db:test:prepare (first_time)
** Invoke db:load_config (first_time)
** Execute db:load_config
rake aborted!
uninitialized constant Settings
(erb):9:in `<main>'
/Users/jasperkennis/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/erb.rb:849:in `eval'
/Users/jasperkennis/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/erb.rb:849:in `result'
/Users/jasperkennis/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/application/configuration.rb:106:in `database_configuration'
/Users/jasperkennis/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/railtie.rb:46:in `block (3 levels) in <class:Railtie>'
/Users/jasperkennis/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:236:in `call'
/Users/jasperkennis/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:236:in `block in execute'
/Users/jasperkennis/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:231:in `each'
/Users/jasperkennis/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:231:in `execute'
/Users/jasperkennis/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:175:in `block in invoke_with_call_chain'
/Users/jasperkennis/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/Users/jasperkennis/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:168:in `invoke_with_call_chain'
/Users/jasperkennis/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:197:in `block in invoke_prerequisites'
/Users/jasperkennis/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:195:in `each'
/Users/jasperkennis/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:195:in `invoke_prerequisites'
/Users/jasperkennis/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:174:in `block in invoke_with_call_chain'
/Users/jasperkennis/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/Users/jasperkennis/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:168:in `invoke_with_call_chain'
/Users/jasperkennis/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:161:in `invoke'
/Users/jasperkennis/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:149:in `invoke_task'
/Users/jasperkennis/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/Users/jasperkennis/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:106:in `each'
/Users/jasperkennis/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:106:in `block in top_level'
/Users/jasperkennis/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:115:in `run_with_threads'
/Users/jasperkennis/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:100:in `top_level'
/Users/jasperkennis/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:78:in `block in run'
/Users/jasperkennis/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:165:in `standard_exception_handling'
/Users/jasperkennis/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:75:in `run'
/Users/jasperkennis/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rake-10.1.0/bin/rake:33:in `<top (required)>'
/Users/jasperkennis/.rbenv/versions/2.0.0-p247/bin/rake:23:in `load'
/Users/jasperkennis/.rbenv/versions/2.0.0-p247/bin/rake:23:in `<main>'
Tasks: TOP => db:test:prepare => db:load_config

The problem goes away when I hardcode my db configurations. Breaking database.yml looks like this:

development:
  host: localhost
  adapter: postgresql
  encoding: utf8
  database: <%= Settings.databases.development.database %>
  pool: 5
  username: <%= Settings.databases.development.user %>
  password:

test:
  adapter: postgresql
  encoding: utf8
  database: <%= Settings.databases.test.database %>
  pool: 5
  username: <%= Settings.databases.development.user %>
  password:

I'm om Rails 4, ruby 2.0.0p247, and rails_config 0.3.3. Don't really know why this is happening, I haven't noticed similar problems with any other tasks. Can provide more info if needed.

Multi-environment settings not loading properly

In config/settings.yml:

foo: bar

In config/settings/development.yml:

foo: bar_dev

In config/settings/test.yml:

foo: bar_test

When i start the rails console in the development environment, i.e. rails console, everything works as expected. The value of Settings.foo equals bar_dev.

However, if I start the rails console in the test environment, i.e. rails console -e test, the value of Settings.foo still equals bar_dev. I would expect it to equal bar_test.

I even tried Settings.reload!, but the value remains bar_dev when in the test environment.

Is this not working as expected, or am I missing something?

Missing keys should raise an error

This really seemed like a perfect gem for me. I extactly wanted specification for different environments in separate files.

However, there is one major design flaw. Accessing an option that wasn't set in the YAML files should raise a NoMethodError. That is, RailsConfig::Options shouldn't be an OpenStruct. Otherwise there is no way to guard yourself from typos, since there is no way to determine whether you made one. And that can cause really hard-to-find bugs (e.g. boolean value like Settings.use_secure_sever).

Using rails_config in config/environments/*.rb

Hi!

I know, there was some issues like that before, but it looks that it happen again. Works for me with 0.2.5, broken above. Ruby 1.9.2, rails 3.2.2

/path/to/project/config/environments/development.rb:33:in `block in <top (required)>': uninitialized constant Settings (NameError)

OH GOD CHANGE THE NAME!

Settings is the name of the constant yet the gem name is rails_config? Ahhh, this hurts so much!

ps: I don't expect this issue to be taken seriously, as it's mainly a complaint of seeing one thing (Settings) and not knowing where it originates (rails_config).

Settings and application.rb

Is there any reason why Settings can be used in development.rb/production.rb, but can't be used in applicaiton.rb? I need to set some config the same way for all environments and wouldn't like to duplicate it for each of the environment files.

Crashes when loading in a Sinatra app

When using in a Sinatra app, RailsConfig crashes because module Rails is not defined:

rails_config-0.4.1/lib/rails_config/engine.rb:2:in `<module:RailsConfig>': uninitialized constant Rails (NameError)

Suggest to only include engine.rb when Rails is defined.

When did a missing setting started to return nil ?

Hi,

I hope this issue's title doesn't seem aggressive, anyway it's not the intention ;-)

For a long time, I've been checking for particular setting like this :

affiliate_id = begin
  Settings.tracking.affiliate_id.send(options[:aid])
rescue NoMethodError, TypeError, ArgumentError => ex
  Settings.tracking.affiliate_id.default
end

Today — I don't think my version has changed (0.2.x) — I get nil for a missing setting, instead of a NoMethodError exception.

I was surprised not to find any test case for this in the current 0.3.1. I even wanted to add one, but i've had issues with Ruby 1.8 compatibility (hence my pull request and my #42 issue).

Am I crazy and it has always been that way?
Am I using it wrong?

Anyway, thanks for your help and for making/maintaining this great gem.

to_hash method doesn't handle YAML arrays of key/value pairs

If the settings.yml file contains an array of key/value pairs then the to_hash method skips over those pairs and fails to convert them even though it works for simple arrays.

For example, if the settings.yml contains,

media:
  types:
   - Video
   - Image
   - Website URL
   - Document

  metadata:
   - name: Field 1
     type: string
     required: true
   - name: Field 2
     type: tag
     required: false
   - name: Field 3
     type: text
   - name: Field 4

then calling Settings.media.to_hash results in,

{:types=>["Video", "Image", "Website URL", "Document"], :metadata=>[#<RailsConfig::Options name="Field 1", type="string", required=true>, #<RailsConfig::Options name="Field 2", type="tag", required=false>, #<RailsConfig::Options name="Field 3", type="text">, #<RailsConfig::Options name="Field 4">]}

The correct output should probably be,

{:types=>["Video", "Image", "Website URL", "Document"], :metadata=>[{:name=>"Field 1", :type=>"string", :required=>true}, {:name=>"Field 2", :type=>"tag", :required=>false}, {:name=>"Field 3", :type=>"text"}, {:name=>"Field 4"}]}

Either I'm misunderstanding config.const_name, or it's not working (for me at least)

Hi!

I just switched to RailsConfig from SettingsLogic and I really like it, apart from a small issue, that might well be a misunderstanding from my side, namely,

in config/initializers/rails_config.rb, I had the following code:

RailsConfig.setup do |config|
  config.const_name = "LplSettings"
end

—since my SettingsLogic class was named LplSettings and I thought that this would make the constant I'm accessing my settings trough named the same and thus make my transition easier. Am I right in assuming that the above code would made my settings accessible via LplSettings instead of just Settings? Settings works and I managed to do a search & replace that didn't break anything. But I'm still wondering if it's working as designed or not. I even renamed the RailsConfig initalizer with a "01_" prefix to make sure it is initialized first, to no avail.

Thanks for the work on RailsConfig, have a nice day/evening.

Samo

Going 1.0

Hi,

RailsConfig has been quite stale for many month : only a handful of commits during the last year, almost no issue closed, …

Maybe it's a good time to evaluate the most important open issues and try to solve them then release a 1.0 version.

A piece of software, used by many in production for a long time, can't really stay in 0.x forever.
Plus, it might never be perfectly bug free and feature complete, but the life of project doesn't stop at 1.0.

FInally, according to SemVer semantics, marking a 1.0 release is a strong indication that the public API is stable (until 2.0) and is trusted to be used in production.

What do you think about that

Rails 4.1.0 configuration error

I have

Exchange24::Application.configure do
  config.action_mailer.default_url_options = { host: Settings.mail.host }
end

I start the server

block in <top (required)>': uninitialized constant Settings (NameError)

Was working fine in Rails 4.0.4

Any idea how to fix ?

How to test config value in production evn?

config/settings/productions.yml
authentication:
password_expired_after: <%= 1.month %>
use_validate_code: true

after(:each) do
Rails.env = 'test'
end

it "should not use validate code at loign page during test env" do
Settings.authentication.use_validate_code.should == false
end

it "should not use validate code at loign page during development env" do
Rails.env = 'development'
Rails.env.should == 'development'
Settings.authentication.use_validate_code.should == false
end

it "should use validate code at loign page during production env" do
Rails.env = 'production'
Rails.env.should == 'production'
Settings.reload!
Settings.authentication.use_validate_code.should == true
end

The spec test can't passed:

Failures:

  1. #<RailsConfig::Options authentication=#<RailsConfig::Options password_expired_after=2592000, use_validate_code=false>> should use validate code at loign page during production env
    Failure/Error: Settings.authentication.use_validate_code.should == true
    expected: true,
    got: false (using ==)

    ./spec/models/settings_spec.rb:49:in `block (2 levels) in <top (required)>'

Prevents loading env. / crashes

I added a rails_config gem into Gemfile, bundle install, wanted to run rails_config generator and it crashed. It also crashes when rails c or rails s with the same error.

Ruby 1.9.2 p290, rails 3.0.14 (apparently 1.9.2 and later does not have Fixnum#to_sym, but not sure what's the problem anyway)

/Users/robert/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/ostruct.rb:81:in new_ostruct_member': undefined methodto_sym' for 1:Fixnum (NoMethodError)
from /Users/robert/.rvm/gems/ruby-1.9.2-p290@sport_centrum/bundler/gems/rails_config-1d0073df191e/lib/rails_config/options.rb:80:in block in __convert' from /Users/robert/.rvm/gems/ruby-1.9.2-p290@sport_centrum/bundler/gems/rails_config-1d0073df191e/lib/rails_config/options.rb:79:ineach'
from /Users/robert/.rvm/gems/ruby-1.9.2-p290@sport_centrum/bundler/gems/rails_config-1d0073df191e/lib/rails_config/options.rb:79:in __convert' from /Users/robert/.rvm/gems/ruby-1.9.2-p290@sport_centrum/bundler/gems/rails_config-1d0073df191e/lib/rails_config/options.rb:83:inblock in __convert'
from /Users/robert/.rvm/gems/ruby-1.9.2-p290@sport_centrum/bundler/gems/rails_config-1d0073df191e/lib/rails_config/options.rb:79:in each' from /Users/robert/.rvm/gems/ruby-1.9.2-p290@sport_centrum/bundler/gems/rails_config-1d0073df191e/lib/rails_config/options.rb:79:in__convert'
from /Users/robert/.rvm/gems/ruby-1.9.2-p290@sport_centrum/bundler/gems/rails_config-1d0073df191e/lib/rails_config/options.rb:83:in block in __convert' from /Users/robert/.rvm/gems/ruby-1.9.2-p290@sport_centrum/bundler/gems/rails_config-1d0073df191e/lib/rails_config/options.rb:79:ineach'
from /Users/robert/.rvm/gems/ruby-1.9.2-p290@sport_centrum/bundler/gems/rails_config-1d0073df191e/lib/rails_config/options.rb:79:in __convert' from /Users/robert/.rvm/gems/ruby-1.9.2-p290@sport_centrum/bundler/gems/rails_config-1d0073df191e/lib/rails_config/options.rb:31:inreload!'
from /Users/robert/.rvm/gems/ruby-1.9.2-p290@sport_centrum/bundler/gems/rails_config-1d0073df191e/lib/rails_config.rb:30:in load_files' from /Users/robert/.rvm/gems/ruby-1.9.2-p290@sport_centrum/bundler/gems/rails_config-1d0073df191e/lib/rails_config.rb:37:inload_and_set_settings'
from /Users/robert/.rvm/gems/ruby-1.9.2-p290@sport_centrum/bundler/gems/rails_config-1d0073df191e/lib/rails_config/integration/rails.rb:15:in block in <class:Railtie>' from /Users/robert/.rvm/gems/ruby-1.9.2-p290@sport_centrum/gems/railties-3.0.14/lib/rails/initializable.rb:25:ininstance_exec'
from /Users/robert/.rvm/gems/ruby-1.9.2-p290@sport_centrum/gems/railties-3.0.14/lib/rails/initializable.rb:25:in run' from /Users/robert/.rvm/gems/ruby-1.9.2-p290@sport_centrum/gems/railties-3.0.14/lib/rails/initializable.rb:50:inblock in run_initializers'
from /Users/robert/.rvm/gems/ruby-1.9.2-p290@sport_centrum/gems/railties-3.0.14/lib/rails/initializable.rb:49:in each' from /Users/robert/.rvm/gems/ruby-1.9.2-p290@sport_centrum/gems/railties-3.0.14/lib/rails/initializable.rb:49:inrun_initializers'
from /Users/robert/.rvm/gems/ruby-1.9.2-p290@sport_centrum/gems/railties-3.0.14/lib/rails/application.rb:134:in initialize!' from /Users/robert/.rvm/gems/ruby-1.9.2-p290@sport_centrum/gems/railties-3.0.14/lib/rails/application.rb:77:inmethod_missing'
from /Users/robert/projects/other/sport_centrum/config/environment.rb:5:in <top (required)>' from /Users/robert/.rvm/gems/ruby-1.9.2-p290@sport_centrum/gems/activesupport-3.0.14/lib/active_support/dependencies.rb:242:inrequire'
from /Users/robert/.rvm/gems/ruby-1.9.2-p290@sport_centrum/gems/activesupport-3.0.14/lib/active_support/dependencies.rb:242:in block in require' from /Users/robert/.rvm/gems/ruby-1.9.2-p290@sport_centrum/gems/activesupport-3.0.14/lib/active_support/dependencies.rb:225:inblock in load_dependency'
from /Users/robert/.rvm/gems/ruby-1.9.2-p290@sport_centrum/gems/activesupport-3.0.14/lib/active_support/dependencies.rb:597:in new_constants_in' from /Users/robert/.rvm/gems/ruby-1.9.2-p290@sport_centrum/gems/activesupport-3.0.14/lib/active_support/dependencies.rb:225:inload_dependency'
from /Users/robert/.rvm/gems/ruby-1.9.2-p290@sport_centrum/gems/activesupport-3.0.14/lib/active_support/dependencies.rb:242:in require' from /Users/robert/projects/other/sport_centrum/config.ru:3:inblock in

'
from /Users/robert/.rvm/gems/ruby-1.9.2-p290@sport_centrum/gems/rack-1.2.5/lib/rack/builder.rb:46:in instance_eval' from /Users/robert/.rvm/gems/ruby-1.9.2-p290@sport_centrum/gems/rack-1.2.5/lib/rack/builder.rb:46:ininitialize'
from /Users/robert/projects/other/sport_centrum/config.ru:1:in new' from /Users/robert/projects/other/sport_centrum/config.ru:1:in'
from /Users/robert/.rvm/gems/ruby-1.9.2-p290@sport_centrum/gems/rack-1.2.5/lib/rack/builder.rb:35:in eval' from /Users/robert/.rvm/gems/ruby-1.9.2-p290@sport_centrum/gems/rack-1.2.5/lib/rack/builder.rb:35:inparse_file'
from /Users/robert/.rvm/gems/ruby-1.9.2-p290@sport_centrum/gems/rack-1.2.5/lib/rack/server.rb:162:in app' from /Users/robert/.rvm/gems/ruby-1.9.2-p290@sport_centrum/gems/rack-1.2.5/lib/rack/server.rb:253:inwrapped_app'
from /Users/robert/.rvm/gems/ruby-1.9.2-p290@sport_centrum/gems/rack-1.2.5/lib/rack/server.rb:204:in start' from /Users/robert/.rvm/gems/ruby-1.9.2-p290@sport_centrum/gems/railties-3.0.14/lib/rails/commands/server.rb:65:instart'
from /Users/robert/.rvm/gems/ruby-1.9.2-p290@sport_centrum/gems/railties-3.0.14/lib/rails/commands.rb:30:in block in <top (required)>' from /Users/robert/.rvm/gems/ruby-1.9.2-p290@sport_centrum/gems/railties-3.0.14/lib/rails/commands.rb:27:intap'
from /Users/robert/.rvm/gems/ruby-1.9.2-p290@sport_centrum/gems/railties-3.0.14/lib/rails/commands.rb:27:in <top (required)>' from script/rails:6:inrequire'
from script/rails:6:in `'

uninitialized constant Settings

Apparently, this code here does not work on Rails 3.1

At rails_config/integration/rails.rb

      if Rails.env.development?
        initializer :rails_config_reload_on_development do
          ActionController::Base.class_eval do
            prepend_before_filter { ::RailsConfig.const_name.constantize.reload! }
          end
        end
      end

Iterating through Settings

I'm a new on the whole rails scene so this might be a really newbie question, but I don't seem to be able to iterate through my Settings. I'm trying to iterate through Settings.navigation which could have another array under them.

With

<%= debug Settings.navigation %>

I get

--- !ruby/object:RailsConfig::Options
table:
  :Book: !ruby/object:RailsConfig::Options
    table:
      :Add: TestBookAdd
      :List: TestBookList
    modifiable: true
  :Users: !ruby/object:RailsConfig::Options
    table:
      :Add: TestUsersAdd
      :View: TestUsersView
    modifiable: true
modifiable: true

I'm trying to each them, but

<% debug Settings.navigation.each do |n| %>
<%= debug c %>
<% end %>

gives me nothing. I actually did find a way to iterate them, by first converting Settings.navigation to JSON and then parse that, but this seem so silly that there has the be a better way :) so:

<% JSON.parse(Settings.navigation.to_json).each do |n| %>
  <%= debug n %>
<% end %>

gives me what I was expecting


---
- Book
- Add: TestBookAdd
  List: TestBookList

---
- Users
- Add: TestUsersAdd
  View: TestUsersView

So is there a better way to iterate through nested configs?
I'm sorry if this isn't the right place for this kind of questions :/

Openstruct hash functionality

Hi,

Perhaps I'm missing how, as I've inspected the objects and looked through the source, but can't find it. I have an api section in my config:

Settings.api.gateways.paypal

This holds the config for our Paypal interface. I wanted to be able to do something similar:

@gw ||= Gateway.factory(:paypal, Settings.api.gateways.paypal)

Unfortunately I don't see a way to convert to a hash. I also can't find much on converting an Openstruct to a hash.

Have any suggestions?

JSON error with resque and multi_json

Hi,

In a Rails app, where I also use resque, multi_json, yajl alongside with rails_config, I get errors whenever I try to pass a RailsConfig::Options object to a Resque job.

The error Is like this :

LoadError (no such file to load -- json):
  activesupport (3.0.7) lib/active_support/dependencies.rb:239:in `require'
  activesupport (3.0.7) lib/active_support/dependencies.rb:239:in `require'
  activesupport (3.0.7) lib/active_support/dependencies.rb:225:in `load_dependency'
  activesupport (3.0.7) lib/active_support/dependencies.rb:596:in `new_constants_in'
  activesupport (3.0.7) lib/active_support/dependencies.rb:225:in `load_dependency'
  activesupport (3.0.7) lib/active_support/dependencies.rb:239:in `require'
  rails_config (0.2.3) lib/rails_config/options.rb:52:in `to_json'
  multi_json (1.0.3) lib/multi_json/engines/yajl.rb:14:in `encode'
  multi_json (1.0.3) lib/multi_json/engines/yajl.rb:14:in `encode'
  multi_json (1.0.3) lib/multi_json.rb:72:in `encode'
  resque (1.19.0) lib/resque/helpers.rb:22:in `encode'
  resque (1.19.0) lib/resque.rb:151:in `push'
  resque (1.19.0) lib/resque/job.rb:48:in `create'
  resque (1.19.0) lib/resque.rb:233:in `enqueue'

Thanks for any help

bundle install fails with Rails 3.0.0

Previously had rails 3.0.0.rc and rails_config 0.1.3. All working fine.

  • Installed Rails 3.0.0 release

  • Updated rails application Gemfile: gem 'rails', '3.0.0'

  • bundle install
    Bundler could not find compatible versions for gem "activesupport":
    In Gemfile:
    rails (= 3.0.0) depends on
    activesupport (= 3.0.0)

    rails_config (= 0.1.3) depends on
    activesupport (3.0.0.rc)

Using different constant names

I am using rails_config and I wanted to namespace the constant used. However, this is not possible using the standard methods.

Also, when a different constant name than 'Settings' is used, all my controllers break in development mode as line 24 of railtie.rb specifically uses the Settings constant, but only in development mode.

I have worked around this in my app, but just wanted to let you know for future versions.

Thanks

Gary

Add Ability to Opt out of OStruct conversion

Sometimes I need a real hash within my Settings. For example:

prices:
  1: 2.99
  5: 9.99
  15: 19.99
  30: 29.99

I'd like to be able to access this setting via Settings.prices[1] #=> 2.99

However, this doesnt work since rails_config tries to convert everything to OpenStructs, and 1,5,15,etc do not work as method accessors.

Here's a workaround syntax:

prices:
  type: hash
  contents:
    1: 2.99
    5: 9.99
    15: 19.99
    30: 29.99

While parsing the settings, if there's a type: field set to "hash", I'll assign the hash value in contents: to the setting. So the previous settings will provide a config of Settings.prices #=> {1 => 2.99, 5 => 9.99, etc..}

not working with Rails 4.1.0.rc2

having rails_config in the project throws an error on start

/Users/jc/.rvm/gems/ruby-2.0.0-p247/bundler/gems/rails_config-a672a110738b/lib/rails_config/integration/rails.rb:8:in `block in <class:Railtie>': undefined method `join' for nil:NilClass (NoMethodError)
  from /Users/jc/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.1.0.rc2/lib/active_support/lazy_load_hooks.rb:36:in `call'
  from /Users/jc/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.1.0.rc2/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
  from /Users/jc/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.1.0.rc2/lib/active_support/lazy_load_hooks.rb:45:in `block in run_load_hooks'
  from /Users/jc/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.1.0.rc2/lib/active_support/lazy_load_hooks.rb:44:in `each'
  from /Users/jc/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.1.0.rc2/lib/active_support/lazy_load_hooks.rb:44:in `run_load_hooks'
  from /Users/jc/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.1.0.rc2/lib/rails/application.rb:121:in `initialize'
  from /Users/jc/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.1.0.rc2/lib/rails/railtie.rb:171:in `new'
  from /Users/jc/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.1.0.rc2/lib/rails/railtie.rb:171:in `instance'
  from /Users/jc/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.1.0.rc2/lib/rails/application.rb:90:in `inherited'
  from /Users/jc/Projects/test/config/application.rb:25:in `<module:Contacts>'
  from /Users/jc/Projects/test/config/application.rb:24:in `<top (required)>'
  from /Users/jc/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.1.0.rc2/lib/rails/commands/commands_tasks.rb:146:in `require'
  from /Users/jc/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.1.0.rc2/lib/rails/commands/commands_tasks.rb:146:in `require_application_and_environment!'
  from /Users/jc/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.1.0.rc2/lib/rails/commands/commands_tasks.rb:68:in `console'
  from /Users/jc/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.1.0.rc2/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
  from /Users/jc/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.1.0.rc2/lib/rails/commands.rb:17:in `<top (required)>'
  from bin/rails:4:in `require'

Dup values in array converted to one

I use this:

photos:
  upload:
    photo_avatar_size: [60, 60]

And I get array with 1 element:

irb(main):001:0> Settings.photos.upload.photo_avatar_size
=> [60]

Is it bug?

rails_config (0.3.1)

Issue on ruby-head / rails (master)

NameError in AppsController#index

uninitialized constant Settings

/Users/kain/.rvm/gems/ruby-head/bundler/gems/rails-84763047a03e/activesupport/lib/active_support/inflector/methods.rb:221:in `block in constantize'
/Users/kain/.rvm/gems/ruby-head/bundler/gems/rails-84763047a03e/activesupport/lib/active_support/inflector/methods.rb:220:in `each'
/Users/kain/.rvm/gems/ruby-head/bundler/gems/rails-84763047a03e/activesupport/lib/active_support/inflector/methods.rb:220:in `constantize'
/Users/kain/.rvm/gems/ruby-head/bundler/gems/rails-84763047a03e/activesupport/lib/active_support/core_ext/string/inflections.rb:42:in `constantize'
rails_config (0.2.4) lib/rails_config/integration/rails.rb:30:in `block (3 levels) in <class:Railtie>'

[Question] Raise Uninitialize constant Rails in deploy.rb

I'm using rails4.1.1 and Ruby2.0.0-p481
I try to use rails_config with capistrano.

and I wrote like this

require 'aws-sdk'
require 'yaml'
require 'rails_config'

RailsConfig.load_and_set_settings('config/settings.yml')

# settings

but the line 3, require "rails_config" raises error uninitialized constant Rails.

I found to work this with rails_config0.3.

Is there any change in rails_config0.4 ?

Currently some keys (e.g. "id" or "domain.tld") are critical

Hi,

today i migrated our existing rails configuration to rails_config gem.

we had a several hundred lines long YAML-file that we accessed using the following syntax:
config['root_key.sub_key']

during the migration i had problems with following keys:
root:
id: 1
'google.com': 2

Settings.root.id => actually called object#id
Settings.root.google.com => :)

maybe we can change this in the future?

cheers,
--dpree

Tests don't pass with Ruby 1.8

I've tried to run te test suite with Ruby 1.8.7 (REE) and I got this :

(the current commit is ec6f46d)

~/Sandbox/jlecour/rails_config [ree-1.8.7-2012.02][git:fix_symbols?]
→ be rake
/Users/jlecour/.rbenv/versions/ree-1.8.7-2012.02/bin/ruby -S rspec spec/rails_config_spec.rb spec/sources/yaml_source_spec.rb
FF......FF...........FFFF.............

Failures:

  1. RailsConfig should load a basic config file
    Failure/Error: config = RailsConfig.load_files(setting_path("settings.yml"))
    TypeError:
    nil is not a symbol

    ./lib/rails_config/options.rb:81:in `__convert'

    ./lib/rails_config/options.rb:79:in`each'

    ./lib/rails_config/options.rb:79:in `__convert'

    ./lib/rails_config/options.rb:31:in`load!'

    ./lib/rails_config.rb:30:in `load_files'

    ./spec/rails_config_spec.rb:6

  2. RailsConfig should load 2 basic config files
    Failure/Error: config = RailsConfig.load_files(setting_path("settings.yml"), setting_path("settings2.yml"))
    TypeError:
    nil is not a symbol

    ./lib/rails_config/options.rb:81:in `__convert'

    ./lib/rails_config/options.rb:79:in`each'

    ./lib/rails_config/options.rb:79:in `__convert'

    ./lib/rails_config/options.rb:31:in`load!'

    ./lib/rails_config.rb:30:in `load_files'

    ./spec/rails_config_spec.rb:12

  3. RailsConfig should allow overrides
    Failure/Error: config = RailsConfig.load_files(files)
    TypeError:
    nil is not a symbol

    ./lib/rails_config/options.rb:81:in `__convert'

    ./lib/rails_config/options.rb:79:in`each'

    ./lib/rails_config/options.rb:79:in `__convert'

    ./lib/rails_config/options.rb:31:in`load!'

    ./lib/rails_config.rb:30:in `load_files'

    ./spec/rails_config_spec.rb:52

  4. RailsConfig should allow full reload of the settings files
    Failure/Error: RailsConfig.load_and_set_settings(files)
    TypeError:
    nil is not a symbol

    ./lib/rails_config/options.rb:81:in `__convert'

    ./lib/rails_config/options.rb:79:in`each'

    ./lib/rails_config/options.rb:79:in `__convert'

    ./lib/rails_config/options.rb:31:in`load!'

    ./lib/rails_config.rb:30:in `load_files'

    ./lib/rails_config.rb:37:in`load_and_set_settings'

    ./spec/rails_config_spec.rb:59

  5. RailsConfig Merging hash at runtime should be chainable
    Failure/Error: let(:config) { RailsConfig.load_files(setting_path("settings.yml")) }
    TypeError:
    nil is not a symbol

    ./lib/rails_config/options.rb:81:in `__convert'

    ./lib/rails_config/options.rb:79:in`each'

    ./lib/rails_config/options.rb:79:in `__convert'

    ./lib/rails_config/options.rb:31:in`load!'

    ./lib/rails_config.rb:30:in `load_files'

    ./spec/rails_config_spec.rb:158

    ./spec/rails_config_spec.rb:162

  6. RailsConfig Merging hash at runtime should preserve existing keys
    Failure/Error: let(:config) { RailsConfig.load_files(setting_path("settings.yml")) }
    TypeError:
    nil is not a symbol

    ./lib/rails_config/options.rb:81:in `__convert'

    ./lib/rails_config/options.rb:79:in`each'

    ./lib/rails_config/options.rb:79:in `__convert'

    ./lib/rails_config/options.rb:31:in`load!'

    ./lib/rails_config.rb:30:in `load_files'

    ./spec/rails_config_spec.rb:158

    ./spec/rails_config_spec.rb:166

    ./spec/rails_config_spec.rb:166

  7. RailsConfig Merging hash at runtime should recursively merge keys
    Failure/Error: let(:config) { RailsConfig.load_files(setting_path("settings.yml")) }
    TypeError:
    nil is not a symbol

    ./lib/rails_config/options.rb:81:in `__convert'

    ./lib/rails_config/options.rb:79:in`each'

    ./lib/rails_config/options.rb:79:in `__convert'

    ./lib/rails_config/options.rb:31:in`load!'

    ./lib/rails_config.rb:30:in `load_files'

    ./spec/rails_config_spec.rb:158

    ./spec/rails_config_spec.rb:170

  8. RailsConfig Merging hash at runtime should rewrite a merged value
    Failure/Error: let(:config) { RailsConfig.load_files(setting_path("settings.yml")) }
    TypeError:
    nil is not a symbol

    ./lib/rails_config/options.rb:81:in `__convert'

    ./lib/rails_config/options.rb:79:in`each'

    ./lib/rails_config/options.rb:79:in `__convert'

    ./lib/rails_config/options.rb:31:in`load!'

    ./lib/rails_config.rb:30:in `load_files'

    ./spec/rails_config_spec.rb:158

    ./spec/rails_config_spec.rb:175

    ./spec/rails_config_spec.rb:175

Finished in 0.04651 seconds
38 examples, 8 failures

Failed examples:

rspec ./spec/rails_config_spec.rb:5 # RailsConfig should load a basic config file
rspec ./spec/rails_config_spec.rb:11 # RailsConfig should load 2 basic config files
rspec ./spec/rails_config_spec.rb:50 # RailsConfig should allow overrides
rspec ./spec/rails_config_spec.rb:57 # RailsConfig should allow full reload of the settings files
rspec ./spec/rails_config_spec.rb:161 # RailsConfig Merging hash at runtime should be chainable
rspec ./spec/rails_config_spec.rb:165 # RailsConfig Merging hash at runtime should preserve existing keys
rspec ./spec/rails_config_spec.rb:169 # RailsConfig Merging hash at runtime should recursively merge keys
rspec ./spec/rails_config_spec.rb:174 # RailsConfig Merging hash at runtime should rewrite a merged value
rake aborted!
/Users/jlecour/.rbenv/versions/ree-1.8.7-2012.02/bin/ruby -S rspec spec/rails_config_spec.rb spec/sources/yaml_source_spec.rb failed

Overloading with nulls

I was expecting to be able to "unset" a property in an overriding config file. For example, consider the following:

In config/settings.yml
name: Admin
email: [email protected]

In config/settings/development.yml
name: Nobody
email: null

However, it looks like instead of unsetting the value, the derived property is untouched.
That is, the above case yields:
Settings.name # => Nobody
Settings.email # => [email protected]

Rather than what I was expecting:
Settings.name # => Nobody
Settings.email # => nil

Bug, or feature?

Cannot visit RailsConfig::Options

This's my code:

cities: 
  - "杭州市":
class City < ActiveRecord::Base
  class << self
    def directory
      @active_cities = []
      Settings.cities.each do |name|
        #active_cities << find_by_name('') 
        @active_cities << where(name: name).first 
      end
      @active_cities
    end
  end
end

If I remove RailsConfig, it does work:

class City < ActiveRecord::Base
  class << self
    def directory
      @active_cities = []
      ['杭州市'].each do |name|
        #active_cities << find_by_name('') 
        @active_cities << where(name: name).first 
      end
      @active_cities
    end
  end
end

broken during assets precompilation

Hello,
Since the recent commit (0.2.6): 1e7f860

I cannot evaluate Settings anymore, if I try to execute the rake task for assets:precompile (Rails 3.2.2) it bombs with

rake aborted!
uninitialized constant Settings

This is working fine in 0.2.5.

config/environments/staging.rb

config.cache_store = :dalli_store, Settings.app.memcache.hosts

command

bundle exec rake RAILS_ENV=staging RAILS_GROUPS=assets assets:precompile --trace

Using rails_config in config/environments/*.rb

I'm not sure if this is a Rails3 issue, as I just started using Rails3, but I had a similar issue with Settingslogic last night on a Rails3 app.

I use a config for various settings in my environment configs, eg:

config.action_mailer.default_url_options = { :host => Settings.base.host }

With that being said, I get the following error:

uninitialized constant Settings

Is this the way the boot process works in Rails3?

Thanks!

Method to specify some settings as read-only

It would be nice to have a method/feature that allows us to specify some settings as read-only, particularly as when I display the settings, there is a key/value that appears for each section as modifiable: true. It seems that this is something to do with the options so if a shortcut could be provided to allow us to mark a section as modifiable or not at runtime, it would make it easier to manage sections.

Integration with encrypted Rails credentials

Rails 4.1 introduces a built-in convention for having a secrets.yml with secrets in it: http://edgeguides.rubyonrails.org/4_1_release_notes.html#config-secrets-yml

It would be cool if this gem did some of these things:

  • Aliased Rails.application.secrets to Settings.secrets
  • Shimmed the secrets.yml functionality in for Rails < 4.1
  • Allowed you to have a secrets.local.yml or config/secrets/production.yml that behave the same way as the existing conventions do for non-secrets files

I think this gem is a better overall solution to managing configuration, but if Rails is making this a thing, it would make sense to go along with it and make this gem work nicely with the new stuff.

ActiveRecord support

I'd like to utilize the power of rails_config to extend the storage of the key / value pairs in ActiveRecord databases. Is this possible with the existing code? Maybe if someone points me in the direction of where to add this functionality I may be able to extend this gem further with ActiveRecord support.

If I was to create a model with the same name of Settings, is it possible to default to ActiveRecord with the YAML files as fallback?

Readme incorrect

It seems that the generator has changed name.

$ bundle exec rails g rails_config:install
Could not find generator rails_config:install.
$ bundle exec rails g rails_config
    create  config/initializers/rails_config.rb

Also looking at the code I thought the generator should provide empty settings files, but it doesn't seem to

NameError: uninitialized constant AppName::Application::Settings

When I upgrade from 0.2.5 to the most recent version, I'm now starting to get this error:

NameError: uninitialized constant AppName::Application::Settings

The offending line comes from app/config/application.rb:

config.action_mailer.default_url_options = { host: Settings.host }

I believe the issue comes from a change in how RailsConfig is loaded, based off one of these two commits: 9e17fac and 1e7f860.

Is there any workaround for this?

Add more YAML files at load time (not runtime)

I'd like to add more files to the RailsConfig file load chain. I know what these files will be in advance, so I'd rather avoid loading them at runtime. Is there some syntax available that works like the following...

RailsConfig.setup do |config|
  config.add_file(Rails.root.join("config", "some_additional_stuff.yml")
end

I didn't see anything whilst digging through the code, though I am admittedly n00bish when it comes to parsing out gems.

secret token not recognized by heroku.

gemfile:
gem "rails_config", '0.2.5'

i have configured secret token:
MySite::Application.config.secret_key_base = Settings.secret_token

In console i have:
2.0.0p353 :001 > Settings.secret_token => "9a1773c......."
the config/initializers/secret_token.rb is not present in my .gitignore

and i have configured SECRET_TOKEN on heroku:
$ heroku config:set SECRET_TOKEN=9a1773c.......

after heroku pushing , i try to open my app:
$ heroku open

but i have this response:
Internal Server Error You must set config.secret_key_base in your app's config.

if i hard-code the secret token it works:
MySite::Application.config.secret_key_base = "9a1773c......."

Ideas?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.