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 foreign_type #1425

Closed

Conversation

mswiszcz
Copy link
Contributor

@mswiszcz mswiszcz commented Mar 5, 2021

Allows to create tests checking set foreign_type

class Visitor < ActiveRecord::Base
  belongs_to :location, foreign_type: :facility_type, polymorphic: true
end

class Hotel < Location
  has_many :visitors, inverse_of: :location, foreign_type: :facility_type, as: :location
end

class PrisonCell < Location
  has_one :visitor, inverse_of: :location, foreign_type: :facility_type, as: :location
end
expect(Visitor.new).to belong_to(:location).with_foreign_type(:facility_type)
expect(Hotel.new).to have_many(:visitors).with_foreign_type(:facility_type)
expect(PrisonCell.new).to have_one(:visitor).with_foreign_type(:facility_type)

@mswiszcz
Copy link
Contributor Author

mswiszcz commented Mar 6, 2021

Can someone rerun those checks? Seems like a dependency / gems server was temporarily unavailable

@mcmire
Copy link
Collaborator

mcmire commented Mar 6, 2021

@mswiszcz Yup! Re-running now.

Copy link
Collaborator

@mcmire mcmire left a comment

Choose a reason for hiding this comment

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

Hey @mswiszcz, sorry for the delay in reviewing this. I've left some comments below, would you mind taking another look?

@@ -1267,6 +1331,12 @@ def foreign_key_exists?
!(belongs_foreign_key_missing? || has_foreign_key_missing?)
end

def foreign_type_matches?
return true unless options.key?(:foreign_type)
Copy link
Collaborator

Choose a reason for hiding this comment

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

We don't typically return early or use the modifier form of unless. Since the end result is a boolean, what do you think about combining these into one conditional?

!options.key?(:foreign_type) || (
  !belongs_foreign_type_missing? &&
  !has_foreign_type_missing?
)

@@ -1472,6 +1579,16 @@ def foreign_key_reflection
end
end

def foreign_type
type = if [:has_one, :has_many].include?(macro)
Copy link
Collaborator

Choose a reason for hiding this comment

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

To simplify this expression and remove the temporary assignment, what do you think about moving the .to_s to has_column? at line 1509?

define_model :parent do
has_many :children, foreign_key: :ancestor_id, inverse_of: false
end

expect(Parent.new).to have_many(:children)
end

it 'accepts an association with a nonstandard type, with reverse association turned off' do
define_model :visitor, location_id: :integer, facitility_type: :string
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should this be facility_type?

@matsales28
Copy link
Member

I think this would be a nice addition, @mswiszcz do you still plan to address the feedback? If not I can address it and give you credits.

@mswiszcz
Copy link
Contributor Author

mswiszcz commented Jan 12, 2024

hey @matsales28, I totally forgot about that PR after changing project, sorry about that. Please feel free to take that over as I don't have much time at the moment to finish it

@matsales28
Copy link
Member

I'm closing this PR in favor of #1609

@matsales28 matsales28 closed this Feb 2, 2024
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.

3 participants