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(resource_class, attr) ⇒ Object



58
59
60
61
# File 'lib/administrate/field/nested_has_many.rb', line 58

def self.associated_attributes(resource_class, attr)
  dashboard_class = dashboard_for_resource(resource_class, attr)
  DEFAULT_ATTRIBUTES + dashboard_class.new.permitted_attributes
end

.dashboard_for_resource(resource_class, attr) ⇒ Object



54
55
56
# File 'lib/administrate/field/nested_has_many.rb', line 54

def self.dashboard_for_resource(resource_class, attr)
  "#{associated_class_name(resource_class, attr)}Dashboard".constantize
end

.permitted_attribute(attr, options = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/administrate/field/nested_has_many.rb', line 63

def self.permitted_attribute(attr, options = {})
  given_class_name = options[:class_name]
  _resource_class =
    if given_class_name
      Administrate.warn_of_deprecated_option(:class_name)
      given_class_name.classify
    else
      options[:resource_class]
    end

  {
    "#{attr}_attributes".to_sym =>
      associated_attributes(_resource_class, attr),
  }
end

Instance Method Details

#associated_class_nameObject



79
80
81
# File 'lib/administrate/field/nested_has_many.rb', line 79

def associated_class_name
  self.class.associated_class_name(resource.class, attribute)
end

#associated_formObject



89
90
91
# File 'lib/administrate/field/nested_has_many.rb', line 89

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

#association_nameObject



83
84
85
86
87
# File 'lib/administrate/field/nested_has_many.rb', line 83

def association_name
  options.fetch(:association_name) do
    associated_class_name.underscore.pluralize[/([^\/]*)$/, 1]
  end
end

#nested_fieldsObject



29
30
31
32
33
# File 'lib/administrate/field/nested_has_many.rb', line 29

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/administrate/field/nested_has_many.rb', line 35

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 = data[form_builder.index]
    nested_field.instance_variable_set(
      "@data",
      resource.send(nested_field.attribute),
    )
  end
end

#to_sObject



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

def to_s
  data
end