Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recursively process nested parameters for polymorphism #1648

Merged
merged 1 commit into from
May 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/controllers/administrate/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ def read_param_value(data)
else
raise "Unrecognised param data: #{data.inspect}"
end
elsif data.is_a?(ActionController::Parameters)
data.transform_values { |v| read_param_value(v) }
else
data
end
Expand Down
46 changes: 46 additions & 0 deletions spec/controllers/admin/log_entries_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,52 @@ def post_create(action: "create", logeable: create(:customer))
post_create
expect(response).to redirect_to([:admin, LogEntry.last])
end

it "parses nested polymorphic resources" do
customer = create(:customer)

resource_params = attributes_for(:log_entry).merge(
arbitrarily: {
nested: {
params: {
logeable: {
type: "Administrate::Field::Polymorphic",
value: customer.to_global_id.to_s,
},
},
},
},
)

allow_any_instance_of(
LogEntryDashboard,
).to receive(:permitted_attributes).and_return(
[
arbitrarily: {
nested: {
params: {
logeable: [
efatsi marked this conversation as resolved.
Show resolved Hide resolved
:type,
:value,
],
},
},
},
],
)

LogEntry.attr_accessor :arbitrarily

post :create, log_entry: resource_params
Copy link

Choose a reason for hiding this comment

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

Rails/HttpPositionalArguments: Use keyword arguments instead of positional arguments for http call: post.

Copy link
Member

Choose a reason for hiding this comment

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

I'm happy to ignore this one.


logeable_in_params = subject.send(:resource_params).dig(
:arbitrarily,
:nested,
:params,
:logeable,
)
expect(logeable_in_params).to eq(customer)
end
end

describe "with invalid params" do
Expand Down