Class: Administrate::Field::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/administrate/field/base.rb

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

#attributeObject (readonly)

Returns the value of attribute attribute.



116
117
118
# File 'lib/administrate/field/base.rb', line 116

def attribute
  @attribute
end

#dataObject (readonly)

Returns the value of attribute data.



116
117
118
# File 'lib/administrate/field/base.rb', line 116

def data
  @data
end

#optionsObject (readonly)

Returns the value of attribute options.



116
117
118
# File 'lib/administrate/field/base.rb', line 116

def options
  @options
end

#pageObject (readonly)

Returns the value of attribute page.



116
117
118
# File 'lib/administrate/field/base.rb', line 116

def page
  @page
end

#resourceObject (readonly)

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

Returns:



15
16
17
# File 'lib/administrate/field/base.rb', line 15

def self.associative?
  self < Associative
end

.eager_load?Boolean

Returns:



19
20
21
# File 'lib/administrate/field/base.rb', line 19

def self.eager_load?
  false
end

.field_typeObject



31
32
33
# File 'lib/administrate/field/base.rb', line 31

def self.field_type
  to_s.split("::").last.underscore
end

.html_classObject



11
12
13
# File 'lib/administrate/field/base.rb', line 11

def self.html_class
  field_type.dasherize
end

.local_partial_prefixesObject



48
49
50
# File 'lib/administrate/field/base.rb', line 48

def self.local_partial_prefixes
  ["fields/#{field_type}"]
end

.partial_prefixesObject



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

Returns:



23
24
25
# File 'lib/administrate/field/base.rb', line 23

def self.searchable?
  false
end

.sortable?Boolean

Returns:



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_classObject



60
61
62
# File 'lib/administrate/field/base.rb', line 60

def html_class
  self.class.html_class
end

#html_controllerObject



64
65
66
# File 'lib/administrate/field/base.rb', line 64

def html_controller
  nil
end

#nameObject



68
69
70
# File 'lib/administrate/field/base.rb', line 68

def name
  attribute.to_s
end

#partial_prefixesObject



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

#required?Boolean

Returns:



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