How to redefine/override initialized variables in Rails? - ruby-on-rails

One of my applications loads a yml file as a global variable in config/environment.rb so that the variable could be used globally, like this.
CFILE = YAML::load(File.open('path/to/the/file'))
Now I need to periodically reload the file to the same variable, since the file would be periodically modified. I've tried adding a rake task with the same line above but it doesn't really make the change.
How can I update the variable?
[Solved] Resolved with CFILE.replace new_cfile

You're not supposed to do that. :) But, you can!
Object.send(:remove_const, :CFILE)
Then redefine as if it were never there.

Related

How to use database.yml define json file for each environment

I working on json file format in rails.
I like to use environment in config/databse.yml (or better place) to define what json file to use in the app.
But have no idea, unable to find any sample.
Any help please.
Existing code in helper as follow
def get_table(foo)
get_data = if [:production, :sandbox].include?(Rails.env)
File.read("support/product.json")
else
File.read("support/sample.json")
end
JSON.parse(get_data)
end
If I understand you correctly then you are trying to load a JSON file based on the current environment? I'm not sure a JSON file is really the kind of data store you are looking for... but that is a different question.
In your case, I would use an environment variable to set the file name that is to be used during the execution of the application.
Change the helper to something like:
def get_table
database_content_from_file = File.read(ENV['database_file'])
JSON.parse(get_data)
end
You can now set the environment variable 'database_file' in each of your different types of environments. This can be done by setting a system-wide variable or using a gem like https://github.com/laserlemon/figaro (which I highly encourage you to use).
With this, you could set ENV['database_file'] to 'support/sample.json' in your development environment and set it to 'support/product.json' in production.
I hope this answers your question. If not please rephrase your question in a manner that is easier to understand.

Rails application global definitions

Hi I want to have some global variables
for example for the slack-notifier I wan to initilize It once and reusing it.
what are the best practices for something like that?
Use $ sign to your variable and it become global. Like below :
$slack-notifier
Also it should initialize in config/initializers folder. For this You can create in any file under config/intializers like config/intializers/xyz.rb
If you don't want to change this throughout application. Then you can create a constant also.
As a convention you should create this constant in intializers folder.
config/intializers/initialize.rb
And contant inside it should be a capital word.
# config/intializers/initialize.rb
ANY_CONSTANT = 'xyz'
You can use this ANY_CONSTANT any where in app.
In /config/initializers/global.rb
$slack-notifier
Starting from Ruby on Rails 4.2 you have config/secrets.yml file, where you can store your settings.
You specify them like this:
your_app: &your_app
app_id: 123
and get the values as follows:
Rails.application.secrets[:your_app]['app_id']
Another option is to have env variables. Take a look into this gem.
With dotenv you create a file called .env in the app's root directory and store your global settings there as follows:
APP_ID=123
To get it within app you would do
ENV['APP_ID']
Add this file to .gitignore. This is the most safe way to store app settings.

application wide global variable

In Rails, where should I define the variable which can be recognized by every layer of Rails stacks.
For example, I would like to have a CUSTOMER_NAME='John' variable which can be accessed in helper, rake task, controller and model. Where should I define this variable in Rails app?
I am using Rails v2.3.2
In an initializer in /app/config/initializers all .rb files in here get loaded, I usually create one called preferences.rb for things like this.
See: http://guides.rubyonrails.org/configuring.html#using-initializer-files
An alternative approach is to set a key on the config object in config/application.rb, like so:
MyApp::Application.configure do
# ...
config.my_key = 'some "global" value'
end
You can then access my_key from anywhere in your app with just this:
MyApp::Application.config.my_key
Also, Mike Perham has described a similar, though a more comprehensive approach in his blog post.
You want a true global constant? Use ::COSTUMER_NAME.
You want a true global variable? Use $COSTUMER_NAME (discouraged).
You want a request-global variable? Use the Hash in the #env method.

How do i define a second environment.rb file in rails?

My default environment.rb is overflowing, and i would like to have a separate file that works exactly the same way. How can I do so?
You're likely adding things to the environment file that should be in an initializer. Check the config/initializers directory for some examples of what to put in there. That should allow you to break things up and make everything more organized.
Rails actually uses eval to load the special environment files such as config/environments/development.rb. This is the code it uses:
eval(IO.read(configuration.environment_path), binding, configuration.environment_path)
You could define a method such as load_more_environment like this:
def load_more_environment(path)
eval(IO.read(path), binding, path)
end
The first argument to eval is just the code you want to load and it will be executed within the current binding. The third argument will be used to report syntax errors in the file.

set configuration constants depending on environment in rails

I would like to define a constant (like the admin-email-adress) depending on the environment. What is the easiest way to do this?
I'd like something like that, in development.rb (or test or production.rb):
ADMIN_EMAIL = "foo#bar.com"
And be able to access it by calling something like
ADMIN_EMAIL
Is there an easy way or do I have to do something like creating a module and initialize it and stuff (and in case you're wondering if I have any idea about this, unfortunately: I don't)
It works this way, but one has to
restart the server, for the constants
to take effect.
In config/environments/, there are some configuration files that get executed based on what environment you're currently in. Try defining a constant in one of those.

Resources