Class: Madmin::Fields::HasMany

Inherits:
Madmin::Field show all
Includes:
Pagy::Backend, Pagy::Method
Defined in:
lib/madmin/fields/has_many.rb

Direct Known Subclasses

NestedHasMany

Instance Attribute Summary

Attributes inherited from Madmin::Field

#attribute_name, #model, #options, #resource

Instance Method Summary collapse

Methods inherited from Madmin::Field

#default_index_attributes, field_type, #initialize, #required?, #searchable?, #to_partial_path, #value, #visible?

Constructor Details

This class inherits a constructor from Madmin::Field

Instance Method Details

#associated_resourceObject



22
23
24
25
# File 'lib/madmin/fields/has_many.rb', line 22

def associated_resource
  Madmin.resource_by_name(model.reflect_on_association(attribute_name).klass)
rescue MissingResource
end

#associated_resource_for(object) ⇒ Object



27
28
29
30
# File 'lib/madmin/fields/has_many.rb', line 27

def associated_resource_for(object)
  Madmin.resource_for(object)
rescue MissingResource
end

#index_path(format: :json) ⇒ Object



18
19
20
# File 'lib/madmin/fields/has_many.rb', line 18

def index_path(format: :json)
  associated_resource&.index_path(format: format)
end

#options_for_select(record) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/madmin/fields/has_many.rb', line 4

def options_for_select(record)
  if (records = record.send(attribute_name))
    return [] unless records.first
    resource = Madmin.resource_for(records.first)
    records.map { |record| [resource.display_name(record), record.id] }
  else
    []
  end
end

#paginateable?Boolean

Returns:



32
33
34
# File 'lib/madmin/fields/has_many.rb', line 32

def paginateable?
  true
end

#paginated_value(record, params) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/madmin/fields/has_many.rb', line 39

def paginated_value(record, params)
  page_key = "#{attribute_name}_page"
  request = {
    query: {
      "#{attribute_name}_page" => [params[page_key].to_i, 1].max
    }
  }
  pagy value(record), page_key: page_key, request: request
rescue Pagy::OptionError
end

#to_paramObject



14
15
16
# File 'lib/madmin/fields/has_many.rb', line 14

def to_param
  {"#{attribute_name.to_s.singularize}_ids": []}
end