/
has_one.rb
35 lines (30 loc) · 928 Bytes
/
has_one.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
require_relative "associative"
module Administrate
module Field
class HasOne < Associative
def self.permitted_attribute(attr, options = nil)
associated_class_name =
if options
options.fetch(:class_name, attr.to_s.singularize.camelcase)
else
attr
end
related_dashboard_attributes =
Administrate::ResourceResolver.new("admin/#{associated_class_name}").
dashboard_class.new.permitted_attributes + [:id]
{ "#{attr}_attributes": related_dashboard_attributes }
end
def nested_form
@nested_form ||= Administrate::Page::Form.new(
resolver.dashboard_class.new,
data || resolver.resource_class.new,
)
end
private
def resolver
@resolver ||=
Administrate::ResourceResolver.new("admin/#{associated_class_name}")
end
end
end
end