How can I apply syntax highlighting to an ActiveAdmin row?

489 views Asked by At

I'm storing Ruby code in a database so that if an administrator needs to tweak the code logic, they use ActiveAdmin to make edits. My app is written in such a way to load & call this code from the database.

My problem now is that the text rendered in the ActiveAdmin UI show page is not very readable code because it is not monospaced or syntax highlighted. How can I apply syntax highlighting to this particular row?

Would you use the Rouge gem? If so, can you wrap your code or do you need to monkey-patch ActiveAdmin? What other solutions exist?

ActiveAdmin.register Model do
  show do |model|
    attributes_table do
      row :name
      row :logic # render this logic text as Ruby code
      row :created_at
      row :updated_at
    end
  end
end

enter image description here

1

There are 1 answers

2
sevensidedmarble On BEST ANSWER

You should be able to use the Arbe syntax to wrap it in a pre or code tag:

ActiveAdmin.register Model do
  show do |model|
    attributes_table do
      row :name
      row :logic, do |logic|
        pre( code(model.logic, class: 'lang-ruby') )
      end
      row :created_at
      row :updated_at
    end
  end
end

You could also use the code tag instead of pre, they're just slightly different. Either way any more serious customization should be done with CSS.

Original Poster's Note: wrap the code block with pre if using highlight.js.