Rails3で設定ファイルを作りたいとき
Rails3でオリジナルの設定ファイル(環境によって値を変える)を作りたくて、
何かないかなーと探していたら、こんなgemがあったのでメモしておきます。
binarylogic/settingslogic · GitHub
インストール
Gemfile
gem 'settingslogic'
bundle install
使い方
設定ファイルを作成して、読み込むモデルを作成するだけです。
(詳しくは、binarylogic/settingslogic · GitHubを参照してください)
config/application.yml
defaults: &defaults api_key: abc123 development: <<: *defaults twitter: consumer_key: 'development_consumer_key' consumer_secret: 'development_consumer_secret' test: <<: *defaults twitter: consumer_key: 'test_consumer_key' consumer_secret: 'test_consumer_secret' production: <<: *defaults twitter: consumer_key: 'production_consumer_key' consumer_secret: 'production_consumer_secret'
app/models/setting.rb
class Setting < Settingslogic source "#{Rails.root}/config/application.yml" namespace Rails.env end
動作確認
$ rails c > Setting.twitter.consumer_key => "development_consumer_key"