Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Rails application config variable in model

I've defined custom configuration variables in my rails app (APP_CONFIG hash). Ok, now how can I use these variables in my models? Directly calling APP_CONFIG['variable'] in models its a not rails way! For example I can use these models without Rails environment. Then APP_CONFIG not be defined.

ATM I use model observer and assign global config variables with instance variables, like this:

def after_initialize model
  Mongoid.observers.disable :all do
    model.user_id = APP_CONFIG['user_id'])
    model.variable = User.find(model.user_id).variable
  end
end

But this solution looks like a monkey patch. There is better way?

Or I should keep it simplest and can just define APP_CONFIG hash in new app (not Rails app)?

like image 407
enRai Avatar asked Nov 13 '22 01:11

enRai


1 Answers

I'd use dependency injection. If you have an object that needs various config values, you could inject the config object through the constructor:

class Something
  def initialize(config = APP_CONFIG)
    @config = config
  end
end

And if the config is only needed for a single method, simply pass it to the method:

def something(config = APP_CONFIG)
  # do something
end

Ruby evaluates parameters when the method is called. The default value allows you to use your config object in development/production without having to manually pass it to the methods and to use a stub instead of the actual config in your tests.

Instead of defining another global variable/constant, you could also use the Rails config instead:

def something(config = Rails.config)
  # do something
end
like image 106
rubiii Avatar answered Dec 23 '22 06:12

rubiii