Class: RubyUIAdmin::Fields::AssociationField

Inherits:
BaseField
  • Object
show all
Defined in:
lib/ruby_ui_admin/fields/association_field.rb

Overview

Base for has_one / has_many / has_and_belongs_to_many fields. Association fields are shown on the show view only by default.

Direct Known Subclasses

HasManyField, HasOneField

Instance Attribute Summary

Attributes inherited from BaseField

#block, #id, #options, #resource

Instance Method Summary collapse

Methods inherited from BaseField

#database_id, #default_value, #description, field_type, #fill, #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, #visible?, #visible_in_view?

Constructor Details

This class inherits a constructor from RubyUIAdmin::Fields::BaseField

Instance Method Details

#association_nameObject

The association read from the record — for_attribute: overrides the field id, e.g. field :discussion, as: :has_many, for_attribute: :comments.



14
15
16
# File 'lib/ruby_ui_admin/fields/association_field.rb', line 14

def association_name
  options[:for_attribute] || id
end

#default_hidden_viewsObject



8
9
10
# File 'lib/ruby_ui_admin/fields/association_field.rb', line 8

def default_hidden_views
  %i[index new edit]
end

#display_label(record) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ruby_ui_admin/fields/association_field.rb', line 49

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

#fill_value(record, value) ⇒ Object

Association values aren't writable through a simple setter here.



64
# File 'lib/ruby_ui_admin/fields/association_field.rb', line 64

def fill_value(record, value); end

#reflectionObject



25
26
27
# File 'lib/ruby_ui_admin/fields/association_field.rb', line 25

def reflection
  resource&.model_class&.reflect_on_association(association_name)
end

#scoped(relation) ⇒ Object

Applies a scope: (Symbol naming a relation method, or a lambda with query) to a relation value. No-op for nil or no scope.



41
42
43
44
45
46
47
# File 'lib/ruby_ui_admin/fields/association_field.rb', line 41

def scoped(relation)
  scope = options[:scope]
  return relation if relation.nil? || scope.nil?
  return relation.public_send(scope) if scope.is_a?(Symbol)

  ExecutionContext.new(target: scope, query: relation, resource: resource).handle
end

#target_modelObject



29
30
31
# File 'lib/ruby_ui_admin/fields/association_field.rb', line 29

def target_model
  options[:model] || reflection&.klass
end

#use_resourceObject

Resource to link associated records through (use_resource: overrides the default model→resource lookup done by the view).



35
36
37
# File 'lib/ruby_ui_admin/fields/association_field.rb', line 35

def use_resource
  options[:use_resource]
end

#value(record, view_context: nil) ⇒ Object

Read the association (honoring for_attribute:); a computed block still wins.



19
20
21
22
23
# File 'lib/ruby_ui_admin/fields/association_field.rb', line 19

def value(record, view_context: nil)
  return super if block

  record&.respond_to?(association_name) ? record.public_send(association_name) : nil
end