Skip to content

Parent resource is made accesible through the template object when th…#137

Closed
ricardojudo wants to merge 1 commit intoactiveadmin-plugins:masterfrom
ricardojudo:master
Closed

Parent resource is made accesible through the template object when th…#137
ricardojudo wants to merge 1 commit intoactiveadmin-plugins:masterfrom
ricardojudo:master

Conversation

@ricardojudo
Copy link
Copy Markdown
Contributor

…e active admin page has the 'belongs_to' statement.

I needed to implement this change in order to get a reference of the parent resource within a proc passed to the before_batch_import option in order to insert additional information to each line.

before_batch_import: Proc.new{|importer|         
      _form = importer.model.parent
       importer.csv_lines.each do |line|
         line.push _form.id
         line.push _form.generate_registration_code
       end
 }

I was not sure where and how to document this change.

What do you think about this proposal?

…e active admin page has the 'belongs_to' statement.
@active_admin_import_model = options[:template_object]
@active_admin_import_model.assign_attributes(params[params_key].try(:deep_symbolize_keys) || {})
@active_admin_import_model.assign_attributes(params[params_key].try(:deep_symbolize_keys) || {})
@active_admin_import_model.assign_attributes(parent: parent) unless defined?(parent).nil?
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [97/80]

params = ActiveSupport::HashWithIndifferentAccess.new _params
@active_admin_import_model = options[:template_object]
@active_admin_import_model.assign_attributes(params[params_key].try(:deep_symbolize_keys) || {})
@active_admin_import_model.assign_attributes(params[params_key].try(:deep_symbolize_keys) || {})
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [112/80]
Trailing whitespace detected.

@coveralls
Copy link
Copy Markdown

coveralls commented Apr 12, 2017

Coverage Status

Coverage decreased (-0.8%) to 96.942% when pulling 7afc6e6 on ricardojudo:master into 30b6d71 on activeadmin-plugins:master.

@Fivell
Copy link
Copy Markdown
Member

Fivell commented Apr 18, 2017

@ricardojudo , for new features to merge there is a requirement to add spec here https://github.com/activeadmin-plugins/active_admin_import/blob/master/spec/import_spec.rb

@active_admin_import_model = options[:template_object]
@active_admin_import_model.assign_attributes(params[params_key].try(:deep_symbolize_keys) || {})
@active_admin_import_model.assign_attributes(params[params_key].try(:deep_symbolize_keys) || {})
@active_admin_import_model.assign_attributes(parent: parent) unless defined?(parent).nil?
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ricardojudo , why not
if parent? instead of defined?(parent).nil?

Fivell added a commit that referenced this pull request Apr 8, 2026
…ntext

Controllers can define an `active_admin_import_context` method returning a
hash; it is merged into the import model after form params so values the
controller is authoritative about (parent.id, current_user.id, request
attributes, etc.) cannot be overridden by tampered form fields.

This supersedes PR #137 by offering a general-purpose hook that works for
nested belongs_to imports and any other controller-derived state.
@Fivell
Copy link
Copy Markdown
Member

Fivell commented Apr 8, 2026

Hi @ricardojudo — thanks for this PR and sorry for the long delay.

I've opened #211 which addresses this use case with a more general mechanism: a convention method active_admin_import_context that the controller can define to provide extra attributes for the import model. It's merged into the model after form params, so the parent id cannot be overridden by tampered form fields.

Your original use case becomes:

ActiveAdmin.register PostComment do
  belongs_to :post

  controller do
    def active_admin_import_context
      { post_id: parent.id }
    end
  end

  active_admin_import template_object: ActiveAdminImport::Model.new,
                      before_batch_import: ->(importer) {
                        importer.csv_lines.map! { |row| row << importer.model.post_id }
                        importer.headers.merge!(:'Post Id' => :post_id)
                      }
end

Since the context method is defined directly on the controller, parent (and current_user, request, etc.) are available naturally without any special accessor.

There is an end-to-end Capybara spec in #211 that verifies nested PostComment imports automatically get post_id from the parent post in the URL. If that approach works for you I'll close this PR in favor of #211.

Fivell added a commit that referenced this pull request Apr 8, 2026
…ntext

Controllers can define an `active_admin_import_context` method returning a
hash; it is merged into the import model after form params so values the
controller is authoritative about (parent.id, current_user.id, request
attributes, etc.) cannot be overridden by tampered form fields.

This supersedes PR #137 by offering a general-purpose hook that works for
nested belongs_to imports and any other controller-derived state.
@Fivell Fivell closed this in #211 Apr 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants