Class: Avo::Fields::BaseField
- Inherits:
-
Object
- Object
- Avo::Fields::BaseField
- Extended by:
- ActiveSupport::DescendantsTracker, FieldExtensions::HasFieldName
- Includes:
- ActionView::Helpers::UrlHelper, Concerns::HandlesFieldArgs, Concerns::HasHTMLAttributes, Concerns::IsResourceItem, Concerns::HasDefault, Concerns::IsDisabled, Concerns::IsReadonly, Concerns::IsRequired, FieldExtensions::VisibleInDifferentViews
- Defined in:
- lib/avo/fields/base_field.rb
Direct Known Subclasses
BadgeField, BelongsToField, BooleanField, BooleanGroupField, CodeField, CountryField, ExternalImageField, FileField, FilesField, GravatarField, HasBaseField, HeadingField, IdField, KeyValueField, MarkdownField, ProgressBarField, SelectField, StatusField, TagsField, TextField, TrixField
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
Returns the value of attribute action.
-
#as_avatar ⇒ Object
readonly
Returns the value of attribute as_avatar.
-
#as_description ⇒ Object
readonly
Returns the value of attribute as_description.
-
#as_label ⇒ Object
readonly
Returns the value of attribute as_label.
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#computable ⇒ Object
readonly
if allowed to be computable.
-
#computed ⇒ Object
readonly
if block is present.
-
#computed_value ⇒ Object
readonly
the value after computation.
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#format_using ⇒ Object
readonly
Returns the value of attribute format_using.
-
#help ⇒ Object
readonly
Returns the value of attribute help.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#index_text_align ⇒ Object
readonly
Returns the value of attribute index_text_align.
-
#model ⇒ Object
readonly
Hydrated payload.
-
#null_values ⇒ Object
readonly
Returns the value of attribute null_values.
-
#nullable ⇒ Object
readonly
Returns the value of attribute nullable.
-
#panel_name ⇒ Object
readonly
Returns the value of attribute panel_name.
-
#readonly ⇒ Object
readonly
Returns the value of attribute readonly.
-
#required ⇒ Object
readonly
Returns the value of attribute required.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
-
#sortable ⇒ Object
readonly
Returns the value of attribute sortable.
-
#stacked ⇒ Object
readonly
Returns the value of attribute stacked.
-
#updatable ⇒ Object
readonly
Private options.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
-
#view ⇒ Object
readonly
Returns the value of attribute view.
-
#visible ⇒ Object
readonly
Returns the value of attribute visible.
Attributes included from Concerns::IsDisabled
Attributes included from Concerns::HasHTMLAttributes
Attributes included from FieldExtensions::VisibleInDifferentViews
#show_on_edit, #show_on_index, #show_on_new, #show_on_show
Instance Method Summary collapse
-
#component_for_view(view = :index) ⇒ Object
Try and build the component class or fallback to a blank one.
- #custom? ⇒ Boolean
- #custom_name? ⇒ Boolean
-
#database_id ⇒ Object
Try to see if the field has a different database ID than it's name.
- #default_name ⇒ Object
- #fill_field(model, key, value, params) ⇒ Object
- #has_own_panel? ⇒ Boolean
- #hidden_in_reflection? ⇒ Boolean
- #hydrate(model: nil, resource: nil, action: nil, view: nil, panel_name: nil, user: nil) ⇒ Object
-
#initialize(id, **args, &block) ⇒ BaseField
constructor
A new instance of BaseField.
- #model_errors ⇒ Object
-
#name ⇒ Object
Getting the name of the resource (user/users, post/posts) We'll first check to see if the user passed a name Secondly we'll try to find a translation key We'll fallback to humanizing the id.
- #placeholder ⇒ Object
- #plural_name ⇒ Object
- #resolve_attribute(value) ⇒ Object
- #to_permitted_param ⇒ Object
- #translation_key ⇒ Object
- #type ⇒ Object
- #value(property = nil) ⇒ Object
- #view_component_name ⇒ Object
- #visible? ⇒ Boolean
- #visible_in_reflection? ⇒ Boolean
Methods included from FieldExtensions::HasFieldName
Methods included from Concerns::HasDefault
Methods included from Concerns::IsDisabled
Methods included from Concerns::IsReadonly
Methods included from Concerns::IsRequired
Methods included from Concerns::HasHTMLAttributes
Methods included from FieldExtensions::VisibleInDifferentViews
#except_on, #hide_on, #only_on, #show_on, #show_on_create, #show_on_update, #visible_on?
Methods included from Concerns::IsResourceItem
#is_field?, #is_main_panel?, #is_panel?, #is_sidebar?, #is_tab?, #is_tab_group?, #is_tool?
Constructor Details
#initialize(id, **args, &block) ⇒ BaseField
Returns a new instance of BaseField.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 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 98 |
# File 'lib/avo/fields/base_field.rb', line 59 def initialize(id, **args, &block) super(id, **args, &block) @id = id @name = args[:name] @translation_key = args[:translation_key] @block = block @required = args.dig(:required) # Value if :required present on args, nil otherwise @readonly = args[:readonly] || false @disabled = args[:disabled] || 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 @index_text_align = args[:index_text_align] || :left @html = args[:html] || nil @view = args[:view] || nil @value = args[:value] || nil @stacked = args[:stacked] || nil @args = args @updatable = !readonly @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
#action ⇒ Object (readonly)
Returns the value of attribute action.
52 53 54 |
# File 'lib/avo/fields/base_field.rb', line 52 def action @action end |
#as_avatar ⇒ Object (readonly)
Returns the value of attribute as_avatar.
37 38 39 |
# File 'lib/avo/fields/base_field.rb', line 37 def as_avatar @as_avatar end |
#as_description ⇒ Object (readonly)
Returns the value of attribute as_description.
38 39 40 |
# File 'lib/avo/fields/base_field.rb', line 38 def as_description @as_description end |
#as_label ⇒ Object (readonly)
Returns the value of attribute as_label.
36 37 38 |
# File 'lib/avo/fields/base_field.rb', line 36 def as_label @as_label end |
#block ⇒ Object (readonly)
Returns the value of attribute block.
26 27 28 |
# File 'lib/avo/fields/base_field.rb', line 26 def block @block end |
#computable ⇒ Object (readonly)
if allowed to be computable
44 45 46 |
# File 'lib/avo/fields/base_field.rb', line 44 def computable @computable end |
#computed ⇒ Object (readonly)
if block is present
45 46 47 |
# File 'lib/avo/fields/base_field.rb', line 45 def computed @computed end |
#computed_value ⇒ Object (readonly)
the value after computation
46 47 48 |
# File 'lib/avo/fields/base_field.rb', line 46 def computed_value @computed_value end |
#default ⇒ Object (readonly)
Returns the value of attribute default.
34 35 36 |
# File 'lib/avo/fields/base_field.rb', line 34 def default @default end |
#format_using ⇒ Object (readonly)
Returns the value of attribute format_using.
32 33 34 |
# File 'lib/avo/fields/base_field.rb', line 32 def format_using @format_using end |
#help ⇒ Object (readonly)
Returns the value of attribute help.
33 34 35 |
# File 'lib/avo/fields/base_field.rb', line 33 def help @help end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
25 26 27 |
# File 'lib/avo/fields/base_field.rb', line 25 def id @id end |
#index_text_align ⇒ Object (readonly)
Returns the value of attribute index_text_align.
39 40 41 |
# File 'lib/avo/fields/base_field.rb', line 39 def index_text_align @index_text_align end |
#model ⇒ Object (readonly)
Hydrated payload
49 50 51 |
# File 'lib/avo/fields/base_field.rb', line 49 def model @model end |
#null_values ⇒ Object (readonly)
Returns the value of attribute null_values.
31 32 33 |
# File 'lib/avo/fields/base_field.rb', line 31 def null_values @null_values end |
#nullable ⇒ Object (readonly)
Returns the value of attribute nullable.
30 31 32 |
# File 'lib/avo/fields/base_field.rb', line 30 def nullable @nullable end |
#panel_name ⇒ Object (readonly)
Returns the value of attribute panel_name.
54 55 56 |
# File 'lib/avo/fields/base_field.rb', line 54 def panel_name @panel_name end |
#readonly ⇒ Object (readonly)
Returns the value of attribute readonly.
28 29 30 |
# File 'lib/avo/fields/base_field.rb', line 28 def readonly @readonly end |
#required ⇒ Object (readonly)
Returns the value of attribute required.
27 28 29 |
# File 'lib/avo/fields/base_field.rb', line 27 def required @required end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
51 52 53 |
# File 'lib/avo/fields/base_field.rb', line 51 def resource @resource end |
#sortable ⇒ Object (readonly)
Returns the value of attribute sortable.
29 30 31 |
# File 'lib/avo/fields/base_field.rb', line 29 def sortable @sortable end |
#stacked ⇒ Object (readonly)
Returns the value of attribute stacked.
40 41 42 |
# File 'lib/avo/fields/base_field.rb', line 40 def stacked @stacked end |
#updatable ⇒ Object (readonly)
Private options
43 44 45 |
# File 'lib/avo/fields/base_field.rb', line 43 def updatable @updatable end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
53 54 55 |
# File 'lib/avo/fields/base_field.rb', line 53 def user @user end |
#view ⇒ Object (readonly)
Returns the value of attribute view.
50 51 52 |
# File 'lib/avo/fields/base_field.rb', line 50 def view @view end |
#visible ⇒ Object (readonly)
Returns the value of attribute visible.
35 36 37 |
# File 'lib/avo/fields/base_field.rb', line 35 def visible @visible end |
Instance Method Details
#component_for_view(view = :index) ⇒ Object
Try and build the component class or fallback to a blank one
221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/avo/fields/base_field.rb', line 221 def component_for_view(view = :index) # Use the edit variant for all "update" views view = :edit if view.in? [:new, :create, :update] component_class = "::Avo::Fields::#{view_component_name}::#{view.to_s.camelize}Component" component_class.constantize rescue # When returning nil, a race condition happens and throws an error in some environments. # See https://github.com/avo-hq/avo/pull/365 ::Avo::BlankFieldComponent end |
#custom? ⇒ Boolean
243 244 245 246 247 |
# File 'lib/avo/fields/base_field.rb', line 243 def custom? !method(:initialize).source_location.first.include?("lib/avo/field") rescue true end |
#custom_name? ⇒ Boolean
141 142 143 |
# File 'lib/avo/fields/base_field.rb', line 141 def custom_name? @name.present? end |
#database_id ⇒ Object
Try to see if the field has a different database ID than it's name
198 199 200 201 202 |
# File 'lib/avo/fields/base_field.rb', line 198 def database_id foreign_key rescue id end |
#default_name ⇒ Object
145 146 147 |
# File 'lib/avo/fields/base_field.rb', line 145 def default_name @id.to_s.humanize(keep_id_suffix: true) end |
#fill_field(model, key, value, params) ⇒ Object
189 190 191 192 193 194 195 |
# File 'lib/avo/fields/base_field.rb', line 189 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
204 205 206 |
# File 'lib/avo/fields/base_field.rb', line 204 def has_own_panel? false end |
#hidden_in_reflection? ⇒ Boolean
253 254 255 |
# File 'lib/avo/fields/base_field.rb', line 253 def hidden_in_reflection? !visible_in_reflection? end |
#hydrate(model: nil, resource: nil, action: nil, view: nil, panel_name: nil, user: nil) ⇒ Object
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/avo/fields/base_field.rb', line 100 def hydrate(model: nil, resource: nil, action: nil, view: nil, panel_name: nil, user: 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? self end |
#model_errors ⇒ Object
233 234 235 236 237 |
# File 'lib/avo/fields/base_field.rb', line 233 def model_errors return {} if model.nil? model.errors end |
#name ⇒ Object
Getting the name of the resource (user/users, post/posts) We'll first check to see if the user passed a name Secondly we'll try to find a translation key We'll fallback to humanizing the id
121 122 123 124 125 126 127 128 129 |
# File 'lib/avo/fields/base_field.rb', line 121 def name return @name if custom_name? if translation_key && ::Avo::App.translation_enabled t(translation_key, count: 1, default: default_name).capitalize else default_name end end |
#placeholder ⇒ Object
149 150 151 152 153 154 155 |
# File 'lib/avo/fields/base_field.rb', line 149 def placeholder if @placeholder.respond_to?(:call) return Avo::Hosts::ResourceViewRecordHost.new(block: @placeholder, record: @model, resource: @resource, view: @view).handle end @placeholder || name end |
#plural_name ⇒ Object
131 132 133 134 135 136 137 138 139 |
# File 'lib/avo/fields/base_field.rb', line 131 def plural_name default = name.pluralize if translation_key && ::Avo::App.translation_enabled t(translation_key, count: 2, default: default).capitalize else default end end |
#resolve_attribute(value) ⇒ Object
208 209 210 |
# File 'lib/avo/fields/base_field.rb', line 208 def resolve_attribute(value) value end |
#to_permitted_param ⇒ Object
212 213 214 |
# File 'lib/avo/fields/base_field.rb', line 212 def to_permitted_param id.to_sym end |
#translation_key ⇒ Object
111 112 113 114 115 |
# File 'lib/avo/fields/base_field.rb', line 111 def translation_key return @translation_key if @translation_key.present? "avo.field_translations.#{@id}" end |
#type ⇒ Object
239 240 241 |
# File 'lib/avo/fields/base_field.rb', line 239 def type self.class.name.demodulize.to_s.underscore.gsub("_field", "") end |
#value(property = nil) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/avo/fields/base_field.rb', line 165 def value(property = nil) return @value if @value.present? property ||= id # Get model value final_value = @model.send(property) if is_model?(@model) && @model.respond_to?(property) # On new views and actions modals we need to prefill the fields with the default value if should_fill_with_default_value? && default.present? final_value = computed_default_value end # Run computable 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_name ⇒ Object
216 217 218 |
# File 'lib/avo/fields/base_field.rb', line 216 def view_component_name "#{type.camelize}Field" end |
#visible? ⇒ Boolean
157 158 159 160 161 162 163 |
# File 'lib/avo/fields/base_field.rb', line 157 def visible? if visible.present? && visible.respond_to?(:call) visible.call resource: @resource else visible end end |
#visible_in_reflection? ⇒ Boolean
249 250 251 |
# File 'lib/avo/fields/base_field.rb', line 249 def visible_in_reflection? true end |