Skip to content

Latest commit

 

History

History
142 lines (112 loc) · 4.87 KB

field.md

File metadata and controls

142 lines (112 loc) · 4.87 KB

field

  field :field_name, {options}

Parameters

:actions

  • Actions that the field will appear
  :actions => [:index]

:advanced_search

  • Define if the field will be in the advanced search
  :advanced_search => {operator: :equal}

:sortable

  • Define if the field will be sortable
  • Default Value: true
  :sortable => true

:as

  • Define the type of the render that will be used to render the field. If the type passed as parameter to SimpleForm, as a type of Input. The only exception is the :partial parameter that is explained below.
as: :partial

Instead of rendereing a field, Carnival will render a partial instead of the default field render, the name of the partial to be rendered must be defined in the :partial_name parameter. The following declaration inside AuthorsPresenter

    field :field_name, actions: [:edit], as: :partial, partial_name: "custom_partial"

Instead of rendereing a field, Carnival fill search for a partial with the field name. The following declaration inside AuthorsPresenter

:partial_name

  • Define the partial to be used to render the field value, this parameter must be used with the as: :partial, because it's only checked when the field is defined to rendered as: :partial

Carnival will try to render the partial '_custom_partial.html.haml' inside /app/views/authors. To be more useful the following objects are available to the called partial:

  • record: available to all partials, it represents de current record being displayed.
  • f: when a new or edit action is being rendered, the 'f' object will contain the current form being rendered.

:hide_if

  • Your fields can have custom visibility, for example if you want show it only for an admin_user, for that you must specify a hide_if, options when defining the field. This must be a proc or a lambda, if it returns true the field will not be shown.

  • The hide_if Proc/lambda is executed in the controller field, so you have access to all of the controller variables and the @record variable is injected in the field, so you can make conditions using the record data.

-Following is an example of how to define a custom field visibility:

    field "account.name",
          :actions => [:index, :show, :csv],
          :sortable => false,
          :hide_if => proc { @record.blacklisted && @current_admin_user.current_account_id == @record.account_id }

:presenter

If the field is a relation specifies a different presenter class for the field (in case the infered type does not match your needs) Example:

field 'user.name', presenter: 'Users::BasicUserPresenter'

Relations

Many relations

has_many and has_and_belongs_to_many

For a many to many relations you should just put the field name just like any other field, like

field :products,
  :actions => [:show, :new, :edit]

###Parameters

  • :show_as_list
    • This param list each associated record. It'll try to call a method to_label in each of the associated records, if the associated record does not have a to_\label method the method will be the to_s method.

      field :products,
        :actions => [:show, :new, :edit],
        :show_as_list => true
  • nested_form
    • When set to true Carnival will render a nested form for the association
  • nested_form_modes (Required in case nested_form is set to true)
    • This field can have the values :new or :associate.
      • :new allow you edit the associated model in the same form of the current model
      • :associate it'll just allow you associate a related model.

###Translations Carnival can use the default ActiveRecord model_name to show your many relation. If you need a different version for plural (you'll probably need one if you have a compound model name), you can specify the same key of ActiveRecord model name appending _plural to it, for ex: activerecord.models.user would have a activerecord.models.user_plural translation. If that key exists, Carnival will use it

One to one relations

belongs_to or has_one

For a one to one relation you have two ways to setup a field, one that is used for exhibition and the other for edition.

Exhibition

If you want to show a one to one relation you should name your field with 'related_model.related_field_name'.

field 'category.name',
  :actions => [:show, :index]

Edition

If you want to edit a one to one relation you should name your field with the name of the relation.

field :category,
  :actions => [:new, :edit]

A sample presenter with exhibition and edition of a one to one relation

class ProductPresenter < Carnival::BaseAdminPresenter
  field 'category.name',
    :actions => [:show, :index]

  field :category,
    :actions => [:new, :edit]
end