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

MONGOID-4921 add discriminator_key as a class variable #4814

Merged
merged 24 commits into from Jul 16, 2020

Conversation

Neilshweky
Copy link
Contributor

Added discriminator key as an attr_accessor on the class. Pre-req to doing MONGOID-4817.

@Neilshweky Neilshweky requested a review from p-mongo July 13, 2020 16:22
lib/mongoid.rb Outdated Show resolved Hide resolved
spec/mongoid/traversable_spec.rb Outdated Show resolved Hide resolved
spec/mongoid/traversable_spec.rb Outdated Show resolved Hide resolved
spec/mongoid/traversable_spec.rb Show resolved Hide resolved
spec/support/models/instrument.rb Outdated Show resolved Hide resolved
spec/mongoid/traversable_spec.rb Outdated Show resolved Hide resolved
lib/mongoid/traversable.rb Outdated Show resolved Hide resolved
lib/mongoid/traversable.rb Outdated Show resolved Hide resolved
@p-mongo
Copy link
Contributor

p-mongo commented Jul 13, 2020

Let's not allow reads of discriminator_key on instance level and add a test for that.

lib/mongoid/errors/setting_discriminator_key_on_child.rb Outdated Show resolved Hide resolved
lib/mongoid/errors/setting_discriminator_key_on_child.rb Outdated Show resolved Hide resolved
lib/mongoid/traversable.rb Outdated Show resolved Hide resolved
lib/mongoid/traversable.rb Outdated Show resolved Hide resolved
lib/mongoid/traversable.rb Outdated Show resolved Hide resolved
spec/mongoid/config_spec.rb Outdated Show resolved Hide resolved
spec/mongoid/config_spec.rb Outdated Show resolved Hide resolved
spec/mongoid/traversable_spec.rb Outdated Show resolved Hide resolved
spec/mongoid/traversable_spec.rb Outdated Show resolved Hide resolved
spec/support/models/instrument.rb Outdated Show resolved Hide resolved
@Neilshweky Neilshweky requested a review from p-mongo July 14, 2020 16:39
@p-mongo p-mongo requested a review from egiurleo July 15, 2020 19:25
@Neilshweky Neilshweky requested a review from p-mongo July 15, 2020 19:29
Copy link
Contributor

@egiurleo egiurleo left a comment

Choose a reason for hiding this comment

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

This is so cool! I left some comments on the tests. ✨

super(
compose_message(
"invalid_discriminator_key_target",
{ class_name: class_name, superclass: superclass}
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick - missing a space before the closing curly brace.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good one! can't believe I missed that... It's bothering me like crazy just looking at it.

describe "#discriminator_key" do

context "when the discriminator key is not set on a class" do
it "equals _type" do
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you make this test description more descriptive?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done!

Comment on lines 262 to 273
it "the global discriminator key is _type" do
expect(Mongoid.discriminator_key).to eq("_type")
end

it "child discriminator keys equal _type: Piano" do
expect(Piano.discriminator_key).to eq("_type")
end

it "child discriminator keys equal _type: Guitar" do
expect(Guitar.discriminator_key).to eq("_type")
end
end
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't mean to be a stickler for rspec conventions (lol) but could you change these test descriptions so the line reads like a sentence? (e.g. it "sets the global discriminator key to _type")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure!

spec/mongoid/traversable_spec.rb Show resolved Hide resolved
Comment on lines 378 to 393
it "doesn't change in that class" do
expect(Guitar.discriminator_key).to eq("_type")
end

it "the global discriminator key is _type" do
expect(Mongoid.discriminator_key).to eq("_type")
end

it "doesn't change in the sibling" do
expect(Piano.discriminator_key).to eq("_type")
end

it "doesn't change in the parent" do
expect(Instrument.discriminator_key).to eq("_type")
end
end
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you need to add some sort of before block that tries to set the discriminator key on Guitar.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed it to a let block... does that work? Because I still have to test that it raises an error, and I don't know how kosher it is to use expect in a before block.

@Neilshweky Neilshweky requested a review from egiurleo July 16, 2020 12:27
Copy link
Contributor

@egiurleo egiurleo left a comment

Choose a reason for hiding this comment

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

Two more small comments but the tests are looking good :)

spec/mongoid/traversable_spec.rb Show resolved Hide resolved
end

context "when the discriminator key is changed in the child" do
let(:guitar) do
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you give this helper a slightly more descriptive name like set_discriminator_key?

@Neilshweky Neilshweky requested a review from egiurleo July 16, 2020 15:39
Copy link
Contributor

@egiurleo egiurleo left a comment

Choose a reason for hiding this comment

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

Thanks for taking the time to make your tests really clear 👍

@Neilshweky Neilshweky merged commit cd298bd into mongodb:master Jul 16, 2020
@Neilshweky Neilshweky deleted the MONGOID-4921 branch July 16, 2020 17:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants