how to declare and refer to a single hash variable in several models (DRY, ruby on rails)

I have several models that need to call a method that takes a particular hash as input. Where should I declare this hash to keep things DRY, and how can I then use it in multiple models?

The hash will be the same for all uses. Specifically, it's just a hash of connection details that I want to pass to a call to establish_connection. I realize that that method takes a URL, though I'm still on rails 3.1, and that method was not made available until a little later.

Instead of repeating the following in five models, I just want to call something like establish_connection(legacy_db_connection_hash).

establish_connection(
  :adapter  => 'mysql',
  :host     => ENV['LEGACY_DATABASE_HOST'],
  :username => ENV['LEGACY_DATABASE_USERNAME'],
  :password => ENV['LEGACY_DATABASE_PASSWORD'],
  :database => ENV['LEGACY_DATABASE_NAME']
)

I was thinking that perhaps there's a way to do it via /lib, but I'm really not sure how to go about it.

Thank you for your time!

Answers


It should go into a module.

For example, declare a module Foo in the file lib/foo.rb (and don't forget self. in front of the method name):

module Foo
  # returns a hash of connection settings
  def self.settings
    {
      adapter: 'mysql2',
      database: ENV['LEGACY_DATABASE_NAME'],
      username: ENV['LEGACY_DATABASE_USERNAME'],
      password: ENV['LEGACY_DATABASE_PASSWORD'],
      host:     ENV['LEGACY_DATABASE_HOST']
    }
  end
end

Foo.settings will be available when you include Foo in your models.

Also, make sure to add the lib directory to config.autoload_paths in config/application.rb, so that lib/foo.rb is read and its contained Foo module is loaded:

config.autoload_paths += %W(#{config.root}/lib)

You can also declare the configuration in your application.rb file


module Yourapp
  class Application < Rails::Application

    config.db_connection_config = {
     .....
    }

  end
end

And you can use it Yourapp::Application.config.db_connection_config You can modify them in separate environment files as well, if need.

And may be instead of using establish_connection each_time you can put in a separate module in the lib module, like its suggested in the previous answer.


Here's how I do it:

config/initializers/profile_store.rb
config = YAML.load_file('config/profile_store.yml')[Rails.env]
Rails.application.config.profile_store = config.symbolize_keys
config/profile_store.yml
development:
  host: localhost
  prefix: dev-profile

test:
  host: localhost
  prefix: test-profile

You access it later with Rails.application.config.profile_store and in development you'll get

{:host => 'localhost', :prefix => 'dev-profile'}

Need Your Help

Struts2 get object attribute from select tag

list object select struts2

My problem is that I don't succeed in getting the attribute of the object of the list of my select tag.

In PHP fgetcsv converts file into one array

php excel csv

I have a csv file I'm uploading that is structured like this: