Sha256: ea9a3da47604e049dfcfdbd9801e9e1a66c2643b080ae4877bdf6734bbf83bde

Contents?: true

Size: 1.63 KB

Versions: 10

Compression:

Stored size: 1.63 KB

Contents

# Fields

## Installation

Run the following generators to generate the migration files.

```sh
$ rails generate headmin:fields
$ rails db:migrate
```

## Getting Started

### Setup model

```ruby

class Settings < ApplicationRecord
  include Headmin::Fieldable
end
```

### Setup forms

```html
# app/views/admin/settings/_form.html.erb

<!-- Company name -->
<%= render 'headmin/forms/fields/text', form: form, name: :company_name do |field, attribute, label| %>
  <%= render 'headmin/forms/text', form: field, attribute: attribute, label: label %>
<% end %>

<!-- People list -->
<%= render 'headmin/forms/fields/list', form: form, name: :people do |item| %>
  <%= render 'headmin/forms/fields/text', form: item, name: :name do |field, attribute, label| %>
    <%= render 'headmin/forms/text', form: field, attribute: attribute, label: label %>
  <% end %>
<% end %>
```

#### Type of fields

- Text: `headmin/forms/fields/text`
- File: `headmin/forms/fields/file`
- Image: `headmin/forms/fields/image`
- List: `headmin/forms/fields/list`
- Group: `headmin/forms/fields/group`

### Seed database

Use the `fields_hash` setter method to set the data, the way you want to retrieve it.

```ruby
# In db/seeds.rb
Setting.create!(
  name: 'Website settings',
  fields_hash: {
    company_name: 'Insiting BV',
    people: [
      { name: 'Jef Vlamings' },
      { name: 'Gert-Jan Peeters' }
    ]
  }
)
```

### Usage in frontend

```html
# app/views/website/settings/show.html.erb
<% fields = @setting.fields_hash %>

<h1><%= fields["company_name"] %></h1>
<ul>
    <% fields["people"].each do |person| %>
    <li><%= person["name"] %></h2></li>
    <% end %>
</ul>
```

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
headmin-0.2.9 docs/fields.md
headmin-0.3.4 docs/fields.md
headmin-0.3.3 docs/fields.md
headmin-0.3.2 docs/fields.md
headmin-0.3.1 docs/fields.md
headmin-0.2.8 docs/fields.md
headmin-0.2.7 docs/fields.md
headmin-0.2.6 docs/fields.md
headmin-0.2.5 docs/fields.md
headmin-0.2.4 docs/fields.md