Parent resource is made accesible through the template object when th…#137
Parent resource is made accesible through the template object when th…#137ricardojudo wants to merge 1 commit intoactiveadmin-plugins:masterfrom
Conversation
…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? |
| 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) || {}) |
There was a problem hiding this comment.
Line is too long. [112/80]
Trailing whitespace detected.
|
@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? |
There was a problem hiding this comment.
@ricardojudo , why not
if parent? instead of defined?(parent).nil?
…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.
|
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 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)
}
endSince the context method is defined directly on the controller, There is an end-to-end Capybara spec in #211 that verifies nested |
…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.
…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.
I was not sure where and how to document this change.
What do you think about this proposal?