How to use database.yml define json file for each environment - ruby-on-rails

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.

Related

Where is a logical place to put a file flag?

In a Ruby on Rails application, where would the most logical place be to put a "file flag."
I am attempting to externalize configuration and allow the presence of a file to be the deciding factor on whether or not something shows on the webapp.
Right now, I have a file here:
lib/
deployment_enabled
Model deployment.rb
class Deployment...
...
def deployment_enabled?
Dir["#{Rails.root}/lib/deployment_enabled"].any?
end
end
Now this works of course, but i'm not sure this follows the MVC paradigms, since the lib directory should consist of scripts. I could put it in config, but again - not sure it belongs there as rails uses this for rails specific configuration, not necessarily the webapp.
I could of course put this in our database, but that require a new table to be created, and that seems unnecessary.
Where's the most logical place to put this flag file? Does Rails have a directory that's created during the generation to put these sort of files?
I suggest using the Rails tmp directory for this purpose. Then:
File.exist?("#{Rails.root}/tmp/deployment_enabled")
Phusion Passenger use this kind of mechanism too.
https://www.phusionpassenger.com/library/walkthroughs/basics/ruby/reloading_code.html#tmp-always_restart-txt
I recommend that you follow the Twelve-Factor App guidelines and keep your code separate from your configuration. In this case you are really talking about a simple boolean value, and the presence of the file is just the mechanism you use to define the value. This should be done instead through an environment variable.
Something like:
DEPLOYMENT_ENABLED=1 RAILS_ENV=production rails server
You would then use an initializer in Rails to detect the value:
# config/initializers/deployment.rb
foo if ENV['DEPLOYMENT_ENABLED']
The value can still be modified at runtime, e.g., ENV['DEPLOYMENT_ENABLED'] = 0.

Where/How to code Constants in Rails 3 Application [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Ruby on Rails: Where to define global constants?
I am interested in doing this the "Rails Way" on a new application. I would also like to refer to constants in some sort of context to make the code more readable. I have an application where a user can request access to another users's data set. This AccessRequest can have one of the following statuses:
Review
Denied
Approved
I can see these values being used in reporting features in the future, so I want to make them constants in order to avoid any spelling or capitalization issues. I thought I would just put these in a constants.rb file in the config/initializers directory.
I would like to refer to these as AccessRequest::REVIEW. Since I already have a model called AccessRequest, does it make sense to put them there? Or wrap them in a class in a constants.rb file in the config/initializers directory? Which way is the Rails Way?
You don't need to use constants in Rails 3.It is better to use the Rails::Application singleton.
In your application.rb you can define your constante like:
module Yourapp
class Application < Rails::Application
config.access_request.review = 'xxx'
end
end
After in your code you can call
Yourapp::Application.config.access_request.review
After if you change value in each environment, You just define the config.xx in your config/environments/production.rb or other environment.
Belated reply, but posting as this answer still comes up in search results. Putting the constant in the model makes sense as the constant pertains directly to it. Using the Rails application config to store constants is incorrect.
As per the comment listed in application.rb:
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded
This is still valid as of Rails 3.

Rails where to put configuration like ini files?

When coding with PHP I always separate configuration values like perPage value in a separated ini file. How are you Ruby masters do this with Rails?
I would like to access config values inside my model, controller and view.
Thx!
I've generally just used a plugin like http://github.com/cjbottaro/app_config or wrote my own. I like using a config.yml file in the config directory.
There isn't really anything built in to rails to do this, but luckily there's a great plugin called settingslogic which lets you externalise your settings.
Having said that I personally like to make these things constants in my model, so for example I'd have something like this:
class Person < AR:B
DEFAULT_PER_PAGE = 10
end
Not sure about masters :) but mortal developers can usually leverage some of the existing plugins like this one: http://www.workingwithrails.com/railsplugin/5324-app-config
There are actually quite a few of them, so you'll probably find something that will suit you.

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.

What is the best way to store app specific configuration in rails?

I need to store app specific configuration in rails. But it has to be:
reachable in any file (model, view, helpers and controllers
environment specified (or not), that means each environment can overwrite the configs specified in environment.rb
I've tried to use environment.rb and put something like
USE_USER_APP = true
that worked to me but when trying to overwrite it in a specific environment it wont work because production.rb, for instance, seems to be inside the Rails:Initializer.run block.
So, anyone?
Look at Configatron: http://github.com/markbates/configatron/tree/master
I have yet to use it, but he's actively developing it now, and looks quite nice.
I was helping a friend set up the solution mentioned by Ricardo yesterday. We hacked it a bit by loading the YAML file with something similar to this (going from memory here):
require 'ostruct'
require 'yaml'
require 'erb'
#config = OpenStruct.new(YAML.load_file("#{RAILS_ROOT}/config/config.yml"))
config = OpenStruct.new(YAML.load(ERB.new(File.read("#{RAILS_ROOT}/config/config.yml")).result))
env_config = config.send(RAILS_ENV)
config.common.update(env_config) unless env_config.nil?
::AppConfig = OpenStruct.new(config.common)
This allowed him to embed Ruby code in the config, like in Rhtml:
development:
path_to_something: <%= RAILS_ROOT %>/config/something.yml
The most basic thing to do is to set a class variable from your environment.rb. I've done this for Google Analytics. Essentially I want a different key depending on which environment I'm in so development or staging don't skew the metrics.
This is how I did it.
In lib/analytics/google_analytics.rb:
module Analytics
class GoogleAnalytics
##account_id = nil
cattr_accessor :account_id
end
end
And then in environment.rb or in environments/production.rb or any of the other environment files:
Analytics::GoogleAnalytics.account_id = "xxxxxxxxx"
Then anywhere you ned to reference, say the default layout with the Google Analytics JavaScript, it you just call Analytics::GoogleAnalytics.account_id.
I found a good way here
Use environment variables. Heroku uses this. Remember that if you keep configuration in the codebase, anyone with access to the code has access to any secret configuration (aws api keys, gateway api keys, etc).
daemontool's envdir is a good tool for setting configuration, I'm pretty sure that's what Heroku uses to give application their environment variables.
I have used Rails Settings Cached.
It is very simple to use, keeps your configuration values cached and allows you to change them dynamically.

Resources