Skip to content

Add support for foreign_type#1425

Closed
mswiszcz wants to merge 3 commits intothoughtbot:masterfrom
mswiszcz:add-support-for-foreign-type
Closed

Add support for foreign_type#1425
mswiszcz wants to merge 3 commits intothoughtbot:masterfrom
mswiszcz:add-support-for-foreign-type

Conversation

@mswiszcz
Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

mswiszcz commented Mar 6, 2021

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

@mcmire
Copy link
Copy Markdown
Collaborator

mcmire commented Mar 6, 2021

@mswiszcz Yup! Re-running now.

Copy link
Copy Markdown
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?

end

def foreign_type_matches?
return true unless options.key?(:foreign_type)
Copy link
Copy Markdown
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?
)

end

def foreign_type
type = if [:has_one, :has_many].include?(macro)
Copy link
Copy Markdown
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?

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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