Class: Madmin::Field
- Inherits:
-
Object
show all
- Defined in:
- lib/madmin/field.rb
Direct Known Subclasses
Madmin::Fields::Attachment, Madmin::Fields::Attachments, Madmin::Fields::BelongsTo, Madmin::Fields::Boolean, Madmin::Fields::Currency, Madmin::Fields::Date, Madmin::Fields::DateTime, Madmin::Fields::Decimal, Madmin::Fields::Enum, Madmin::Fields::File, Madmin::Fields::Float, Madmin::Fields::HasMany, Madmin::Fields::HasOne, Madmin::Fields::Integer, Madmin::Fields::Json, Madmin::Fields::NestedHasMany, Madmin::Fields::Password, Madmin::Fields::Polymorphic, Madmin::Fields::RichText, Madmin::Fields::Select, Madmin::Fields::String, Madmin::Fields::Text, Madmin::Fields::Time
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attribute_name:, model:, resource:, options:) ⇒ 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_name ⇒ Object
Returns the value of attribute attribute_name.
3
4
5
|
# File 'lib/madmin/field.rb', line 3
def attribute_name
@attribute_name
end
|
#model ⇒ Object
Returns the value of attribute model.
3
4
5
|
# File 'lib/madmin/field.rb', line 3
def model
@model
end
|
#options ⇒ Object
Returns the value of attribute options.
3
4
5
|
# File 'lib/madmin/field.rb', line 3
def options
@options
end
|
#resource ⇒ Object
Returns the value of attribute resource.
3
4
5
|
# File 'lib/madmin/field.rb', line 3
def resource
@resource
end
|
Class Method Details
.field_type ⇒ Object
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_attributes ⇒ Object
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
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
53
54
55
|
# File 'lib/madmin/field.rb', line 53
def searchable?
false
end
|
#to_param ⇒ Object
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
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
|