Class: Administrate::Field::NestedHasMany

Inherits:
HasMany
  • Object
show all
Defined in:
lib/viniBaxter/spa/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, options) ⇒ Object



52
53
54
55
# File 'lib/viniBaxter/spa/nested_has_many.rb', line 52

def self.associated_attributes(associated_resource, options)
  dashboard_class = dashboard_for_resource(associated_resource, options)
  DEFAULT_ATTRIBUTES + dashboard_class.new.permitted_attributes
end

.dashboard_for_resource(resource, options) ⇒ Object



47
48
49
50
# File 'lib/viniBaxter/spa/nested_has_many.rb', line 47

def self.dashboard_for_resource(resource, options)
  class_name = options && options[:class_name] || resource.to_s.classify
  "#{class_name}Dashboard".constantize
end

.permitted_attribute(associated_resource, options = nil) ⇒ Object



57
58
59
60
61
62
# File 'lib/viniBaxter/spa/nested_has_many.rb', line 57

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

Instance Method Details

#associated_class_nameObject



64
65
66
# File 'lib/viniBaxter/spa/nested_has_many.rb', line 64

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

#associated_formObject



75
76
77
# File 'lib/viniBaxter/spa/nested_has_many.rb', line 75

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

#association_nameObject



68
69
70
71
72
73
# File 'lib/viniBaxter/spa/nested_has_many.rb', line 68

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

#nested_fieldsObject



22
23
24
25
26
# File 'lib/viniBaxter/spa/nested_has_many.rb', line 22

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



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/viniBaxter/spa/nested_has_many.rb', line 28

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



43
44
45
# File 'lib/viniBaxter/spa/nested_has_many.rb', line 43

def to_s
  data
end