Hosein Zarifi
Hosein Zarifi

Reputation: 69

Populate .yml file dynamically in Ruby on Rails

I want to populate my .yml file in config folder dynamically when my server is loading.

The following is an example of my .yml file

# Configuration Credentials

development: &development
  user: "***"
  password: "***" 

test:
  <<: *development

non-prod:
  user: "***"
  password: "***" 

production: &production
  user: "***"
  password: "***"  

I want to set the value for each key in the form of embedded ruby like this:

development: &development
  user: <%= get_value_for(user)  %>
  password: <%= get_value_for(password)  %>

I want to load the .yml file when the server is about to run.

I want to know where should I define the get_value_for method so I can call it inside my yml file? (probably in application.rb but I do not know how exactly)

Upvotes: 1

Views: 1667

Answers (1)

Rashid D R
Rashid D R

Reputation: 160

Create a custom class and define the method their. Then require the class inside config/application.rb

# app/global/database_details.rb
class DatabaseDetails

  def self.get_value_for(user)
  end
end

Inside config/application.rb

require_relative '../app/global/database_details'

module AppName
  class Application < Rails::Application

  end
end

Inside config/database.yml

development: &development
  user: <%= DatabaseDetails.get_value_for(user)  %>
  password: <%= DatabaseDetails.get_value_for(password)  %>

Upvotes: 1

Related Questions