Class: Administrate::Field::NestedHasMany
- Inherits:
-
HasMany
- Object
- HasMany
- Administrate::Field::NestedHasMany
- 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
- .associated_attributes(associated_resource, options) ⇒ Object
- .dashboard_for_resource(resource, options) ⇒ Object
- .permitted_attribute(associated_resource, options = nil) ⇒ Object
Instance Method Summary collapse
- #associated_class_name ⇒ Object
- #associated_form ⇒ Object
- #association_name ⇒ Object
- #nested_fields ⇒ Object
- #nested_fields_for_builder(form_builder) ⇒ Object
- #to_s ⇒ Object
Class Method Details
.associated_attributes(associated_resource, options) ⇒ Object
52 53 54 55 |
# File 'lib/administrate/field/nested_has_many.rb', line 52 def self.associated_attributes(associated_resource, ) dashboard_class = dashboard_for_resource(associated_resource, ) DEFAULT_ATTRIBUTES + dashboard_class.new.permitted_attributes end |
.dashboard_for_resource(resource, options) ⇒ Object
47 48 49 50 |
# File 'lib/administrate/field/nested_has_many.rb', line 47 def self.dashboard_for_resource(resource, ) class_name = && [: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/administrate/field/nested_has_many.rb', line 57 def self.permitted_attribute(associated_resource, = nil) { "#{associated_resource}_attributes".to_sym => associated_attributes(associated_resource, ), } end |
Instance Method Details
#associated_class_name ⇒ Object
64 65 66 |
# File 'lib/administrate/field/nested_has_many.rb', line 64 def associated_class_name .fetch(:class_name, attribute.to_s.singularize.camelcase) end |
#associated_form ⇒ Object
75 76 77 |
# File 'lib/administrate/field/nested_has_many.rb', line 75 def associated_form Administrate::Page::Form.new(associated_dashboard, new_resource) end |
#association_name ⇒ Object
68 69 70 71 72 73 |
# File 'lib/administrate/field/nested_has_many.rb', line 68 def association_name .fetch( :association_name, associated_class_name.underscore.pluralize[/([^\/]*)$/, 1], ) end |
#nested_fields ⇒ Object
22 23 24 25 26 |
# File 'lib/administrate/field/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/administrate/field/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_s ⇒ Object
43 44 45 |
# File 'lib/administrate/field/nested_has_many.rb', line 43 def to_s data end |