Class: Administrate::Field::NestedHasMany

Inherits:
HasMany
  • Object
show all
Defined in:
lib/administrate/field/nested_has_many.rb

Defined Under Namespace

Classes: Engine

Constant Summary collapse

DEFAULT_ATTRIBUTES =
%i(id _destroy).freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.associated_attributes(associated_resource) ⇒ Object



50
51
52
53
# File 'lib/administrate/field/nested_has_many.rb', line 50

def self.associated_attributes(associated_resource)
  DEFAULT_ATTRIBUTES +
    dashboard_for_resource(associated_resource).new.permitted_attributes
end

.dashboard_for_resource(resource) ⇒ Object



46
47
48
# File 'lib/administrate/field/nested_has_many.rb', line 46

def self.dashboard_for_resource(resource)
  "#{resource.to_s.classify}Dashboard".constantize
end

.permitted_attribute(associated_resource, _options = nil) ⇒ Object



55
56
57
58
59
60
# File 'lib/administrate/field/nested_has_many.rb', line 55

def self.permitted_attribute(associated_resource, _options = nil)
  {
    "#{associated_resource}_attributes".to_sym =>
    associated_attributes(associated_resource),
  }
end

Instance Method Details

#associated_class_nameObject



62
63
64
# File 'lib/administrate/field/nested_has_many.rb', line 62

def associated_class_name
  options.fetch(:class_name, attribute.to_s.singularize.camelcase)
end

#associated_formObject



70
71
72
# File 'lib/administrate/field/nested_has_many.rb', line 70

def associated_form
  Administrate::Page::Form.new(associated_dashboard, data)
end

#association_nameObject



66
67
68
# File 'lib/administrate/field/nested_has_many.rb', line 66

def association_name
  associated_class_name.underscore.pluralize
end

#nested_fieldsObject



21
22
23
24
25
# File 'lib/administrate/field/nested_has_many.rb', line 21

def nested_fields
  associated_form.attributes.reject do |nested_field|
    skipped_fields.include?(nested_field.attribute)
  end
end

#nested_fields_for_builder(form_builder) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/administrate/field/nested_has_many.rb', line 27

def nested_fields_for_builder(form_builder)
  return nested_fields unless form_builder.index.is_a? Integer

  nested_fields.each do |nested_field|
    next if nested_field.resource.blank?

    # inject current data into field
    resource = nested_field.resource[form_builder.index]
    nested_field.instance_variable_set(
      "@data",
      resource.send(nested_field.attribute),
    )
  end
end

#to_sObject



42
43
44
# File 'lib/administrate/field/nested_has_many.rb', line 42

def to_s
  data
end