简体   繁体   English

通过Web表单使用户可以访问应用程序范围的设置

[英]Make application-wide settings accessible to the user via web forms

I've been looking around all afternoon to try and find the dominant convention for setting up user-modifiable application-wide settings in Rails... no dice so far. 我整个下午都在四处寻找尝试找到在Rails中设置用户可修改的应用程序范围设置的主导惯例...到目前为止没有骰子。

I'm working on a simple application which is intended to be used by one organization, so there's no need for a user model, there's only a single administrator. 我正在开发一个旨在供一个组织使用的简单应用程序,因此不需要用户模型,只有一个管理员。 That administrator needs to have the ability to modify certain site-wide preferences, things like the logo, color scheme, tagline, etc. 该管理员需要能够修改某些站点范围的首选项,例如徽标,配色方案,标语等。

What's the best practice for creating this kind of application-wide settings in Rails 3.1, and making them easily accessible to the end-user? 在Rails 3.1中创建这种应用程序范围设置的最佳实践是什么,并使最终用户可以轻松访问它们? Bonus points for any example apps you can link to. 您可以链接到的任何示例应用的奖励积分。

The dominant convention to store editable app-wide settings seems to be the concept of a key-value store, backed either by ActiveRecord or other mechanisms. 存储可编辑应用程序范围设置的主要约定似乎是键值存储的概念,由ActiveRecord或其他机制支持。 And, as far as I know, there are at least two nice strategies for storing your app-wide settings, depending on your requisites. 而且,据我所知,根据您的要求,至少有两种很好的策略来存储您的应用程序范围的设置。

If you want a generic approach, yet extremely flexible for defining a couple of (not) scoped settings that can be in association with AR Models, you have Rails-Settings (or its cached version Rails-Settings-Cached ). 如果您想要一种通用方法,但是非常灵活地定义可以与AR模型关联的几个(非)范围设置,您可以使用Rails-Settings (或其缓存版本Rails-Settings-Cached )。 I haven't tried using the plugin in Rails 3.1 but it works well on 3.0. 我没有尝试在Rails 3.1中使用插件,但它在3.0上运行良好。 It allows you to have things like: 它允许你有这样的事情:

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

In case you want a robust approach, with Single-Table-Inheritance and allowing you to perform validations in certain settings as you would with actual AR Records, you have this nice article , written by Jeff Dean, which steps you through the process. 如果你想要一个强大的方法,使用单表继承并允许你像在实际的AR记录中那样在某些设置中执行验证,那么你有一篇很好的文章 ,由Jeff Dean编写,它将引导您完成整个过程。 This way you scope settings 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 guess that with some simple tuning you can even have has_many and belongs_to associations in some of your settings (like a variable-sized list of phone numbers or e-mails). 我想通过一些简单的调整,您甚至可以在某些设置中使用has_manybelongs_to关联(例如可变大小的电话号码或电子邮件列表)。

Personally I prefer the latter approach (when settings are a big issue) because it gives you more control over the settings you store and keeps your code clean and DRY, allowing you to follow the MVC pattern. 我个人更喜欢后一种方法(当设置是一个大问题时),因为它可以让你更好地控制你存储的设置并保持你的代码干净和干燥,让你遵循MVC模式。

I generally set up a Properties model with some basic scaffolding in the admin section - then call the relevant Property 'field' where required, say Property.find(:field => "EARLIEST_DATE:YEAR") which would have a user settable value. 我通常在admin部分中设置一个带有一些基本脚手架的Properties模型 - 然后在需要时调用相关的Property'字段',比如Property.find(:field =>“EARLIEST_DATE:YEAR”),它具有用户可设置的值。

Properties might not be the best name for a database table (tend to think there's too much chance of a reserved name collision somewhere down the line) - but you get the idea. 属性可能不是数据库表的最佳名称(往往认为保留名称碰撞的可能性太大了) - 但是你明白了。 Advantage is you can set up scopes to access the values set by the user. 优点是您可以设置范围以访问用户设置的值。

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM