19

I have a current need for allowing both admin configurable global settings as well as per-user configurable settings within a Rails 3 app.

Are there any gems or best practices that work well for this situation?

I've found a couple of gems and blog posts but they all date back to 2006-08 and are no longer maintained. Any pointers would be appreciated, thanks.

Note: I have seen this question with the serialised hash response but in my particular case the ability to query the database for users based on their settings is required - I don't think a serialized hash would work well in that situation.

Community
  • 1
  • 1
Kevin Ansfield
  • 2,343
  • 1
  • 19
  • 20
  • 1
    As a followup to this, if you are just looking for system-wide configuration there is a new option that looks good - https://github.com/paulca/configurable_engine – Kevin Ansfield Feb 10 '11 at 13:41
  • thanks for that, looks like an awesome gem. – Pierre Oct 11 '11 at 14:55
  • Possible duplicate of [Ruby on Rails - Storing application configuration](http://stackoverflow.com/questions/2127960/ruby-on-rails-storing-application-configuration) – prograils Oct 28 '16 at 12:56

4 Answers4

20

As always right after asking for help my search bears fruit. Found Georg Ledermann's fork of rails-settings that looks like it will do just what I need.

Kevin Ansfield
  • 2,343
  • 1
  • 19
  • 20
4

Not sure I would look for a library for this function.

For global settings, I would create a config/application.yml file and read it in environment.rb and set it to an APP_CONFIG global. Then, I would override those settings on a per-user basis by adding a text column to the user table with json-encoded settings hash. Add a method to the user model that grabs APP_CONFIG and does a deep merge on the decoded user settings.

Another option would be to create a settings table, with a user_id column, and columns for each setting. One row with a null user_id would represent global settings. If a row exists for the current user, any non-nil values would override the global settings row.

aceofspades
  • 7,568
  • 1
  • 35
  • 48
  • Thanks for the response. Both of the above are definitely valid approaches. In the end I went with the Rails 3 fork of the rails-settings gem as it also easily allowed settings to be applied to multiple model types (I have global settings as well as settings for Admins, Users, and users' individual Auditions among others). – Kevin Ansfield Dec 03 '10 at 21:50
  • 1
    I had a lot of trouble with the gem in the accepted answer in rails 3.2. This solusion I was up and running in a few minutes. I did YAML serialization for my database column and setup an initializer like the one mentioned here http://stackoverflow.com/questions/592554/best-way-to-create-custom-config-options-for-my-rails-app – Kansha Jan 20 '13 at 21:14
2

May also want to look at https://github.com/railsjedi/rails_config. Amongst it's many features:

  • Global settings.yml
  • Per-environment settings files (settings/development.yml, settings/production.yml etc.)
  • Local settings files (settings.local.yml, settings/development.local.yml, etc. )
  • Embedded ERB support
  • Rails 3 / Padrino / Sinatra support
Tanzeeb Khalili
  • 7,324
  • 2
  • 23
  • 27
0

For rails 4, if you are using postgresql, you can use HStore, which is just like a serializable attribute, but you do SQL queries with it.

For rails 3, you can use activerecord-postgres-hstore gem.

fotanus
  • 19,618
  • 13
  • 77
  • 111