Railsの起動: コンフィグレーション
「config/environment.rb」
Rails::Initializer.run do |config|
# Settings in config/environments/* take precedence over those specified here
take precedence over those specified here (ここに指定されたものよりも優先される)
つまりモード固定の環境ファイルに含まれている設定が config/environment.rb より優先される
モード固有の環境ファイルが後から読み込まれ、設定を上書きする
●フレームワークの省略
「config/environment.rb」
# Skip frameworks you're not going to use (only works if using vendor/rails)
# config.frameworks -= [ :action_web_service, :action_mailer ]
●プラグインのロード
「config/environment.rb」
# Only load the plugins named here, by default all plugins in vendor/plugins are loaded
# config.plugins = %W( exception_notification ssl_requirement )
●追加のロードパス
デフォルト以外のロードパスを追加する場合に使用
「config/environment.rb」
# Add additional load paths for your own custom dirs
# config.load_paths += %W( #{RAILS_ROOT}/extras )
%W は%記法はシングルクォートで囲んだ文字列を空白文字で分割したのと同じ
参考 : http://www.ruby-lang.org/ja/man/html/_A5EAA5C6A5E9A5EB.html#a.25.b5.ad.cb.a1
プラグインを記述して Rails を拡張すればロードパスの規約は、そのプラグイン固有のものになる
ってことは、追加のロードパスを指定するオプションは不要なのか
参考:=== 10 ActionView===
●ログレベルの上書き
デフォルトのログレベルは :debug であり、必要に応じて上書きできる
「config/environment.rb」
# Force all environments to use the same logger level
# (by default production uses :info, the others :debug)
# config.log_level = :debug
参考:=== 1.5 ログ ===
●ActiveRecord セッションストア
ユーザーセッションをデータベースに保存したい場合、そのオプションを設定
「config/environment.rb」
# Use the database for sessions instead of the file system
# (create the session table with 'rake db:sessions:create')
# config.action_controller.session_store = :active_record_store
参考:=== 13 セッション管理 ===
●スキーマダンパー
テストを実行するたび Rails は development データベースのスキーマをダンプし
自動生成された schema.rb スクリプトを利用し test データベースにコピーする
これは ActiveRecord の移行スクリプトに似ている。実際、同じ API を使用
「config/environment.rb」
# Use SQL instead of Active Record's schema dumper when creating the test database.
# This is necessary if your schema can't be completely dumped by the schema dumper,
# like if you have constraints or database-specific column types
# config.active_record.schema_format = :sql
●オブザーバ
ActiveRecord オブザーバは Rails アプリケーションのファーストクラスオブジェクトで
キャッシュの消去や非正規化データの管理といった特定のタスクを実行
デフォルトの environment.rb での例では、アプリケーションにおいて
名目上オブザーバとして記述するクラスが挙げられます
Rails は cacher オブザーバ、garbage_collector オブザーバを実際に提供しませんが
Ruby はガベージコレクションを行います
「config/environment.rb」
# Activate observers that should always be running
# config.active_record.observers = :cacher, :garbage_collector
●タイムゾーン
タイムゾーンを正しく扱うのは難しく、慎重に対応しなければならいない
Rails のデフォルトのタイムゾーンはローカル時間(Time.local)で、サーバと同じタイムゾーン
Rails が使用するタイムゾーンを変更するには、TZ環境変数を変更
Web アプリケーション全体で変更したい場合
environment.rb で他のタイムゾーン設定の近くに配置する方がよい
※TZ コードの値は environment.rb に限らず、どこで設定しても構わない
ユーザー固有のタイムゾーンをサポートしたい場合
Time.utc を使用して、データベースに時刻を UTC として記録しておく
「config/environment.rb」
# Make Active Record use UTC-base instead of local time
# config.active_record.default_timezone = :utc
ユーザーに関連付けられているタイムゾーンとの間で時刻を変換する方法が必要
ただ、Rails 標準の TimeZone クラスは夏時間(DST)に対応していないみたい…
TZInfo(http://tzinfo.rubyforge.org/) という Pure Ruby gem には
DST を正しく処理し Rails 標準クラスの代わりに使用できる TimeZone クラスがある
このクラスをアプリケーションで使用するには tzinfo gem と tzinfo_timezone
両方のプラグインをインストールする必要がある
ただし、このソリューションは Pure Ruby で、かなり低速らしい
また、Ruby の Time クラスは、DST を正しく処理できる
以下のコードをアプリケーションのヘルパーモジュールの1つに追加する
もしくは、 lib フォルダ内のカスタムクラスに追加する方法もある
※ただ、この方法は Windows 環境ではうまくいかないらしい
(参照:http://www.ruby-forum.com/topic/75790)
「参考サイト:http://www.ruby-forum.com/topic/79431」
# to convert posted date/time to UTC and save to DB
def user2utc(t)
ENV["TZ"] = current_user.time_zone_name
res = Time.local(t.year, t.month, t.day, t.hour, t.min, t.sec).utc
ENV["TZ"] = "UTC"
res
end
# to display date/time in a view
def utc2user(t)
ENV["TZ"] = current_user.time_zone_name
res = t.getlocal
ENV["TZ"] = "UTC"
res
end