Make application settings accessible to the user through web forms

I scanned all day to try and find the dominant agreement on setting user-modifiable application settings in Rails ... without doses.

I am working on a simple application that is intended for use by one organization, so there is no need for a user model, there is only one administrator. This administrator should be able to change some settings on the site, such as logo, color scheme, tagline, etc.

What is the best practice for creating these types of applications in Rails 3.1 and making them easily accessible to the end user? Bonus points for any applications that you can link to.

+4
source share
2 answers

The dominant convention for storing editable application parameters as a whole seems to be the concept of a keystore supported by either ActiveRecord or other mechanisms. And, as far as I know, there are at least two good strategies for storing the settings of your application depending on your details.

If you need a general approach, but extremely flexible for defining several (non) areas that may be associated with AR models, you are Rails-Settings (or its cached version of Rails-Settings-Cached ). I have not tried using the plugin in Rails 3.1, but it works well on version 3.0. This allows you to have things like:

Settings.main_color = '#3333CC' Settings.logo_file_name = 'images/logo.png' Setting['preferences.color'] = :blue 

In case you need a reliable approach using Single-Table-Inheritance and allowing you to perform checks in certain settings, as is the case with actual AR Records, you have this good article written by Jeff Dean that will help you in this process. This way you set the parameters by grouping them into subclasses, and you can have things like:

 class ApplicationSettings::PageLayout < ApplicationSetting validates :title, :presence => true ... def title value end def title=(value) self.value = value end 

And I assume that with some simple setup, you might even have has_many and belongs_to in some of your settings (for example, a list of phone numbers or emails with a variable size).

Personally, I prefer the latter approach (when settings are a big problem) as it gives you more control over the settings you store and keeps your code clean and dry, which allows you to follow the MVC pattern.

+3
source

Usually I create a "Properties" model with some base forests in the "admin" section, and then call the corresponding "Property" field, where, say, the property Property.find (: field => "EARLIEST_DATE: YEAR"), which will have user settable value.

Properties may not be the best name for a database table (they tend to think that there are too many chances to reserve a name place somewhere in the queue), but you get an idea. The advantage is that you can configure the scope to access user-defined values.

enter image description here

0
source

Source: https://habr.com/ru/post/1381587/


All Articles