ruby-on-railsadministrate

Administrate Field::Select custom display value with symbols


I'm pretty new to both Ruby and Administrate. I have a type where I want users to choose from a dropdown of operators and display those operators as their symbols.

I was able to achieve operator symbols in the database and dropdown with this code (edited for brevity)

class CompatibilityRule < ApplicationRecord
    #...
    enum comparison: { eq: "==", ne: "!=", gt: ">", lt: "<", ge: ">=", le: "<="}
end

class CompatibilityRuleDashboard < Administrate::BaseDashboard
  ATTRIBUTE_TYPES = {
    #...
    comparison: Field::Select.with_options(searchable: false, 
      collection: CompatibilityRule.comparisons.values,
      ),
    # ...
  }.freeze

Dropdown shows operator symbols

But the operator still shows up as the key value in list views and detail pages

enter image description here

I've tried using symbols in the enum keys, but it throws the error

You tried to define an enum named "comparison" on the model "CompatibilityRule", but this will generate a class method "==", which is already defined by ActiveRecord::Relation

I've also tried looking through Administrate's issues and guides, but Field:Select doesn't appear to any other configuration for display.

Is there any way I can display the symbols everywhere the enum displayed?


Solution

  • I figured out a solution. A bit awkward, but you can add spaces to the keys. It solves the conflict with existing operators and looks correct in the UI.

    enum comparison: { " == ": "==", " != ": "!=", " > ": ">", " < ": "<", " >= ": ">=", " <= ": "<="}