Class: Avo::Fields::Field
- Inherits:
-
Object
- Object
- Avo::Fields::Field
- Extended by:
- Avo::Fields::FieldExtensions::HasFieldName
- Defined in:
- lib/avo/app/fields/field.rb
Direct Known Subclasses
BadgeField, BelongsToField, BooleanField, BooleanGroupField, CodeField, CountryField, CurrencyField, FileField, FilesField, GravatarField, HasAndBelongsToManyField, HasManyField, HasOneField, HeadingField, IdField, KeyValueField, SelectField, StatusField, TextField, TrixField
Instance Attribute Summary collapse
-
#block ⇒ Object
Returns the value of attribute block.
-
#component ⇒ Object
Returns the value of attribute component.
-
#computable ⇒ Object
Returns the value of attribute computable.
-
#default ⇒ Object
Returns the value of attribute default.
-
#format_using ⇒ Object
Returns the value of attribute format_using.
-
#help ⇒ Object
Returns the value of attribute help.
-
#id ⇒ Object
Returns the value of attribute id.
-
#is_array_param ⇒ Object
Returns the value of attribute is_array_param.
-
#is_object_param ⇒ Object
Returns the value of attribute is_object_param.
-
#name ⇒ Object
Returns the value of attribute name.
-
#null_values ⇒ Object
Returns the value of attribute null_values.
-
#nullable ⇒ Object
Returns the value of attribute nullable.
-
#placeholder ⇒ Object
Returns the value of attribute placeholder.
-
#readonly ⇒ Object
Returns the value of attribute readonly.
-
#required ⇒ Object
Returns the value of attribute required.
-
#sortable ⇒ Object
Returns the value of attribute sortable.
-
#updatable ⇒ Object
Returns the value of attribute updatable.
Attributes included from Avo::Fields::FieldExtensions::VisibleOnDifferentViews
#show_on_create, #show_on_edit, #show_on_index, #show_on_show
Instance Method Summary collapse
-
#database_id(model) ⇒ Object
Try to see if the field has a different database ID than it’s name.
- #fetch_for_action(model, resource) ⇒ Object
- #fetch_for_resource(model, resource, view) ⇒ Object
- #fill_field(model, key, value) ⇒ Object
- #has_own_panel? ⇒ Boolean
- #hydrate_field(fields, model, resource) ⇒ Object
-
#initialize(id, **args, &block) ⇒ Field
constructor
A new instance of Field.
- #resolve_attribute(value) ⇒ Object
Methods included from Avo::Fields::FieldExtensions::HasFieldName
field_name, field_name_attribute, get_field_name
Methods included from Avo::Fields::FieldExtensions::VisibleOnDifferentViews
#except_on, #hide_on, #only_on, #show_on
Constructor Details
#initialize(id, **args, &block) ⇒ Field
Returns a new instance of Field.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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 |
# File 'lib/avo/app/fields/field.rb', line 28 def initialize(id, **args, &block) super(id, **args, &block) @defaults ||= {} args = @defaults.merge(args).symbolize_keys null_values = [nil, '', *args[:null_values]] # The field properties as a hash {property: default_value} @field_properties = { id: id, name: id.to_s.humanize, block: block, component: 'field', required: false, readonly: false, updatable: true, sortable: false, nullable: false, null_values: null_values, computable: true, is_array_param: false, format_using: false, placeholder: id.to_s.camelize, help: nil, default: nil, } # Set the values in the following order # - app defaults # - field defaults # - field option @field_properties.each do |name, default_value| final_value = args[name.to_sym] self.send("#{name}=", name != 'null_values' && (final_value.nil? || !defined?(final_value)) ? default_value : final_value) end # 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
#block ⇒ Object
Returns the value of attribute block.
23 24 25 |
# File 'lib/avo/app/fields/field.rb', line 23 def block @block end |
#component ⇒ Object
Returns the value of attribute component.
12 13 14 |
# File 'lib/avo/app/fields/field.rb', line 12 def component @component end |
#computable ⇒ Object
Returns the value of attribute computable.
20 21 22 |
# File 'lib/avo/app/fields/field.rb', line 20 def computable @computable end |
#default ⇒ Object
Returns the value of attribute default.
26 27 28 |
# File 'lib/avo/app/fields/field.rb', line 26 def default @default end |
#format_using ⇒ Object
Returns the value of attribute format_using.
19 20 21 |
# File 'lib/avo/app/fields/field.rb', line 19 def format_using @format_using end |
#help ⇒ Object
Returns the value of attribute help.
25 26 27 |
# File 'lib/avo/app/fields/field.rb', line 25 def help @help end |
#id ⇒ Object
Returns the value of attribute id.
10 11 12 |
# File 'lib/avo/app/fields/field.rb', line 10 def id @id end |
#is_array_param ⇒ Object
Returns the value of attribute is_array_param.
21 22 23 |
# File 'lib/avo/app/fields/field.rb', line 21 def is_array_param @is_array_param end |
#is_object_param ⇒ Object
Returns the value of attribute is_object_param.
22 23 24 |
# File 'lib/avo/app/fields/field.rb', line 22 def is_object_param @is_object_param end |
#name ⇒ Object
Returns the value of attribute name.
11 12 13 |
# File 'lib/avo/app/fields/field.rb', line 11 def name @name end |
#null_values ⇒ Object
Returns the value of attribute null_values.
18 19 20 |
# File 'lib/avo/app/fields/field.rb', line 18 def null_values @null_values end |
#nullable ⇒ Object
Returns the value of attribute nullable.
17 18 19 |
# File 'lib/avo/app/fields/field.rb', line 17 def nullable @nullable end |
#placeholder ⇒ Object
Returns the value of attribute placeholder.
24 25 26 |
# File 'lib/avo/app/fields/field.rb', line 24 def placeholder @placeholder end |
#readonly ⇒ Object
Returns the value of attribute readonly.
16 17 18 |
# File 'lib/avo/app/fields/field.rb', line 16 def readonly @readonly end |
#required ⇒ Object
Returns the value of attribute required.
15 16 17 |
# File 'lib/avo/app/fields/field.rb', line 15 def required @required end |
#sortable ⇒ Object
Returns the value of attribute sortable.
14 15 16 |
# File 'lib/avo/app/fields/field.rb', line 14 def sortable @sortable end |
#updatable ⇒ Object
Returns the value of attribute updatable.
13 14 15 |
# File 'lib/avo/app/fields/field.rb', line 13 def updatable @updatable end |
Instance Method Details
#database_id(model) ⇒ Object
Try to see if the field has a different database ID than it’s name
168 169 170 171 172 173 174 |
# File 'lib/avo/app/fields/field.rb', line 168 def database_id(model) begin foreign_key(model) rescue => exception id end end |
#fetch_for_action(model, resource) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/avo/app/fields/field.rb', line 110 def fetch_for_action(model, resource) fields = { id: id, # computed: block.present?, } # Fill the properties with values @field_properties.each do |name, value| fields[name] = self.send(name) end # Set initial value # fields[:value] = model.send(id) if model_or_class(model) == 'model' and model.methods.include? id # Set default value for create view if fields[:default].present? and fields[:default].respond_to? :call fields[:value] = fields[:default].call model, resource, self else fields[:value] = fields[:default] end # Run callback block if present # if computable and @block.present? # fields[:computed_value] = @block.call model, resource, self # fields[:value] = fields[:computed_value] # end # Run each field's custom hydration fields.merge! self.hydrate_field(fields, model, resource, :create) # Run the value through resolver if present fields[:value] = @format_using.call fields[:value] if @format_using.present? fields end |
#fetch_for_resource(model, resource, view) ⇒ Object
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 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/avo/app/fields/field.rb', line 71 def fetch_for_resource(model, resource, view) fields = { id: id, computed: block.present?, } # Fill the properties with values @field_properties.each do |name, value| fields[name] = self.send(name) end # Set model value fields[:value] = model.send(id) if model_or_class(model) == 'model' and model.methods.include? id # Set default value for create view if view === :create if fields[:default].present? and fields[:default].respond_to? :call fields[:value] = fields[:default].call model, resource, view, self else fields[:value] = fields[:default] end end # Run callback block if present if computable and @block.present? fields[:computed_value] = @block.call model, resource, view, self fields[:value] = fields[:computed_value] end # Run each field's custom hydration fields.merge! self.hydrate_field(fields, model, resource, view) # Run the value through resolver if present fields[:value] = @format_using.call fields[:value] if @format_using.present? fields end |
#fill_field(model, key, value) ⇒ Object
159 160 161 162 163 164 165 |
# File 'lib/avo/app/fields/field.rb', line 159 def fill_field(model, key, value) return model unless model.methods.include? key.to_sym model.send("#{key}=", value) model end |
#has_own_panel? ⇒ Boolean
176 177 178 |
# File 'lib/avo/app/fields/field.rb', line 176 def has_own_panel? false end |
#hydrate_field(fields, model, resource) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/avo/app/fields/field.rb', line 147 def hydrate_field(fields, model, resource) final_value = fields[:value] if fields[:computed_value].present? final_value = fields[:computed_value] end { value: final_value } end |
#resolve_attribute(value) ⇒ Object
180 181 182 |
# File 'lib/avo/app/fields/field.rb', line 180 def resolve_attribute(value) value end |