Module: Uchi::Field::BelongsTo::Helpers

Included in:
Edit, Index, Show
Defined in:
app/components/uchi/field/belongs_to.rb

Instance Method Summary collapse

Instance Method Details

#associated_recordObject



9
10
11
# File 'app/components/uchi/field/belongs_to.rb', line 9

def associated_record
  field.value(record)
end

#associated_repositoryObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/components/uchi/field/belongs_to.rb', line 13

def associated_repository
  reflection = record.class.reflect_on_association(field.name)

  unless reflection
    raise \
      ArgumentError,
      "No association named #{field.name.inspect} found on #{record.class}"
  end

  model = if reflection.polymorphic?
    associated_record&.class
  else
    reflection.klass
  end

  return nil if model.nil?

  repository_class = Uchi::Repository.for_model(model)
  repository_class.new
end

#label_for_associated_recordObject



34
35
36
# File 'app/components/uchi/field/belongs_to.rb', line 34

def label_for_associated_record
  associated_repository.title(associated_record)
end