Skip to content

Helper methods

Mattia Roccoberton edited this page May 2, 2023 · 2 revisions

A support class can be provided for helper methods used to present the field values. Sample:

helper_class: AdminUtils

Then define the class:

class AdminUtils < TinyAdmin::Support
  class << self
    def compact_datetime(input, options: [])
      input ? input.strftime(options&.first || '%Y-%m-%d %H:%M') : ''
    end

    def label_for(value, options: [])
      options&.first ? ::I18n.t(options.first, default: value) : value
    end
  end
end

And to use an helper method:

sections:
  - slug: posts
    name: Posts
    type: resource
    model: Post
    index:
      attributes:
        - id
        - title
        - created_at: compact_datetime, %Y%m%d
Clone this wiki locally