Reputation: 3467
I'm looking for a good way to configure a Rails app when it is already loaded. For instance, I need particular "admin" roles to be able to activate an specific option that will make the app change the way it sends emails (based on different logic). That change must be persisted across the whole app lifecycle, including restarts... until another admin decides to just uncheck the option.
Now we could create a "settings" table and store in there the configuration, however it feels like the most appropriate way to do it is by using an initializer (app.rb as an example). I know that within rails I can get those config attributes and change them to whatever I want... now how can I "persist" them?
Obviously any other approach or solution to this problem is totally welcome.
Thanks a lot
Upvotes: 3
Views: 1052
Reputation: 1088
I had the same question for a while, and came up with a mix of what you proposed: saving values in db, with defaults being set in initializer. You can always change values in runtime, it will be persisted and survive any restart, which cannot be said about, say, environment variables. I even made a gem, but it is too immature, can't give a link, but can assure you that it is very easy to implement
BTW Spree gem, which is quite popular, creates such table
create_table "spree_preferences", force: true do |t|
t.text "value"
t.string "key"
t.string "value_type"
t.datetime "created_at"
t.datetime "updated_at"
end
Upvotes: 0
Reputation: 237050
Making your app modify its code on disk in response to user input is probably not the most appropriate way to do this — just think of how that might work with e.g. app updates. It's a fragile design. This configuration is user-provided data, so store it like user-provided data.
Upvotes: 1
Reputation: 1718
You would need to use the admin role with the tables. you can use the rails_admin Gem, and persist the setting as people change them.
Upvotes: 0