Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access a yaml in rails config path

I'm writting a ruby gem and I want to let user use your own yaml file configuration, but I don't know how to test if the user_config.yml exists into rails config path.
I want to use user_config.yml only if this file exists into config path.

like image 398
squiter Avatar asked Sep 27 '12 18:09

squiter


People also ask

Where is Rails application config?

You probably know that you can configure Rails in config/application. rb and config/environments/development. rb etc. But you can also leverage that for configuring your own custom settings for your application.

What is Rails application config?

The configuration file config/application. rb and environment-specific configuration files (such as config/environments/production. rb ) allow you to specify the various settings that you want to pass down to all of the components. Rails will use that particular setting to configure Active Record.

What are Initializers in Rails?

An initializer is any file of ruby code stored under /config/initializers in your application. You can use initializers to hold configuration settings that should be made after all of the frameworks and plugins are loaded.

What is Rails root?

In Rails 2.3 Rails. root is an instance of Pathname where RAILS_ROOT is a string.


1 Answers

Rails.root (or RAILS_ROOT in earlier versions of Rails) gives you the path of the application root. From there, you can see if the file you're interested in exists.

e.g.

path = File.join(Rails.root, "config", "user_config.yml")
if File.exists?(path)
  # do something
end
like image 98
Jeremy Roman Avatar answered Oct 11 '22 01:10

Jeremy Roman