Class: Avo::Fields::BaseField
- Inherits:
-
Object
- Object
- Avo::Fields::BaseField
- Extended by:
- ActiveSupport::DescendantsTracker
- Includes:
- ActionView::Helpers::UrlHelper, Concerns::HasHelpers, Concerns::HasItemType, Concerns::IsResourceItem, Concerns::IsVisible, Concerns::VisibleInDifferentViews, Concerns::HandlesFieldArgs, Concerns::HasDefault, Concerns::HasFieldName, Concerns::HasHTMLAttributes, Concerns::IsDisabled, Concerns::IsReadonly, Concerns::IsRequired, Concerns::UseViewComponents
- Defined in:
- lib/avo/fields/base_field.rb
Direct Known Subclasses
AreaField, BadgeField, BelongsToField, BooleanField, BooleanGroupField, CodeField, CountryField, ExternalImageField, FileField, FilesField, GravatarField, HasBaseField, HeadingField, IdField, KeyValueField, LocationField, MarkdownField, PreviewField, ProgressBarField, SelectField, StatusField, TextField, TrixField
Instance Attribute Summary collapse
-
#action ⇒ Object
Returns the value of attribute action.
-
#as_avatar ⇒ Object
readonly
Returns the value of attribute as_avatar.
-
#autocomplete ⇒ Object
readonly
Returns the value of attribute autocomplete.
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#computable ⇒ Object
readonly
Private options.
-
#computed ⇒ Object
readonly
if block is present.
-
#computed_value ⇒ Object
readonly
the value after computation.
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#for_presentation_only ⇒ Object
readonly
Returns the value of attribute for_presentation_only.
-
#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.
-
#null_values ⇒ Object
readonly
Returns the value of attribute null_values.
-
#nullable ⇒ Object
readonly
Returns the value of attribute nullable.
-
#panel_name ⇒ Object
Returns the value of attribute panel_name.
-
#readonly ⇒ Object
readonly
Returns the value of attribute readonly.
-
#record ⇒ Object
Hydrated payload.
-
#required ⇒ Object
readonly
Returns the value of attribute required.
-
#sortable ⇒ Object
readonly
Returns the value of attribute sortable.
-
#stacked ⇒ Object
readonly
Returns the value of attribute stacked.
-
#user ⇒ Object
Returns the value of attribute user.
Attributes included from Concerns::IsDisabled
Attributes included from Concerns::HasHTMLAttributes
Attributes included from Concerns::VisibleInDifferentViews
#show_on_edit, #show_on_index, #show_on_new, #show_on_preview, #show_on_show
Attributes included from Concerns::IsVisible
Attributes included from Concerns::IsResourceItem
Instance Method Summary collapse
-
#assign_value(record:, value:) ⇒ Object
Used by Avo to fill the record with the default value on :new and :edit views.
- #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
- #execute_block ⇒ Object
-
#fill_field(record, key, value, params) ⇒ Object
Fills the record with the received value on create and update actions.
- #has_own_panel? ⇒ Boolean
- #hidden_in_reflection? ⇒ Boolean
-
#initialize(id, **args, &block) ⇒ BaseField
constructor
A new instance of BaseField.
-
#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.
- #options_for_filter ⇒ Object
- #placeholder ⇒ Object
- #plural_name ⇒ Object
- #record_errors ⇒ Object
- #resolve_attribute(value) ⇒ Object
- #table_header_label ⇒ Object
- #to_permitted_param ⇒ Object
- #translated_name(default:) ⇒ Object
- #translated_plural_name(default:) ⇒ Object
- #translation_key ⇒ Object
- #type ⇒ Object
- #updatable ⇒ Object
- #value(property = nil) ⇒ Object
- #visible_in_reflection? ⇒ Boolean
Methods included from Concerns::UseViewComponents
#component_for_view, #view_component_name, #view_component_namespace
Methods included from Concerns::IsRequired
Methods included from Concerns::IsDisabled
Methods included from Concerns::IsReadonly
Methods included from Concerns::HasHTMLAttributes
Methods included from Concerns::HasDefault
Methods included from Concerns::HasHelpers
Methods included from Concerns::VisibleInDifferentViews
#except_on, #hide_on, #initialize_views, #only_on, #post_initialize, #show_on, #show_on_create, #show_on_update, #visible_in_view?
Methods included from Concerns::IsVisible
Methods included from Concerns::HasItemType
#is_field?, #is_heading?, #is_main_panel?, #is_panel?, #is_row?, #is_sidebar?, #is_tab?, #is_tab_group?, #is_tool?
Methods included from Concerns::IsResourceItem
Constructor Details
#initialize(id, **args, &block) ⇒ BaseField
Returns a new instance of BaseField.
58 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 |
# File 'lib/avo/fields/base_field.rb', line 58 def initialize(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 @update_using = args[:update_using] || nil @placeholder = args[:placeholder] @autocomplete = args[:autocomplete] || nil @help = args[:help] || nil @default = args[:default] || nil @visible = args[:visible] @as_avatar = args[:as_avatar] || false @html = args[:html] || nil @view = Avo::ViewInquirer.new(args[:view]) || nil @value = args[:value] || nil @stacked = args[:stacked] || nil @for_presentation_only = args[:for_presentation_only] || false @resource = args[:resource] @components = args[:components] || {} @args = args @computable = true @computed = block.present? @computed_value = nil post_initialize if respond_to?(:post_initialize) end |
Instance Attribute Details
#action ⇒ Object
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.
41 42 43 |
# File 'lib/avo/fields/base_field.rb', line 41 def as_avatar @as_avatar end |
#autocomplete ⇒ Object (readonly)
Returns the value of attribute autocomplete.
38 39 40 |
# File 'lib/avo/fields/base_field.rb', line 38 def autocomplete @autocomplete end |
#block ⇒ Object (readonly)
Returns the value of attribute block.
31 32 33 |
# File 'lib/avo/fields/base_field.rb', line 31 def block @block end |
#computable ⇒ Object (readonly)
Private options
46 47 48 |
# File 'lib/avo/fields/base_field.rb', line 46 def computable @computable end |
#computed ⇒ Object (readonly)
if block is present
47 48 49 |
# File 'lib/avo/fields/base_field.rb', line 47 def computed @computed end |
#computed_value ⇒ Object (readonly)
the value after computation
48 49 50 |
# File 'lib/avo/fields/base_field.rb', line 48 def computed_value @computed_value end |
#default ⇒ Object (readonly)
Returns the value of attribute default.
40 41 42 |
# File 'lib/avo/fields/base_field.rb', line 40 def default @default end |
#for_presentation_only ⇒ Object (readonly)
Returns the value of attribute for_presentation_only.
43 44 45 |
# File 'lib/avo/fields/base_field.rb', line 43 def for_presentation_only @for_presentation_only end |
#format_using ⇒ Object (readonly)
Returns the value of attribute format_using.
37 38 39 |
# File 'lib/avo/fields/base_field.rb', line 37 def format_using @format_using end |
#help ⇒ Object (readonly)
Returns the value of attribute help.
39 40 41 |
# File 'lib/avo/fields/base_field.rb', line 39 def help @help end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
30 31 32 |
# File 'lib/avo/fields/base_field.rb', line 30 def id @id end |
#null_values ⇒ Object (readonly)
Returns the value of attribute null_values.
36 37 38 |
# File 'lib/avo/fields/base_field.rb', line 36 def null_values @null_values end |
#nullable ⇒ Object (readonly)
Returns the value of attribute nullable.
35 36 37 |
# File 'lib/avo/fields/base_field.rb', line 35 def nullable @nullable end |
#panel_name ⇒ Object
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.
33 34 35 |
# File 'lib/avo/fields/base_field.rb', line 33 def readonly @readonly end |
#record ⇒ Object
Hydrated payload
51 52 53 |
# File 'lib/avo/fields/base_field.rb', line 51 def record @record end |
#required ⇒ Object (readonly)
Returns the value of attribute required.
32 33 34 |
# File 'lib/avo/fields/base_field.rb', line 32 def required @required end |
#sortable ⇒ Object (readonly)
Returns the value of attribute sortable.
34 35 36 |
# File 'lib/avo/fields/base_field.rb', line 34 def sortable @sortable end |
#stacked ⇒ Object (readonly)
Returns the value of attribute stacked.
42 43 44 |
# File 'lib/avo/fields/base_field.rb', line 42 def stacked @stacked end |
#user ⇒ Object
Returns the value of attribute user.
53 54 55 |
# File 'lib/avo/fields/base_field.rb', line 53 def user @user end |
Instance Method Details
#assign_value(record:, value:) ⇒ Object
Used by Avo to fill the record with the default value on :new and :edit views
262 263 264 265 266 267 268 |
# File 'lib/avo/fields/base_field.rb', line 262 def assign_value(record:, value:) id = type == "belongs_to" ? foreign_key : database_id if record.send(id).nil? record.send("#{id}=", value) end end |
#custom? ⇒ Boolean
239 240 241 242 243 |
# File 'lib/avo/fields/base_field.rb', line 239 def custom? !method(:initialize).source_location.first.include?("lib/avo/field") rescue true end |
#custom_name? ⇒ Boolean
134 135 136 |
# File 'lib/avo/fields/base_field.rb', line 134 def custom_name? !@name.nil? end |
#database_id ⇒ Object
Try to see if the field has a different database ID than it’s name
213 214 215 216 217 |
# File 'lib/avo/fields/base_field.rb', line 213 def database_id foreign_key rescue id end |
#default_name ⇒ Object
138 139 140 |
# File 'lib/avo/fields/base_field.rb', line 138 def default_name @id.to_s.humanize(keep_id_suffix: true) end |
#execute_block ⇒ Object
180 181 182 183 184 185 186 187 188 189 |
# File 'lib/avo/fields/base_field.rb', line 180 def execute_block Avo::ExecutionContext.new( target: block, record: record, resource: resource, view: view, field: self, include: self.class.included_modules ).handle end |
#fill_field(record, key, value, params) ⇒ Object
Fills the record with the received value on create and update actions.
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/avo/fields/base_field.rb', line 192 def fill_field(record, key, value, params) return record unless record.methods.include? key.to_sym if @update_using.present? value = Avo::ExecutionContext.new( target: @update_using, record: record, key: key, value: value, resource: resource, field: self, include: self.class.included_modules ).handle end record.public_send("#{key}=", value) record end |
#has_own_panel? ⇒ Boolean
219 220 221 |
# File 'lib/avo/fields/base_field.rb', line 219 def has_own_panel? false end |
#hidden_in_reflection? ⇒ Boolean
249 250 251 |
# File 'lib/avo/fields/base_field.rb', line 249 def hidden_in_reflection? !visible_in_reflection? 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
110 111 112 113 114 115 116 117 118 |
# File 'lib/avo/fields/base_field.rb', line 110 def name return @name if custom_name? if translation_key translated_name default: default_name else default_name end end |
#options_for_filter ⇒ Object
253 254 255 |
# File 'lib/avo/fields/base_field.rb', line 253 def end |
#placeholder ⇒ Object
142 143 144 |
# File 'lib/avo/fields/base_field.rb', line 142 def placeholder Avo::ExecutionContext.new(target: @placeholder || name, record: record, resource: @resource, view: @view).handle end |
#plural_name ⇒ Object
120 121 122 123 124 125 126 127 128 |
# File 'lib/avo/fields/base_field.rb', line 120 def plural_name default = name.pluralize if translation_key translated_plural_name default: default else default end end |
#record_errors ⇒ Object
231 232 233 |
# File 'lib/avo/fields/base_field.rb', line 231 def record_errors record.nil? ? {} : record.errors end |
#resolve_attribute(value) ⇒ Object
223 224 225 |
# File 'lib/avo/fields/base_field.rb', line 223 def resolve_attribute(value) value end |
#table_header_label ⇒ Object
130 131 132 |
# File 'lib/avo/fields/base_field.rb', line 130 def table_header_label name end |
#to_permitted_param ⇒ Object
227 228 229 |
# File 'lib/avo/fields/base_field.rb', line 227 def to_permitted_param id.to_sym end |
#translated_name(default:) ⇒ Object
98 99 100 |
# File 'lib/avo/fields/base_field.rb', line 98 def translated_name(default:) t(translation_key, count: 1, default: default).humanize end |
#translated_plural_name(default:) ⇒ Object
102 103 104 |
# File 'lib/avo/fields/base_field.rb', line 102 def translated_plural_name(default:) t(translation_key, count: 2, default: default).humanize end |
#translation_key ⇒ Object
94 95 96 |
# File 'lib/avo/fields/base_field.rb', line 94 def translation_key @translation_key || "avo.field_translations.#{@id}" end |
#type ⇒ Object
235 236 237 |
# File 'lib/avo/fields/base_field.rb', line 235 def type self.class.name.demodulize.to_s.underscore.gsub("_field", "") end |
#updatable ⇒ Object
257 258 259 |
# File 'lib/avo/fields/base_field.rb', line 257 def updatable !is_disabled? && visible? end |
#value(property = nil) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/avo/fields/base_field.rb', line 146 def value(property = nil) return @value if @value.present? property ||= id # Get record value final_value = record.send(property) if is_model?(record) && record.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 = execute_block end # Run the value through resolver if present if format_using.present? final_value = Avo::ExecutionContext.new( target: format_using, value: final_value, record: record, resource: resource, view: view, field: self, include: self.class.included_modules ).handle end final_value end |
#visible_in_reflection? ⇒ Boolean
245 246 247 |
# File 'lib/avo/fields/base_field.rb', line 245 def visible_in_reflection? true end |