Class: Avo::Fields::BelongsToField
- Includes:
- Concerns::IsSearchable, Concerns::UseResource
- Defined in:
- lib/avo/fields/belongs_to_field.rb
Overview
Requirements
-
list
-
ID
-
label
-
Type
-
foreign_key
-
foreign_key for poly type
-
foreign_key for poly id
-
is_disabled?
Defined Under Namespace
Classes: EditComponent, IndexComponent, ShowComponent
Instance Attribute Summary collapse
-
#allow_via_detaching ⇒ Object
readonly
Returns the value of attribute allow_via_detaching.
-
#attach_scope ⇒ Object
readonly
Returns the value of attribute attach_scope.
-
#polymorphic_as ⇒ Object
readonly
Returns the value of attribute polymorphic_as.
-
#polymorphic_help ⇒ Object
readonly
Returns the value of attribute polymorphic_help.
-
#relation_method ⇒ Object
readonly
Returns the value of attribute relation_method.
-
#target ⇒ Object
Returns the value of attribute target.
-
#types ⇒ Object
readonly
for Polymorphic associations.
Attributes inherited from BaseField
#action, #as_avatar, #autocomplete, #block, #computable, #computed, #computed_value, #default, #for_attribute, #for_presentation_only, #format_using, #help, #id, #null_values, #nullable, #panel_name, #readonly, #record, #required, #sortable, #stacked, #summarizable, #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
- #can_create? ⇒ Boolean
- #database_id ⇒ Object
- #database_value ⇒ Object
- #default_name ⇒ Object
-
#field_label ⇒ Object
What the user sees in the text field.
-
#field_value ⇒ Object
The value.
- #fill_field(model, key, value, params) ⇒ Object
- #foreign_key ⇒ Object
- #form_field_label ⇒ Object
- #get_record ⇒ Object
- #id_input_foreign_key ⇒ Object
-
#initialize(id, **args, &block) ⇒ BelongsToField
constructor
A new instance of BelongsToField.
- #is_polymorphic? ⇒ Boolean
- #label ⇒ Object
- #options ⇒ Object
- #polymorphic_form_field_label ⇒ Object
-
#reflection ⇒ Object
Get the model reflection instance.
- #reflection_for_key(key) ⇒ Object
- #relation_model_class ⇒ Object
- #target_resource ⇒ Object
- #to_permitted_param ⇒ Object
- #type_input_foreign_key ⇒ Object
- #valid_polymorphic_class(possible_class) ⇒ Object
- #value ⇒ Object
- #values_for_type(model = nil) ⇒ Object
Methods included from Concerns::UseResource
Methods included from Concerns::IsSearchable
Methods inherited from BaseField
#assign_value, #custom?, #custom_name?, #execute_block, #has_attribute?, #has_own_panel?, #hidden_in_reflection?, #name, #options_for_filter, #placeholder, #plural_name, #record_errors, #resolve_attribute, #table_header_label, #translated_name, #translated_plural_name, #translation_key, #type, #updatable, #visible_in_reflection?
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
Methods included from Concerns::Hydration
Constructor Details
#initialize(id, **args, &block) ⇒ BelongsToField
Returns a new instance of BelongsToField.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/avo/fields/belongs_to_field.rb', line 72 def initialize(id, **args, &block) args[:placeholder] ||= I18n.t("avo.choose_an_option") super(id, **args, &block) @searchable = args[:searchable] == true @polymorphic_as = args[:polymorphic_as] @types = args[:types] @relation_method = id.to_s.parameterize.underscore @allow_via_detaching = args[:allow_via_detaching] == true @attach_scope = args[:attach_scope] @polymorphic_help = args[:polymorphic_help] @target = args[:target] @use_resource = args[:use_resource] || nil @can_create = args[:can_create].nil? ? true : args[:can_create] end |
Instance Attribute Details
#allow_via_detaching ⇒ Object (readonly)
Returns the value of attribute allow_via_detaching.
68 69 70 |
# File 'lib/avo/fields/belongs_to_field.rb', line 68 def allow_via_detaching @allow_via_detaching end |
#attach_scope ⇒ Object (readonly)
Returns the value of attribute attach_scope.
69 70 71 |
# File 'lib/avo/fields/belongs_to_field.rb', line 69 def attach_scope @attach_scope end |
#polymorphic_as ⇒ Object (readonly)
Returns the value of attribute polymorphic_as.
65 66 67 |
# File 'lib/avo/fields/belongs_to_field.rb', line 65 def polymorphic_as @polymorphic_as end |
#polymorphic_help ⇒ Object (readonly)
Returns the value of attribute polymorphic_help.
70 71 72 |
# File 'lib/avo/fields/belongs_to_field.rb', line 70 def polymorphic_help @polymorphic_help end |
#relation_method ⇒ Object (readonly)
Returns the value of attribute relation_method.
66 67 68 |
# File 'lib/avo/fields/belongs_to_field.rb', line 66 def relation_method @relation_method end |
#target ⇒ Object
Returns the value of attribute target.
63 64 65 |
# File 'lib/avo/fields/belongs_to_field.rb', line 63 def target @target end |
#types ⇒ Object (readonly)
for Polymorphic associations
67 68 69 |
# File 'lib/avo/fields/belongs_to_field.rb', line 67 def types @types end |
Instance Method Details
#can_create? ⇒ Boolean
264 265 266 |
# File 'lib/avo/fields/belongs_to_field.rb', line 264 def can_create? @can_create end |
#database_id ⇒ Object
223 224 225 226 227 228 229 230 |
# File 'lib/avo/fields/belongs_to_field.rb', line 223 def database_id # If the field is a polymorphic value, return the polymorphic_type as key and pre-fill the _id in fill_field. return :"#{polymorphic_as}_type" if polymorphic_as.present? foreign_key rescue id end |
#database_value ⇒ Object
130 131 132 133 134 |
# File 'lib/avo/fields/belongs_to_field.rb', line 130 def database_value target_resource.id rescue nil end |
#default_name ⇒ Object
258 259 260 261 262 |
# File 'lib/avo/fields/belongs_to_field.rb', line 258 def default_name return polymorphic_as.to_s.humanize if polymorphic_as.present? super end |
#field_label ⇒ Object
What the user sees in the text field
107 108 109 |
# File 'lib/avo/fields/belongs_to_field.rb', line 107 def field_label label end |
#field_value ⇒ Object
The value
100 101 102 103 104 |
# File 'lib/avo/fields/belongs_to_field.rb', line 100 def field_value value.send(database_value) rescue nil end |
#fill_field(model, key, value, params) ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/avo/fields/belongs_to_field.rb', line 196 def fill_field(model, key, value, params) return model unless model.methods.include? key.to_sym if polymorphic_as.present? valid_model_class = valid_polymorphic_class params[:"#{polymorphic_as}_type"] model.send(:"#{polymorphic_as}_type=", valid_model_class) # If the type is blank, reset the id too. if valid_model_class.blank? model.send(:"#{polymorphic_as}_id=", nil) else model.send(:"#{polymorphic_as}_id=", params["#{polymorphic_as}_id"]) end else model.send("#{key}=", value) end model end |
#foreign_key ⇒ Object
156 157 158 159 160 161 162 163 164 |
# File 'lib/avo/fields/belongs_to_field.rb', line 156 def foreign_key @foreign_key ||= if polymorphic_as.present? polymorphic_as elsif @record.present? get_model_class(@record).reflections[@relation_method].foreign_key elsif @resource.present? && @resource.model_class.reflections[@relation_method].present? @resource.model_class.reflections[@relation_method].foreign_key end end |
#form_field_label ⇒ Object
268 269 270 |
# File 'lib/avo/fields/belongs_to_field.rb', line 268 def form_field_label "#{id}_id" end |
#get_record ⇒ Object
254 255 256 |
# File 'lib/avo/fields/belongs_to_field.rb', line 254 def get_record @record || @resource.record end |
#id_input_foreign_key ⇒ Object
142 143 144 145 146 147 148 |
# File 'lib/avo/fields/belongs_to_field.rb', line 142 def id_input_foreign_key if is_polymorphic? "#{foreign_key}_id" else foreign_key end end |
#is_polymorphic? ⇒ Boolean
150 151 152 153 154 |
# File 'lib/avo/fields/belongs_to_field.rb', line 150 def is_polymorphic? polymorphic_as.present? rescue false end |
#label ⇒ Object
183 184 185 186 |
# File 'lib/avo/fields/belongs_to_field.rb', line 183 def label return if target_resource.blank? target_resource.new(record: value)&.record_title end |
#options ⇒ Object
111 112 113 |
# File 'lib/avo/fields/belongs_to_field.rb', line 111 def values_for_type end |
#polymorphic_form_field_label ⇒ Object
272 273 274 |
# File 'lib/avo/fields/belongs_to_field.rb', line 272 def polymorphic_form_field_label "#{id}_type" end |
#reflection ⇒ Object
Get the model reflection instance
173 174 175 176 177 |
# File 'lib/avo/fields/belongs_to_field.rb', line 173 def reflection reflection_for_key(id) rescue nil end |
#reflection_for_key(key) ⇒ Object
166 167 168 169 170 |
# File 'lib/avo/fields/belongs_to_field.rb', line 166 def reflection_for_key(key) get_model_class(get_record).reflections[key.to_s] rescue nil end |
#relation_model_class ⇒ Object
179 180 181 |
# File 'lib/avo/fields/belongs_to_field.rb', line 179 def relation_model_class @resource.model_class end |
#target_resource ⇒ Object
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/avo/fields/belongs_to_field.rb', line 232 def target_resource @target_resource ||= if use_resource.present? use_resource elsif is_polymorphic? if value.present? get_resource_by_model_class(value.class) else return nil end else reflection_key = polymorphic_as || id if @record._reflections[reflection_key.to_s].klass.present? get_resource_by_model_class(@record._reflections[reflection_key.to_s].klass.to_s) elsif @record._reflections[reflection_key.to_s].[:class_name].present? get_resource_by_model_class(@record._reflections[reflection_key.to_s].[:class_name]) else App.get_resource_by_name reflection_key.to_s end end end |
#to_permitted_param ⇒ Object
188 189 190 191 192 193 194 |
# File 'lib/avo/fields/belongs_to_field.rb', line 188 def to_permitted_param if polymorphic_as.present? return [:"#{polymorphic_as}_type", :"#{polymorphic_as}_id"] end foreign_key.to_sym end |
#type_input_foreign_key ⇒ Object
136 137 138 139 140 |
# File 'lib/avo/fields/belongs_to_field.rb', line 136 def type_input_foreign_key if is_polymorphic? "#{foreign_key}_type" end end |
#valid_polymorphic_class(possible_class) ⇒ Object
217 218 219 220 221 |
# File 'lib/avo/fields/belongs_to_field.rb', line 217 def valid_polymorphic_class(possible_class) types.find do |type| type.to_s == possible_class.to_s end end |
#value ⇒ Object
89 90 91 92 93 94 95 96 97 |
# File 'lib/avo/fields/belongs_to_field.rb', line 89 def value if is_polymorphic? # Get the value from the pre-filled association record super(polymorphic_as) else # Get the value from the pre-filled association record super(relation_method) end end |
#values_for_type(model = nil) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/avo/fields/belongs_to_field.rb', line 115 def values_for_type(model = nil) resource = target_resource resource = Avo.resource_manager.get_resource_by_model_class model if model.present? query = resource.query_scope if attach_scope.present? query = Avo::ExecutionContext.new(target: attach_scope, query: query, parent: get_record).handle end query.all.map do |record| [resource.new(record: record).record_title, record.id] end end |