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

Should still able to set other reflection successfully #62

Merged
merged 1 commit into from
Dec 20, 2023

Conversation

wendy-clio
Copy link
Contributor

@wendy-clio wendy-clio commented Dec 19, 2023

The error caught my attention while implementing another ticket.

Making sure the PolymorphicForeignAssociationExtension will be compatible with any reflection (in this case, ActiveRecord::Reflection::ThroughReflection)

Since this is such a minor fix, will not bump the version but re-release after the merge.

@wendy-clio wendy-clio self-assigned this Dec 19, 2023
@@ -371,4 +371,13 @@ class InlineDrink2 < ActiveRecord::Base
expect(link.normal_target_type).to eq("InlineDrink2")
end
end

context "when using other reflection" do
Copy link
Member

Choose a reason for hiding this comment

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

What is other? Can we be more specific?

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 specify one example underneath in it aka ActiveRecord::Reflection::ThroughReflection

@@ -3,7 +3,7 @@ module PolymorphicForeignAssociationExtension

def set_owner_attributes(record)
super
if reflection.foreign_integer_type && reflection.integer_type
if reflection.try(:foreign_integer_type) && reflection.try(:integer_type)
Copy link
Member

Choose a reason for hiding this comment

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

Hmm. My general feeling is that the use of try is a bit lazy => why not simply have those methods defined but return nil to that we end up with reflections continuing to look similar, versus needing to remember that we need to guard against some differences.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

you are more suggesting to something like this?

def foreign_integer_type(reflection)
  if reflection.respond_to? (:foreign_integer_type)
    reflection.foreign_integer_type
  else
    nil
  end  
end

or simple

def foreign_integer_type(reflection)
  reflection.try(: foreign_integer_type)
end

which feel like the same though

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also, I do not want to just adding moreattr_accessor to ThroughReflection in here https://github.com/clio/polymorphic_integer_type/blob/master/lib/polymorphic_integer_type/extensions.rb#L5-L8 in case there is more reflection I am not aware of.

Copy link

@MrJakeReid MrJakeReid left a comment

Choose a reason for hiding this comment

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

I've read Adam's suggestion and while I wouldn't be opposed to it, I don't inherently see the value in adding a method definition to guard against the edge case that's demonstrated in the specs. I think checking with try is acceptable, and if there are more edge cases then I'd be open to the refactor.

@wendy-clio wendy-clio merged commit 246e6c9 into master Dec 20, 2023
8 checks passed
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