View

In Wallaby, the core of view layer is to iterate decorator’s *_field_names and render the type partial using corresponding type from decorator’s *_fields.

For example, for index view, the core looks like the following pseudocode:

<% index_field_names.each do |field_name| %>
  <%= render decorated.index_type_of(field_name),
    object: decorated, field_name: field_name,
    value: decorated.try(field_name), metadata: decorated.index_metadata_of(field_name) %>
<% end %>

It extends Rails’ partial lookup order (see Rails Template Inheritance) and supports Type Partial.

NOTE: Cell (since 5.2.0) is removed from 6.1.0

Starting with these concepts:

For Type Partial:

Also check out:

Type Partial

A type partial is no different from Rails partial template. Just its name associates with the type defined in decorator’s metadata.

For example, a custom type markdown is defined for show field description in ProductDecorator:

class ProductDecorator < Admin::ApplicationDecorator
  show_fields[:description][:type] = 'markdown'
end

Then a partial can be created using the type markdown to serve this field description using the following file path:

<%# app/views/admin/products/show/_markdown.html.erb %>
<%# @param object [model] model instance %>
<%# @param field_name [String] name of the field %>
<%# @param value [Object] value of the field %>
<%# @param metadata [Hash] metadata of the field %>
<%= Redcarpet.new(value).to_html %>

Type Partial Locals

The following variables are available for a type partial:

  • object: resource object wrapped in a decorator
  • field_name: current field name
  • value: current value
  • metadata: associated metadata for current field and current action (index/show/form).
  • form: form object associated with the object

Partial Lookup Order

On top of Rails’ lookup order, Wallaby has extended the order from high to low precedence as:

  • app/views/$MOUNTED_PATH/$RESOURCES/$ACTION_PREFIX
  • app/views/$MOUNTED_PATH/$RESOURCES
  • app/views/$CONTROLLER_PATH/$ACTION_PREFIX
  • app/views/$CONTROLLER_PATH
  • app/views/$PARENT_CONTROLLER_PATH/$ACTION_PREFIX (it will keep going if there are more ancestor controllers)
  • app/views/$PARENT_CONTROLLER_PATH
  • app/views/$THEME_NAME/$ACTION_PREFIX
  • app/views/$THEME_NAME
  • app/views/wallaby/resources/$ACTION_PREFIX - (will not be applicable when $THEME_NAME is set)
  • app/views/wallaby/resources - (will not be applicable when $THEME_NAME is set)

For example, for model Product, a Wallaby controller is declared as:

# app/controllers/backend/goods_controller.rb
class Backend::GoodsController < Admin::ApplicationsController
  self.theme_name = 'foundation'
  self.model_class = Product
end

And given that:

  • Wallaby is mounted under path /manage (see how Wallaby is mounted in route).
  • action is index.

In this case, variables become:

Then the partial lookup order becomes:

  • app/views/manage/products/index/_markdown.html.erb (type partial)
  • app/views/manage/products/_markdown.html.erb (type partial)
  • app/views/backend/goods/index/_markdown.html.erb (type partial)
  • app/views/backend/goods/_markdown.html.erb (type partial)
  • app/views/admin/application/index/_markdown.html.erb (type partial)
  • app/views/admin/application/_markdown.html.erb (type partial)
  • app/views/foundation/index/_markdown.html.erb (type partial)
  • app/views/foundation/_markdown.html.erb (type partial)

Therefore, the type partial can be created in one of the above paths depending on how it’s shared:

  • if the type partial is only needed for model Product when no controller is created, then it can be created as:

    app/views/admin/products/index/_markdown.html.erb
    
  • if the type partial is only needed for controller Backend::GoodsController, then it can be created as:

    app/views/backend/goods/index/_markdown.html.erb
    
  • if the type partial is supposed to be shared among the app, then it can be created as:

    app/views/admin/application/index/_markdown.html.erb
    

NOTE: both locale and format can be included in file name. For example, to include locale en and format json for a type partial, it goes as app/views/admin/products/index/_markdown.en.json.rb.

NOTE: for type partial, the file extension doesn’t have to be erb, it’s totally fine to create it with preferable markup language (e.g. haml/slim/etc.)

NOTE: if CSV export feature is in use, two type partials should be created for index, one for HTML and one for CSV.

Action Prefix Mapping

The following resourcesful actions are mapped to the action prefixes:

ActionAction Prefix
indexindex
newform
createform
showshow
editform
updateform
destroy<N/A>

Built-in Type Partials

Association Type Partials

Type PartialAvailable to Action PrefixesMetadata Options
belongs_toindex, show, formoptions for form:
- :polymorphic_list - the list of polymorphic classes for this association.
has_oneindex, show, form 
has_manyindex, show, form 
has_and_belongs_to_manyindex, show, form 

General Type Partials

Type PartialAvailable to Action PrefixesMetadata Options
active_storageindex, show, form 
bigintindex, show, form 
bigserialindex, show, form 
binaryindex, show, form 
bit_varyingindex, show, form 
bitindex, show, form 
blobindex, show, form 
booleanindex, show, form 
boxindex, show, formoptions for index:
- :max - truncate text at max length.
cidrindex, show, form 
circleindex, show, formoptions for index:
- :max - truncate text at max length.
citextindex, show, formoptions for index:
- :max - truncate text at max length.
colorindex, show, form 
dateindex, show, form 
daterangeindex, show, form 
datetimeindex, show, form 
decimalindex, show, form 
dollarindex 
dropdownformoptions for form:
- :choices - choices for ActionView::Helpers::FormBuilder#select.
- :options - options for ActionView::Helpers::FormBuilder#select.
- :html_options - html_options for ActionView::Helpers::FormBuilder#select.
emailindex, show, form 
fileform 
floatindex, show, formoptions for form:
- :options - options for ActionView::Helpers::FormHelper#number_field.
hstoreindex, show, formoptions for index:
- :max - truncate text at max length.
imageshowoptions for show:
- :options - options for ActionView::Helpers::AssetTagHelper#image_tag.
inetindex, show, form 
int4rangeindex, show, form 
int8rangeindex, show, form 
integerindex, show, form 
jsonindex, show, formoptions for index:
- :max - truncate text at max length.
jsonbindex, show, formoptions for index:
- :max - truncate text at max length.
lineindex, show, formoptions for index:
- :max - truncate text at max length.
linkindex, showoptions for index, show:
- :title - title of the link.
- :html_options - options for ActionView::Helpers::UrlHelper.html#link_to
longblobindex, show, form 
longtextindex, show, formoptions for index:
- :max - truncate text at max length.
lsegindex, show, formoptions for index:
- :max - truncate text at max length.
ltreeindex, show, formoptions for index:
- :max - truncate text at max length.
macaddrindex, show, form 
markdownform 
mediumblobindex, show, form 
mediumtextindex, show, formoptions for index:
- :max - truncate text at max length.
moneyindex, show, form 
numrangeindex, show, form 
passwordindex, show, form 
percentindex 
pathindex, show, formoptions for index:
- :max - truncate text at max length.
pointindex, show, form 
polygonindex, show, formoptions for index:
- :max - truncate text at max length.
rawindex, show 
serialindex, show, form 
stiindex, show, formoptions for index:
- :max - truncate text at max length.
options for form:
- :sti_class_list - list of STI classes for user to choose.
stringindex, show, formoptions for index:
- :max - truncate text at max length.
textindex, show, formoptions for index:
- :max - truncate text at max length.
timeindex, show, form 
tinyblobindex, show, form 
tinytextindex, show, formoptions for index:
- :max - truncate text at max length.
tsrangeindex, show, form 
tstzrangeindex, show, form 
tsvectorindex, show, formoptions for index:
- :max - truncate text at max length.
unsigned_bigintindex, show, form 
unsigned_decimalindex, show, form 
unsigned_floatindex, show, form 
unsigned_integerindex, show, form 
uuidindex, show, formoptions for index:
- :max - truncate text at max length.
xmlindex, show, formoptions for index:
- :max - truncate text at max length.