Class: Avo::Fields::BaseField

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::DescendantsTracker, FieldExtensions::HasFieldName
Includes:
ActionView::Helpers::UrlHelper, FieldExtensions::VisibleInDifferentViews
Defined in:
lib/avo/fields/base_field.rb

Instance Attribute Summary collapse

Attributes included from FieldExtensions::VisibleInDifferentViews

#show_on_edit, #show_on_index, #show_on_new, #show_on_show

Instance Method Summary collapse

Methods included from FieldExtensions::HasFieldName

field_name, get_field_name

Methods included from FieldExtensions::VisibleInDifferentViews

#except_on, #hide_on, #only_on, #show_on

Constructor Details

#initialize(id, _options: {}, **args, &block) ⇒ BaseField

Returns a new instance of BaseField.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/avo/fields/base_field.rb', line 45

def initialize(id, _options: {}, **args, &block)
  super(id, **args, &block)

  @id = id
  @name = args[:name]
  @translation_key = args[:translation_key]
  @translation_enabled = false
  @block = block
  @required = args[:required] || false
  @readonly = args[:readonly] || false
  @sortable = args[:sortable] || false
  @nullable = args[:nullable] || false
  @null_values = args[:null_values] || [nil, ""]
  @format_using = args[:format_using] || nil
  @placeholder = args[:placeholder]
  @help = args[:help] || nil
  @default = args[:default] || nil
  @visible = args[:visible] || true
  @as_label = args[:as_label] || false
  @as_avatar = args[:as_avatar] || false
  @as_description = args[:as_description] || false

  @updatable = true
  @computable = true
  @computed = block.present?
  @computed_value = nil

  # Set the visibility
  show_on args[:show_on] if args[:show_on].present?
  hide_on args[:hide_on] if args[:hide_on].present?
  only_on args[:only_on] if args[:only_on].present?
  except_on args[:except_on] if args[:except_on].present?
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



39
40
41
# File 'lib/avo/fields/base_field.rb', line 39

def action
  @action
end

#as_avatarObject (readonly)

Returns the value of attribute as_avatar.



26
27
28
# File 'lib/avo/fields/base_field.rb', line 26

def as_avatar
  @as_avatar
end

#as_descriptionObject (readonly)

Returns the value of attribute as_description.



27
28
29
# File 'lib/avo/fields/base_field.rb', line 27

def as_description
  @as_description
end

#as_labelObject (readonly)

Returns the value of attribute as_label.



25
26
27
# File 'lib/avo/fields/base_field.rb', line 25

def as_label
  @as_label
end

#blockObject (readonly)

Returns the value of attribute block.



15
16
17
# File 'lib/avo/fields/base_field.rb', line 15

def block
  @block
end

#computableObject (readonly)

if allowed to be computable



31
32
33
# File 'lib/avo/fields/base_field.rb', line 31

def computable
  @computable
end

#computedObject (readonly)

if block is present



32
33
34
# File 'lib/avo/fields/base_field.rb', line 32

def computed
  @computed
end

#computed_valueObject (readonly)

the value after computation



33
34
35
# File 'lib/avo/fields/base_field.rb', line 33

def computed_value
  @computed_value
end

#defaultObject (readonly)

Returns the value of attribute default.



23
24
25
# File 'lib/avo/fields/base_field.rb', line 23

def default
  @default
end

#format_usingObject (readonly)

Returns the value of attribute format_using.



21
22
23
# File 'lib/avo/fields/base_field.rb', line 21

def format_using
  @format_using
end

#helpObject (readonly)

Returns the value of attribute help.



22
23
24
# File 'lib/avo/fields/base_field.rb', line 22

def help
  @help
end

#idObject (readonly)

Returns the value of attribute id.



14
15
16
# File 'lib/avo/fields/base_field.rb', line 14

def id
  @id
end

#modelObject (readonly)

Hydrated payload



36
37
38
# File 'lib/avo/fields/base_field.rb', line 36

def model
  @model
end

#null_valuesObject (readonly)

Returns the value of attribute null_values.



20
21
22
# File 'lib/avo/fields/base_field.rb', line 20

def null_values
  @null_values
end

#nullableObject (readonly)

Returns the value of attribute nullable.



19
20
21
# File 'lib/avo/fields/base_field.rb', line 19

def nullable
  @nullable
end

#panel_nameObject (readonly)

Returns the value of attribute panel_name.



41
42
43
# File 'lib/avo/fields/base_field.rb', line 41

def panel_name
  @panel_name
end

#readonlyObject (readonly)

Returns the value of attribute readonly.



17
18
19
# File 'lib/avo/fields/base_field.rb', line 17

def readonly
  @readonly
end

#requiredObject (readonly)

Returns the value of attribute required.



16
17
18
# File 'lib/avo/fields/base_field.rb', line 16

def required
  @required
end

#resourceObject (readonly)

Returns the value of attribute resource.



38
39
40
# File 'lib/avo/fields/base_field.rb', line 38

def resource
  @resource
end

#sortableObject (readonly)

Returns the value of attribute sortable.



18
19
20
# File 'lib/avo/fields/base_field.rb', line 18

def sortable
  @sortable
end

#updatableObject (readonly)

Private options



30
31
32
# File 'lib/avo/fields/base_field.rb', line 30

def updatable
  @updatable
end

#userObject (readonly)

Returns the value of attribute user.



40
41
42
# File 'lib/avo/fields/base_field.rb', line 40

def user
  @user
end

#viewObject (readonly)

Returns the value of attribute view.



37
38
39
# File 'lib/avo/fields/base_field.rb', line 37

def view
  @view
end

#visibleObject (readonly)

Returns the value of attribute visible.



24
25
26
# File 'lib/avo/fields/base_field.rb', line 24

def visible
  @visible
end

Instance Method Details

#component_for_view(view = :index) ⇒ Object



175
176
177
# File 'lib/avo/fields/base_field.rb', line 175

def component_for_view(view = :index)
  "Avo::Fields::#{view_component_name}::#{view.to_s.camelize}Component".safe_constantize
end

#custom?Boolean

Returns:

  • (Boolean)


189
190
191
192
193
# File 'lib/avo/fields/base_field.rb', line 189

def custom?
  !method(:initialize).source_location.first.include?("lib/avo/field")
rescue
  true
end

#database_id(model) ⇒ Object

Try to see if the field has a different database ID than it's name



153
154
155
156
157
# File 'lib/avo/fields/base_field.rb', line 153

def database_id(model)
  foreign_key
rescue
  id
end

#fill_field(model, key, value, params) ⇒ Object



144
145
146
147
148
149
150
# File 'lib/avo/fields/base_field.rb', line 144

def fill_field(model, key, value, params)
  return model unless model.methods.include? key.to_sym

  model.send("#{key}=", value)

  model
end

#has_own_panel?Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/avo/fields/base_field.rb', line 159

def has_own_panel?
  false
end

#hydrate(model: nil, resource: nil, action: nil, view: nil, panel_name: nil, user: nil, translation_enabled: nil) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/avo/fields/base_field.rb', line 79

def hydrate(model: nil, resource: nil, action: nil, view: nil, panel_name: nil, user: nil, translation_enabled: nil)
  @model = model if model.present?
  @view = view if view.present?
  @resource = resource if resource.present?
  @action = action if action.present?
  @user = user if user.present?
  @panel_name = panel_name if panel_name.present?
  @translation_enabled = translation_enabled if translation_enabled.present?

  self
end

#model_errorsObject



179
180
181
182
183
# File 'lib/avo/fields/base_field.rb', line 179

def model_errors
  return {} if model.nil?

  model.errors
end

#nameObject



97
98
99
100
101
102
103
# File 'lib/avo/fields/base_field.rb', line 97

def name
  return @name if @name.present?

  return I18n.t(translation_key, count: 1).capitalize if translation_key

  @id.to_s.humanize(keep_id_suffix: true)
end

#placeholderObject



105
106
107
108
109
# File 'lib/avo/fields/base_field.rb', line 105

def placeholder
  return @placeholder if @placeholder.present?

  name
end

#resolve_attribute(value) ⇒ Object



163
164
165
# File 'lib/avo/fields/base_field.rb', line 163

def resolve_attribute(value)
  value
end

#to_permitted_paramObject



167
168
169
# File 'lib/avo/fields/base_field.rb', line 167

def to_permitted_param
  id.to_sym
end

#translation_keyObject



91
92
93
94
95
# File 'lib/avo/fields/base_field.rb', line 91

def translation_key
  return "avo.field_translations.#{@id}" if @translation_enabled

  @translation_key
end

#typeObject



185
186
187
# File 'lib/avo/fields/base_field.rb', line 185

def type
  self.class.name.demodulize.to_s.underscore.gsub("_field", "")
end

#value(property = nil) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/avo/fields/base_field.rb', line 119

def value(property = nil)
  property ||= id

  # Get model value
  final_value = @model.send(property) if (model_or_class(@model) == "model") && @model.respond_to?(property)

  if (@view === :new) || @action.present?
    final_value = if default.present? && default.respond_to?(:call)
      default.call
    else
      default
    end
  end

  # Run callback block if present
  if computable && block.present?
    final_value = instance_exec(@model, @resource, @view, self, &block)
  end

  # Run the value through resolver if present
  final_value = instance_exec(final_value, &@format_using) if @format_using.present?

  final_value
end

#view_component_nameObject



171
172
173
# File 'lib/avo/fields/base_field.rb', line 171

def view_component_name
  "#{type.camelize}Field"
end

#visible?Boolean

Returns:

  • (Boolean)


111
112
113
114
115
116
117
# File 'lib/avo/fields/base_field.rb', line 111

def visible?
  if visible.present? && visible.respond_to?(:call)
    visible.call resource: @resource
  else
    visible
  end
end