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.



35
36
37
38
39
40
41
# File 'lib/administrate/field/base.rb', line 35

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.



99
100
101
# File 'lib/administrate/field/base.rb', line 99

def attribute
  @attribute
end

#dataObject (readonly)

Returns the value of attribute data.



99
100
101
# File 'lib/administrate/field/base.rb', line 99

def data
  @data
end

#optionsObject (readonly)

Returns the value of attribute options.



99
100
101
# File 'lib/administrate/field/base.rb', line 99

def options
  @options
end

#pageObject (readonly)

Returns the value of attribute page.



99
100
101
# File 'lib/administrate/field/base.rb', line 99

def page
  @page
end

#resourceObject (readonly)

Returns the value of attribute resource.



99
100
101
# File 'lib/administrate/field/base.rb', line 99

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



27
28
29
# File 'lib/administrate/field/base.rb', line 27

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

.permitted_attribute(attr, _options = nil) ⇒ Object



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

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

.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



43
44
45
# File 'lib/administrate/field/base.rb', line 43

def html_class
  self.class.html_class
end

#html_controllerObject



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

def html_controller
  nil
end

#nameObject



51
52
53
# File 'lib/administrate/field/base.rb', line 51

def name
  attribute.to_s
end

#read_value(data) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/administrate/field/base.rb', line 55

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:



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/administrate/field/base.rb', line 73

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

#to_partial_pathObject



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

def to_partial_path
  "/fields/#{self.class.field_type}/#{page}"
end