Class: Uchi::Field::HasMany::Show

Inherits:
Base::Show show all
Defined in:
app/components/uchi/field/has_many.rb

Instance Attribute Summary

Attributes inherited from Base::Component

#field, #record, #repository

Instance Method Summary collapse

Methods inherited from Base::Component

#initialize, #value

Methods included from RoutesHelper

#uchi_path_to

Constructor Details

This class inherits a constructor from Uchi::Field::Base::Component

Instance Method Details

#associated_records ⇒ Object



70
71
72
73
74
75
76
77
# File 'app/components/uchi/field/has_many.rb', line 70

def associated_records
  records = field.value(record)
  return [] if records.nil?

  return Array(records) if field.nested_fields.any?

  associated_repository.find_all(scope: records)
end

#associated_repository ⇒ Object

Raises:

  • (NameError)


79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/components/uchi/field/has_many.rb', line 79

def associated_repository
  raise NameError, "No association named #{field.name.inspect} found on #{record.class}" unless reflection

  associated_model = reflection.klass
  repository_class = Uchi::Repository.for_model(associated_model)
  unless repository_class
    raise \
      NameError,
      "No repository found for associated model #{associated_model}"
  end

  repository_class.new(context: repository.context)
end

#attach_scope ⇒ Hash

Returns the scope to pass when linking to create a new record for this association (e.g. from Company#show, to Person#new), so that the new record ends up associated with this record.

Returns:

  • (Hash)


98
99
100
101
102
103
104
# File 'app/components/uchi/field/has_many.rb', line 98

def attach_scope
  {
    field: field.name,
    id: record.id,
    model: record.model_name.to_s
  }
end