Class: RubyUIAdmin::Fields::BelongsToField
- Defined in:
- lib/ruby_ui_admin/fields/belongs_to_field.rb
Instance Attribute Summary
Attributes inherited from BaseField
#block, #id, #options, #resource
Instance Method Summary collapse
-
#database_id ⇒ Object
belongs_to writes/reads the foreign key column.
- #display_label(record) ⇒ Object
- #foreign_key_value(record) ⇒ Object
-
#options_for_select(limit: 1000) ⇒ Object
Candidate records to choose from in forms.
-
#reflection ⇒ Object
The ActiveRecord reflection for this association on the resource model.
- #target_model ⇒ Object
Methods inherited from BaseField
#default_hidden_views, #default_value, #description, field_type, #fill, #fill_value, #filterable?, #formatted_value, #has_default?, #help, #initialize, #link_to_record?, #name, #permit_param, #permit_params, #permitted_param, #placeholder, #readonly?, register_as, #required?, #sort_lambda, #sortable?, #type, #value, #visible?, #visible_in_view?
Constructor Details
This class inherits a constructor from RubyUIAdmin::Fields::BaseField
Instance Method Details
#database_id ⇒ Object
belongs_to writes/reads the foreign key column.
9 10 11 |
# File 'lib/ruby_ui_admin/fields/belongs_to_field.rb', line 9 def database_id :"#{id}_id" end |
#display_label(record) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/ruby_ui_admin/fields/belongs_to_field.rb', line 34 def display_label(record) return nil if record.nil? if record.respond_to?(:to_label) record.to_label elsif record.respond_to?(:name) record.name elsif record.respond_to?(:title) record.title else "#{record.model_name.human} ##{record.id}" end end |
#foreign_key_value(record) ⇒ Object
13 14 15 |
# File 'lib/ruby_ui_admin/fields/belongs_to_field.rb', line 13 def foreign_key_value(record) record.public_send(database_id) if record&.respond_to?(database_id) end |
#options_for_select(limit: 1000) ⇒ Object
Candidate records to choose from in forms. Capped to avoid huge selects.
27 28 29 30 31 32 |
# File 'lib/ruby_ui_admin/fields/belongs_to_field.rb', line 27 def (limit: 1000) model = target_model return [] if model.nil? model.all.limit(limit).map { |record| [display_label(record), record.id] } end |
#reflection ⇒ Object
The ActiveRecord reflection for this association on the resource model.
18 19 20 |
# File 'lib/ruby_ui_admin/fields/belongs_to_field.rb', line 18 def reflection resource&.model_class&.reflect_on_association(id) end |
#target_model ⇒ Object
22 23 24 |
# File 'lib/ruby_ui_admin/fields/belongs_to_field.rb', line 22 def target_model [:model] || reflection&.klass end |