Class: Administrate::Field::Base
- Inherits:
-
Object
- Object
- Administrate::Field::Base
show all
- Defined in:
- lib/administrate/field/base.rb
Direct Known Subclasses
Associative, Boolean, Date, DateTime, Email, Number, Password, RichText, Select, String, Text, Time, Url
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attribute, data, page, options = {}) ⇒ Base
Returns a new instance of Base.
52
53
54
55
56
57
58
|
# File 'lib/administrate/field/base.rb', line 52
def initialize(attribute, data, page, options = {})
@attribute = attribute
@page = page
@resource = options.delete(:resource)
@options = options
@data = read_value(data)
end
|
Instance Attribute Details
#attribute ⇒ Object
Returns the value of attribute attribute.
116
117
118
|
# File 'lib/administrate/field/base.rb', line 116
def attribute
@attribute
end
|
#data ⇒ Object
Returns the value of attribute data.
116
117
118
|
# File 'lib/administrate/field/base.rb', line 116
def data
@data
end
|
#options ⇒ Object
Returns the value of attribute options.
116
117
118
|
# File 'lib/administrate/field/base.rb', line 116
def options
@options
end
|
#page ⇒ Object
Returns the value of attribute page.
116
117
118
|
# File 'lib/administrate/field/base.rb', line 116
def page
@page
end
|
#resource ⇒ Object
Returns the value of attribute resource.
116
117
118
|
# File 'lib/administrate/field/base.rb', line 116
def resource
@resource
end
|
Class Method Details
.associative? ⇒ Boolean
15
16
17
|
# File 'lib/administrate/field/base.rb', line 15
def self.associative?
self < Associative
end
|
.eager_load? ⇒ Boolean
19
20
21
|
# File 'lib/administrate/field/base.rb', line 19
def self.eager_load?
false
end
|
.field_type ⇒ Object
31
32
33
|
# File 'lib/administrate/field/base.rb', line 31
def self.field_type
to_s.split("::").last.underscore
end
|
.html_class ⇒ Object
11
12
13
|
# File 'lib/administrate/field/base.rb', line 11
def self.html_class
field_type.dasherize
end
|
.local_partial_prefixes ⇒ Object
48
49
50
|
# File 'lib/administrate/field/base.rb', line 48
def self.local_partial_prefixes
["fields/#{field_type}"]
end
|
.partial_prefixes ⇒ Object
39
40
41
42
43
44
45
46
|
# File 'lib/administrate/field/base.rb', line 39
def self.partial_prefixes
@partial_prefixes ||=
if superclass.respond_to?(:partial_prefixes)
local_partial_prefixes + superclass.partial_prefixes
else
local_partial_prefixes
end
end
|
.permitted_attribute(attr, _options = nil) ⇒ Object
35
36
37
|
# File 'lib/administrate/field/base.rb', line 35
def self.permitted_attribute(attr, _options = nil)
attr
end
|
.searchable? ⇒ Boolean
23
24
25
|
# File 'lib/administrate/field/base.rb', line 23
def self.searchable?
false
end
|
27
28
29
|
# File 'lib/administrate/field/base.rb', line 27
def self.sortable?
true
end
|
.with_options(options = {}) ⇒ Object
7
8
9
|
# File 'lib/administrate/field/base.rb', line 7
def self.with_options(options = {})
Deferred.new(self, options)
end
|
Instance Method Details
#html_class ⇒ Object
60
61
62
|
# File 'lib/administrate/field/base.rb', line 60
def html_class
self.class.html_class
end
|
#html_controller ⇒ Object
64
65
66
|
# File 'lib/administrate/field/base.rb', line 64
def html_controller
nil
end
|
#name ⇒ Object
68
69
70
|
# File 'lib/administrate/field/base.rb', line 68
def name
attribute.to_s
end
|
#partial_prefixes ⇒ Object
86
87
88
|
# File 'lib/administrate/field/base.rb', line 86
def partial_prefixes
self.class.partial_prefixes
end
|
#read_value(data) ⇒ Object
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/administrate/field/base.rb', line 72
def read_value(data)
if options.key?(:getter)
if options[:getter].respond_to?(:call)
options[:getter].call(self)
else
resource.try(options[:getter])
end
elsif data.nil?
resource.try(attribute)
else
data
end
end
|
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/administrate/field/base.rb', line 90
def required?
return false unless resource.class.respond_to?(:validators_on)
resource.class.validators_on(attribute).any? do |v|
next false unless v.instance_of?(ActiveRecord::Validations::PresenceValidator)
options = v.options
next false if options.include?(:if)
next false if options.include?(:unless)
if (on_option = options[:on])
if on_option == :create && !resource.persisted?
next true
end
if on_option == :update && resource.persisted?
next true
end
next false
end
true
end
end
|