管理字段::选择自定义显示值与符号

huangapple go评论62阅读模式
英文:

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

管理字段::选择自定义显示值与符号

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

管理字段::选择自定义显示值与符号

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?

英文:

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

管理字段::选择自定义显示值与符号

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

管理字段::选择自定义显示值与符号

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?

答案1

得分: 0

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: { " == ": "==", " != ": "!=", " > ": ">", " < ": "<", " >= ": ">=", " <= ": "<="}
英文:

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: { &quot; == &quot;: &quot;==&quot;, &quot; != &quot;: &quot;!=&quot;, &quot; &gt; &quot;: &quot;&gt;&quot;, &quot; &lt; &quot;: &quot;&lt;&quot;, &quot; &gt;= &quot;: &quot;&gt;=&quot;, &quot; &lt;= &quot;: &quot;&lt;=&quot;}

huangapple
  • 本文由 发表于 2023年4月17日 10:55:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76031425.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定