huacnlee / rails-settings-cached

Global settings for your Rails application.
Other
1.06k stars 202 forks source link

Silent return of defaults if Rails not initialized #152

Closed one-more-alex closed 4 years ago

one-more-alex commented 5 years ago

Looks like here is some mistake. Suppose I have setting "allow_anonymous" = false in database, but true in defaults. Then in initializes scripts will have access allowed instead of desired to be forbidden. Initializes may require settings to start Rails (setup routes, for example).

Here are two options:

Sorry to say, but silent return of default values from file instead of database is definitely mess.

I prefer to have second solution like this:

      def rails_initialized?
        return true if Rails.application && Rails.application.initialized?
        return true if try(:consider_initialized?)
        raise "Can't get values before initialization of Rails or #{self.name}.consider_initialized? successful return"
      end

# Also remove conditional returning of defaults if not rails_initialized?
# End even better to convert it to some private method like .ensure_initialized! 

Any case this situation should be highlighted in Readme. What do you think? Need to apply such a patch?

huacnlee commented 4 years ago
class Setting
  field : allow_anonymous, default: ENV["ALLOW_ANONYMOUS"], type: :boolean, readonly: true
end

Now, in rails-settings-cached 2.x, you can use readonly option to solve that.