英文:
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: { " == ": "==", " != ": "!=", " > ": ">", " < ": "<", " >= ": ">=", " <= ": "<="}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论