-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
base.rb
64 lines (51 loc) · 1.35 KB
/
base.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
require_relative "deferred"
require "active_support/core_ext/string/inflections"
module Administrate
module Field
class Base
def self.with_options(options = {})
Deferred.new(self, options)
end
def self.html_class
field_type.dasherize
end
def self.associative?
self < Associative
end
def self.searchable?
false
end
def self.field_type
to_s.split("::").last.underscore
end
def self.permitted_attribute(attr, _options = nil)
attr
end
def initialize(attribute, data, page, options = {})
@attribute = attribute
@data = data
@page = page
@resource = options.delete(:resource)
@options = options
end
def html_class
self.class.html_class
end
def name
attribute.to_s
end
def to_partial_path
"/fields/#{self.class.field_type}/#{page}"
end
def required?
return false unless resource.class.respond_to?(:validators_on)
resource.class.validators_on(attribute).any? do |v|
v.class == ActiveRecord::Validations::PresenceValidator &&
!v.options.include?(:if) &&
!v.options.include?(:unless)
end
end
attr_reader :attribute, :data, :options, :page, :resource
end
end
end