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

Add support for editing polymorphic fields - Revisited #1020

Merged
merged 14 commits into from Nov 17, 2017

Conversation

@pablobm
Copy link
Collaborator

pablobm commented Nov 1, 2017

This adds support for editing polymorphic fields. Props go to @pedantic-git, on whose work this is based. This PR supersedes #984.

An important aspect of this PR is how it handles the polymorphic value. At heart, polymorphic values are split in two: an id and a type. Using the globalid gem we can obtain a string that represents both. However, we still need to tell that this string is indeed a globalid and not a literal value when we get to Administrate::ApplicationController#resource_params. I handle this by splitting the field in two:

  • my_field[value]: the global id, which includes both type and instance id.
  • my_field[type]: literally "Administrate::Field::Polymorphic"

Then #resource_params can look at the param values and treat them appropriately. If it's a simple value such a string, it does nothing special. However, if it comes in this value/type form, we treat it as polymorphic, reading the global id.

@@ -16,7 +17,7 @@
end
end

it { should_permit_param(:foo, for_attribute: :foo) }
it { should_permit_param({ foo: [:type, :value] }, for_attribute: :foo) }

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Nov 1, 2017

Use %i or %I for an array of symbols.


expect(page).to have_link(customer.name)
expect(page).to have_flash(
t("administrate.controller.create.success", resource: "LogEntry")

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Nov 1, 2017

Put a comma after the last parameter of a multiline method call.

LogEntry.create!({
action: 'create',
logeable: order,
})

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Nov 1, 2017

Indent the right brace the same as the first position after the preceding left parenthesis.

@@ -53,6 +63,10 @@
address_state: Faker::Address.state_abbr,
address_zip: Faker::Address.zip,
)
LogEntry.create!({
action: 'create',

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Nov 1, 2017

Use 2 spaces for indentation in a hash, relative to the first position after the preceding left parenthesis.
Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

@@ -53,6 +63,10 @@
address_state: Faker::Address.state_abbr,
address_zip: Faker::Address.zip,
)
LogEntry.create!({

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Nov 1, 2017

Redundant curly braces around a hash parameter.

@@ -27,8 +28,11 @@
"Administrate::Field::Polymorphic",
display_associated_resource: product.name,
data: product,
attribute: 'product',

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Nov 1, 2017

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

end

double("Form Builder").as_null_object.tap do |form_builder|
allow(form_builder).to receive(:fields_for) do |*args, &block|

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Nov 1, 2017

Unused block argument - args. If it's necessary, use _ or _args as an argument name to indicate that it won't be used.

end

def permitted_attribute
{ attribute => [:type, :value] }

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Nov 1, 2017

Use %i or %I for an array of symbols.

end

def self.permitted_attribute(attr)
{ attr => [:type, :value] }

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Nov 1, 2017

Use %i or %I for an array of symbols.

@@ -120,7 +120,20 @@ def resource_includes

def resource_params
params.require(resource_class.model_name.param_key).
permit(dashboard.permitted_attributes)
permit(dashboard.permitted_attributes).
transform_values {|v| read_param_value(v) }

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Nov 1, 2017

Space between { and | missing.

@pedantic-git

This comment has been minimized.

Copy link
Contributor

pedantic-git commented Nov 1, 2017

Wow! This is amazing! Thanks for your additional work to bring this up to the right standard!

end
end
end

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Nov 2, 2017

Extra empty line detected at block body end.

logeable: {
type: "Administrate::Field::Polymorphic",
value: logeable.to_global_id.to_s,
}

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Nov 2, 2017

Put a comma after the last parameter of a multiline method call.

describe "with valid params" do
it "creates a new LogEntry" do
customer = create(:customer)
expect {

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Nov 2, 2017

Avoid using {...} for multi-line blocks.

logeable: {
type: "Administrate::Field::Polymorphic",
value: logeable.to_global_id.to_s,
}

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Nov 2, 2017

Put a comma after the last parameter of a multiline method call.

@pablobm pablobm force-pushed the edit_polymorphic-pablobm branch from f6b6981 to 6003e99 Nov 2, 2017
@pablobm pablobm force-pushed the edit_polymorphic-pablobm branch from 6003e99 to eaa7c5e Nov 2, 2017
@pablobm

This comment has been minimized.

Copy link
Collaborator Author

pablobm commented Nov 2, 2017

I'm going to leave this to rest for a week, just in case there is any more feedback. I'll merge it Friday next week if there are no issues.

@ewilliam

This comment has been minimized.

Copy link

ewilliam commented Nov 16, 2017

will this be merged soon? thanks @pablobm for your work!

@pablobm

This comment has been minimized.

Copy link
Collaborator Author

pablobm commented Nov 17, 2017

I shall...

make it so

@pablobm pablobm merged commit 8329b21 into master Nov 17, 2017
2 checks passed
2 checks passed
ci/circleci Your tests passed on CircleCI!
Details
hound No violations found. Woof!
@tysongach tysongach deleted the edit_polymorphic-pablobm branch Nov 17, 2017
manuelmeurer added a commit to krautcomputing/administrate that referenced this pull request Dec 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.