Class: Avo::Fields::HasOneField
- Defined in:
- lib/avo/app/fields/has_one.rb
Instance Attribute Summary collapse
-
#relation_method ⇒ Object
Returns the value of attribute relation_method.
Attributes inherited from Field
#block, #component, #computable, #default, #format_using, #help, #id, #is_array_param, #is_object_param, #name, #null_values, #nullable, #placeholder, #readonly, #required, #sortable, #updatable
Attributes included from FieldExtensions::VisibleOnDifferentViews
#show_on_create, #show_on_edit, #show_on_index, #show_on_show
Instance Method Summary collapse
- #fill_field(model, key, value) ⇒ Object
- #get_related_resource(resource) ⇒ Object
- #hydrate_field(fields, model, resource, view) ⇒ Object
-
#initialize(name, **args, &block) ⇒ HasOneField
constructor
A new instance of HasOneField.
Methods inherited from Field
#database_id, #fetch_for_action, #fetch_for_resource, #has_own_panel?, #resolve_attribute
Methods included from FieldExtensions::HasFieldName
#field_name, #field_name_attribute, #get_field_name
Methods included from FieldExtensions::VisibleOnDifferentViews
#except_on, #hide_on, #only_on, #show_on
Constructor Details
#initialize(name, **args, &block) ⇒ HasOneField
Returns a new instance of HasOneField.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/avo/app/fields/has_one.rb', line 6 def initialize(name, **args, &block) @defaults = { updatable: true, component: 'has-one-field', } super(name, **args, &block) hide_on :create @placeholder = 'Choose an option' @relation_method = name.to_s.parameterize.underscore end |
Instance Attribute Details
#relation_method ⇒ Object
Returns the value of attribute relation_method.
4 5 6 |
# File 'lib/avo/app/fields/has_one.rb', line 4 def relation_method @relation_method end |
Instance Method Details
#fill_field(model, key, value) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/avo/app/fields/has_one.rb', line 56 def fill_field(model, key, value) if value.blank? = nil else = model.class.reflections[name.to_s.downcase].class_name = .safe_constantize.find value end model.public_send("#{key}=", ) model end |
#get_related_resource(resource) ⇒ Object
51 52 53 54 |
# File 'lib/avo/app/fields/has_one.rb', line 51 def (resource) class_name = resource.model.reflections[name.to_s.downcase].class_name App.get_resources.find { |r| r.class == "Avo::Resources::#{class_name}".safe_constantize } end |
#hydrate_field(fields, model, resource, view) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/avo/app/fields/has_one.rb', line 21 def hydrate_field(fields, model, resource, view) target_resource = (resource) fields[:relation_class] = target_resource.class.to_s relation_model = model.public_send(@relation_method) if relation_model.present? relation_model = model.public_send(@relation_method) fields[:value] = relation_model.send(target_resource.title) fields[:database_value] = relation_model[:id] fields[:link] = Avo::Resources::Resource.show_path(relation_model) end # Populate the options on show and edit fields[:options] = [] if [:edit, :create].include? view fields[:options] = target_resource.model.all.map do |model| { value: model.id, label: model.send(target_resource.title) } end end fields[:resource_name_plural] = target_resource.resource_name_plural fields end |