From b5d27a9b60a35639516b33d56ea13acc36ccce4f Mon Sep 17 00:00:00 2001 From: nakamura Date: Sat, 20 Jul 2024 12:00:03 +0000 Subject: [PATCH] feat: Add pass_parent_resource_to_nested_has_many patch --- lib/administrate/mistybird.rb | 1 + .../pass_parent_resource_to_nested_has_many.rb | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 lib/administrate/patches/pass_parent_resource_to_nested_has_many.rb diff --git a/lib/administrate/mistybird.rb b/lib/administrate/mistybird.rb index b9afbcf..71b8beb 100644 --- a/lib/administrate/mistybird.rb +++ b/lib/administrate/mistybird.rb @@ -4,6 +4,7 @@ require_relative "field/refile" require_relative "role_aware_resource_resolver" require_relative "patches/administrate_datetime_default" +require_relative "patches/pass_parent_resource_to_nested_has_many" require_relative "patches/role_aware_resource_resolver" module Administrate diff --git a/lib/administrate/patches/pass_parent_resource_to_nested_has_many.rb b/lib/administrate/patches/pass_parent_resource_to_nested_has_many.rb new file mode 100644 index 0000000..6d74c91 --- /dev/null +++ b/lib/administrate/patches/pass_parent_resource_to_nested_has_many.rb @@ -0,0 +1,16 @@ +require "administrate/field/nested_has_many" + +Administrate::Field::NestedHasMany.prepend ( + Module.new { + private + + # see https://github.com/nickcharlton/administrate-field-nested_has_many/blob/main/lib/administrate/field/nested_has_many.rb#L101 + def new_resource + if options[:parent] + @new_resource ||= associated_class_name.constantize.new("#{options[:parent]}": @resource) + else + @new_resource ||= @resource.send(association_name).build + end + end + } +)