-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
enum.rb
52 lines (44 loc) · 1.75 KB
/
enum.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# frozen_string_literal: true
require 'rails_admin/config/fields/types/string'
module RailsAdmin
module Config
module Fields
module Types
class Enum < RailsAdmin::Config::Fields::Base
RailsAdmin::Config::Fields::Types.register(self)
register_instance_option :filter_operators do
%w[_discard] +
enum.map do |label, value|
{label: label, value: value || label}
end + (required? ? [] : %w[_separator _present _blank])
end
register_instance_option :partial do
:form_enumeration
end
register_instance_option :enum_method do
@enum_method ||= bindings[:object].class.respond_to?("#{name}_enum") || (bindings[:object] || abstract_model.model.new).respond_to?("#{name}_enum") ? "#{name}_enum" : name
end
register_instance_option :enum do
if abstract_model.model.respond_to?(enum_method)
abstract_model.model.send(enum_method)
else
(bindings[:object] || abstract_model.model.new).send(enum_method)
end
end
register_instance_option :pretty_value do
if enum.is_a?(::Hash)
enum.select { |_k, v| v.to_s == value.to_s }.keys.first.to_s.presence || value.presence || ' - '
elsif enum.is_a?(::Array) && enum.first.is_a?(::Array)
enum.detect { |e| e[1].to_s == value.to_s }.try(:first).to_s.presence || value.presence || ' - '
else
value.presence || ' - '
end
end
register_instance_option :multiple? do
properties && [:serialized].include?(properties.type)
end
end
end
end
end
end