Skip to content
coezbek edited this page Dec 8, 2021 · 21 revisions

Welcome to the Wiki for the Trestle Admin project!

Full documentation is coming soon.

FAQ

Q: How to set the Favicon?

A: In config/initializers/trestle.rb set config.favicon = "favicon.ico"

Q: How to rename the title of a resource view?

A: In config/locales/en.yml set en.admin.<model>.titles.index (see issue). For instance if you model is called AuthorityCard:

# config/locales/en.yml
en:
  admin:
     authority_cards:
        breadcrumbs:
          index: "Authority Cards"
        titles:
          index: "List of Authority cards"
        table:
          headers:
            auth: "Authority"

Similarly, to rename the header_text of a column in a resource admin table, set the translation for en.admin.<model>.table.headers.<column field name> (see code).

Q: How can I remove the Add / Delete Buttons?

A: You can remove default actions (e.g. create, destroy) from a resource like so:

Trestle.resource(:articles) do
  remove_action :destroy

This will result in the delete button no longer being present on the index or show pages.

Note that if you want to remove the "New resource" button to prevent resources from being created, you should remove both :new and :create:

Trestle.resource(:articles) do
  remove_action :new, :create

Q: How can I link back to a model in the main app?

A: If you need to create a button that brings you to the resource in the "frontend" using the Rails routes you can use the follow (given model is your admin instance):

# /app/admin/model_admin.rb
unless model.new_record?
  concat content_tag(:hr)
  concat link_to("Preview", Rails.application.routes.url_helpers.model_path(model), class: "btn btn-primary", target: :_blank)
end

Q: How can I add several separate resource admins for the same ActiveRecord model?

A: To add separate admin_resources create separate admin files and pass the model (Product in the example) as the model: parameter:

# app/admin/test_product_admin.rb
Trestle.resource(:test_product, model: Product) do
...
end

# app/admin/real_product_admin.rb
Trestle.resource(:real_product, model: Product) do
...
end

Source: Issue 370

Q: How can I start with customizing the table definition?

A: When you add a table do ... end into your admin resource then the automatically generated columns will disappear. To get back to those default columns you can use the following code and copy the resulting text from your Rails log:

# /app/admin/<your model>_admin.rb
Trestle.resource(:<your model>) do
  controller do 
    def index
      print_table
    end

    def print_table
      Rails.logger.silence do
        puts "table do"
        admin.default_table_attributes.map.with_index do |attribute, index|
          case attribute.type
          when :association
            puts "  column #{attribute.association_name.inspect}, sort: false"
          else
            puts "  column #{attribute.name.inspect}#{", link: true" if index.zero?}#{", align: :center" if [:datetime, :boolean].include?(attribute.type)}"
          end
        end
        puts "  actions"
        puts "end"
      end
    end
  end
end