Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/tapioca/dsl/compilers/active_record_associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ def populate_associations(mod)
add_error(<<~MSG.strip)
Cannot generate association `#{declaration(reflection)}` on `#{constant}` since the constant `#{error.class_name}` does not exist.
MSG
rescue StandardError => error

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We think this rescue is a bit of an overkill for one method missing. Rescuing every error and generating potentially incorrect RBIs with no one noticing it would be a real possibility with this change.

Looking at the monkey patching in the test it seems to break Rails internals that we rely on. Can this be fixed in that monkey patch instead?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think you are right and it should be fixed in the monkey-patch for rails.

add_error(<<~MSG.strip)
Cannot generate association `#{association_name}` on `#{constant}` because of `#{error}`.".
MSG
end
end

Expand Down
42 changes: 42 additions & 0 deletions spec/tapioca/dsl/compilers/active_record_associations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1337,6 +1337,48 @@ def reload_post; end
expect_dsl_compiler_errors!
end

it "generates RBI file for monkey patches reflections" do
add_ruby_file("schema.rb", <<~RUBY)
ActiveRecord::Migration.suppress_messages do
ActiveRecord::Schema.define do
create_table :posts do |t|
end
end
end
RUBY

add_ruby_file("custom_reflection.rb", <<~RUBY)
class CustomReflection
def parent_reflection
end
end
RUBY

add_ruby_file("post.rb", <<~RUBY)
class Post < ActiveRecord::Base
::ActiveRecord::Reflection.add_reflection(self, :monkey, CustomReflection.new)
end
RUBY

expected = <<~RBI
# typed: strong

class Post
include GeneratedAssociationMethods

module GeneratedAssociationMethods; end
end
RBI

assert_equal(expected, rbi_for(:Post))

assert_equal(1, generated_errors.size)

assert_match(Regexp.new(<<~MSG.strip), generated_errors.first)
Cannot generate association `monkey` on `Post` because of `undefined method `collection?.*
MSG
end

it "generates RBI file for broken associations" do
add_ruby_file("schema.rb", <<~RUBY)
ActiveRecord::Migration.suppress_messages do
Expand Down