huacnlee / rails-settings-cached

Global settings for your Rails application.
Other
1.06k stars 202 forks source link

Get setting type from record #162

Closed kausters closed 3 years ago

kausters commented 5 years ago

Hi!

I'm trying to update to 2.0. Previously I had added a "format" field (enum of :boolean, :string, etc) to my Setting model so I could render the appropriate form control automatically. I was glad to see that now there is a type property in the field declaration but I can't find a way to access this property in my form view.

Any tips? Thanks and congratulations on the update!

huacnlee commented 5 years ago

You wants type for write admin interface?

I strongly recommend you follow the README guides to write Setting form, write form control HTML template for each keys, not use a loop to generate.

<%= form_for(Setting.new, url: admin_settings_path) do |f| %>
  <div class="form-group">
    <label class="control-label">Host</label>
    <%= f.text_field :host, value: Setting.host, class: "form-control", placeholder: "http://localhost"  %>
  </div>

  <div class="form-group form-checkbox">
    <label>
      <%= f.check_box :captcha_enable, checked: Setting.captcha_enable? %>
      Enable/Disable Captcha
    </label>
  </div>

  <div class="form-group">
    <label class="control-label">Admin Emails</label>
    <%= f.text_area :admin_emails, value: Setting.admin_emails.join("\n"), class: "form-control" %>
  </div>

  <div class="form-group">
    <label class="control-label">Notification options</label>
    <%= f.text_area :notification_options, value: YAML.dump(Setting.notification_options), class: "form-control", style: "height: 180px;"  %>
    <div class="form-text">
      Use YAML format to config the SMTP_html
    </details>
<% end %>

After that, you will get a better setting interface like this:

屏幕快照 2019-05-20 下午1 56 32 屏幕快照 2019-05-20 下午1 58 58
kausters commented 5 years ago

For the site I'm building, the settings are internal admin stuff with basic fields, not an artisanal user settings page. Regardless, everything in the screenshots could just as well be abstracted away in a generator/loop — groups, labels, placeholders, extra text etc. And as a programmer I want to automate my work, and not being able to add extra fields to Settings records and then access them is a major limitation, especially if I need to add more settings or change the field template in the future — customizing a view with the same things over and over feels counter-productive.

huacnlee commented 5 years ago

I will try to implement this.

svarione commented 4 years ago

Hi, I had the same problem here! I created a super simple singleton_method (no test 👎) here: https://github.com/huacnlee/rails-settings-cached/compare/master...svarione:master which adds a #{key}_setting_type method which returns a symbol

huacnlee commented 3 years ago

get_field method can do that:

Setting.get_field("host")
=> { key: "host", type: :string, default: "http://example.com", readonly: true }