0

I need to store semi-constants :

  • Static variables (just one of each per application)
  • Used in many places (somewhat as templates)
  • Some SuperUsers can edit them (frontend form to edit them)
  • Persistence (must keep their value even with a serverrestart)
  • Different types (DateTime, String, Float, Integer, etc.)

Is there a nice way to do that ? (We're talking about just ~20 constants)

There is the solution of 1 table with as many columns as constants. Ugly. Our old application used to have a table "Constants" where one constant = one entry. But I think this is ugly as well because they had one "column" per type of constant

(I'm talking about columns as for now my app is using ActiveRecord, but we might change to mongoid later. Yet it would pose the same problem of having one table with just one entry)

Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145
Cyril Duchon-Doris
  • 12,964
  • 9
  • 77
  • 164
  • hey, did you have a moment to check this out? will it work with Mongoid? – Andrey Deineko Sep 09 '14 at 12:24
  • Sorry, I just added Mongoid (so now I have a temporary hybrid ActiveRecord/Mongoid install) but it brought its set of problems, and I havent had time to check yet if the gem was mongoid compatible. I'll let you know later. – Cyril Duchon-Doris Sep 10 '14 at 12:10

1 Answers1

1

This gem is an answer to your needs.

gem "rails-settings-cached" 

It allows you to easily define any settings/constants whatever like so:

Setting.admin_password = 'supersecret'
Setting.date_format    = '%m %d, %Y'
Setting.cocktails      = ['Martini', 'Screwdriver', 'White Russian']
Setting.foo            = 123
Setting.credentials    = { :username => 'tom', :password => 'secret' }

Due to last comments, here is the link to same gem for Mongoid.

Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145
  • This gem uses ActiveRecord. So if I plan to migrate from ActiveRecord to Mongoid documents I'm screwed ? Otherwise, yes I would need something like this. – Cyril Duchon-Doris Sep 07 '14 at 14:19
  • I will be honest - I am of no help in questions ActiveRecord/Mongoid, so this on your shoulders now to find out if it will work with mongoid (the fastest way would be just to send to gem author an email with short question) – Andrey Deineko Sep 07 '14 at 15:40
  • If you edit your answer to include a link to https://github.com/marten/mongoid-app_settings I'll accept it. (It's a similar gem but for Mongoid) – Cyril Duchon-Doris Sep 15 '14 at 17:33