Created
November 17, 2011 12:14
-
-
Save deepak/1373010 to your computer and use it in GitHub Desktop.
rails custom config
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| FooBar::Application.config.mine = OpenStruct.new({}) | |
| FooBar::Application.config.mine.omniauth_config = {:facebook => { :app_id => 123 } } | |
| # how do i access the config in my controller | |
| # i can do FooBar::Application.config.mine but that is too verbose |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
setting the config in a variable for shorter access
but the problem here is that we have to allocate another string/hash on every request even when it does not change.
if the config does not change, can just use a constant. can reassign it, but it will be semantically dirty and ruby will give a warning
Also using Rails.configuration as otherwise will have to remember the name of the app like FooBar::Application.config
storing & sharing config is a pain.
-> if it does not change use a constant. semantically correct and will utilize less memory and less GC cycles
-> if it can change per-request, use instance variable in the controller and pass the values to the model when ever needed
we can just reassign the constant. but it is semantically dirty and ruby will throw a warning
-> sometimes we will just have to assign a thread local variable. useful in case controller needs to set a variable which needs to be used in the model in a filter