Class: Madmin::Field

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute_name:, model:, resource:, options:) ⇒ Field

Returns a new instance of Field.



9
10
11
12
13
14
# File 'lib/madmin/field.rb', line 9

def initialize(attribute_name:, model:, resource:, options:)
  @attribute_name = attribute_name.to_sym
  @model = model
  @resource = resource
  @options = options
end

Instance Attribute Details

#attribute_nameObject (readonly)

Returns the value of attribute attribute_name.



3
4
5
# File 'lib/madmin/field.rb', line 3

def attribute_name
  @attribute_name
end

#modelObject (readonly)

Returns the value of attribute model.



3
4
5
# File 'lib/madmin/field.rb', line 3

def model
  @model
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/madmin/field.rb', line 3

def options
  @options
end

#resourceObject (readonly)

Returns the value of attribute resource.



3
4
5
# File 'lib/madmin/field.rb', line 3

def resource
  @resource
end

Class Method Details

.field_typeObject



5
6
7
# File 'lib/madmin/field.rb', line 5

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

Instance Method Details

#default_index_attributesObject



45
46
47
# File 'lib/madmin/field.rb', line 45

def default_index_attributes
  [model.primary_key.to_sym, :avatar, :title, :name, :user, :created_at]
end

#required?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/madmin/field.rb', line 49

def required?
  model.validators_on(attribute_name).any? { |v| v.is_a? ActiveModel::Validations::PresenceValidator }
end

#searchable?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/madmin/field.rb', line 53

def searchable?
  false
end

#to_paramObject



28
29
30
# File 'lib/madmin/field.rb', line 28

def to_param
  attribute_name
end

#to_partial_path(name) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/madmin/field.rb', line 20

def to_partial_path(name)
  unless %w[index show form].include? name.to_s
    raise ArgumentError, "`partial` must be 'index', 'show', or 'form'"
  end

  "/madmin/fields/#{self.class.field_type}/#{name}"
end

#value(record) ⇒ Object



16
17
18
# File 'lib/madmin/field.rb', line 16

def value(record)
  record.public_send(attribute_name)
end

#visible?(action) ⇒ Boolean

Used for checking visibility of attribute on an view

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
42
43
# File 'lib/madmin/field.rb', line 33

def visible?(action)
  action = action.to_sym
  options.fetch(action) do
    case action
    when :index
      default_index_attributes.include?(attribute_name)
    else
      true
    end
  end
end