Class: Uchi::Field::HasMany::Edit

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

Instance Attribute Summary

Attributes inherited from Base::Edit

#form, #hint, #label

Attributes inherited from Base::Component

#field, #record, #repository

Instance Method Summary collapse

Methods inherited from Base::Edit

#initialize

Methods inherited from Base::Component

#initialize, #value

Constructor Details

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

Instance Method Details

#associated_recordsObject



9
10
11
12
13
14
# File 'app/components/uchi/field/has_many.rb', line 9

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

  associated_repository.find_all(scope: records)
end

#associated_repositoryObject



16
17
18
19
20
21
22
# File 'app/components/uchi/field/has_many.rb', line 16

def associated_repository
  @associated_repository ||= begin
    model = reflection.klass
    repository_class = Uchi::Repository.for_model(model)
    repository_class.new
  end
end

#attribute_nameObject



24
25
26
# File 'app/components/uchi/field/has_many.rb', line 24

def attribute_name
  "#{field.name.to_s.singularize}_ids"
end

#dom_id_for_filter_query_inputObject



28
29
30
# File 'app/components/uchi/field/has_many.rb', line 28

def dom_id_for_filter_query_input
  "#{form.object_name}_#{attribute_name}_has_many_filter_query"
end

#dom_id_for_toggleObject



32
33
34
# File 'app/components/uchi/field/has_many.rb', line 32

def dom_id_for_toggle
  "#{form.object_name}_#{attribute_name}_has_many_toggle"
end

#field_name_for_inputObject



36
37
38
# File 'app/components/uchi/field/has_many.rb', line 36

def field_name_for_input
  "#{form.object_name}[#{attribute_name}][]"
end

#record_title(record) ⇒ Object



40
41
42
43
44
# File 'app/components/uchi/field/has_many.rb', line 40

def record_title(record)
  return "" if record.nil?

  associated_repository.title(record)
end

#selected_titlesObject



46
47
48
# File 'app/components/uchi/field/has_many.rb', line 46

def selected_titles
  associated_records.map { |record| record_title(record) }.join(", ")
end