-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
polymorphic_association.rb
85 lines (70 loc) · 2.57 KB
/
polymorphic_association.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
require 'rails_admin/config/fields/types/belongs_to_association'
module RailsAdmin
module Config
module Fields
module Types
class PolymorphicAssociation < RailsAdmin::Config::Fields::Types::BelongsToAssociation
# Register field type for the type loader
RailsAdmin::Config::Fields::Types.register(self)
register_instance_option :partial do
:form_polymorphic_association
end
# Accessor whether association is visible or not. By default
# association checks that any of the child models are included in
# configuration.
register_instance_option :visible? do
associated_model_config.any?
end
register_instance_option :formatted_value do
(o = value) && o.send(RailsAdmin.config(o).object_label_method)
end
register_instance_option :sortable do
false
end
register_instance_option :searchable do
false
end
# TODO: not supported yet
register_instance_option :associated_collection_cache_all do
false
end
# TODO: not supported yet
register_instance_option :associated_collection_scope do
nil
end
register_instance_option :allowed_methods do
[children_fields]
end
register_instance_option :eager_load? do
false
end
def associated_collection(type)
return [] if type.blank?
config = RailsAdmin.config(type)
config.abstract_model.all.collect do |object|
[object.send(config.object_label_method), object.id]
end
end
def associated_model_config
@associated_model_config ||= association.klass.collect { |type| RailsAdmin.config(type) }.select { |config| !config.excluded? }
end
def polymorphic_type_collection
associated_model_config.collect do |config|
[config.label, config.abstract_model.model.name]
end
end
def polymorphic_type_urls
types = associated_model_config.collect do |config|
[config.abstract_model.model.name, config.abstract_model.to_param]
end
::Hash[*types.collect { |v| [v[0], bindings[:view].index_path(v[1])] }.flatten]
end
# Reader for field's value
def value
bindings[:object].send(association.name)
end
end
end
end
end
end