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

Ensure delegated type with single type generates safely #2225

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def populate_role_accessors(mod, role, types)
mod.create_method(
"build_#{role}",
parameters: [create_rest_param("args", type: "T.untyped")],
return_type: "T.any(#{types.join(", ")})",
return_type: types.size > 1 ? "T.any(#{types.join(", ")})" : types[0],
)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,60 @@ def message_uuid; end

assert_equal(expected, rbi_for(:Entry))
end

it "generates RBI file for delegated_type with single type" do
add_ruby_file("schema.rb", <<~RUBY)
ActiveRecord::Migration.suppress_messages do
ActiveRecord::Schema.define do
create_table :entries do |t|
t.string :entryable_type
t.integer :entryable_id
end
end
end
RUBY

add_ruby_file("message.rb", <<~RUBY)
class Message < ActiveRecord::Base
end
RUBY

add_ruby_file("entry.rb", <<~RUBY)
class Entry < ActiveRecord::Base
delegated_type :entryable, types: %w[ Message ]
end
RUBY

expected = <<~RBI
# typed: strong

class Entry
include GeneratedDelegatedTypeMethods

module GeneratedDelegatedTypeMethods
sig { params(args: T.untyped).returns(Message) }
def build_entryable(*args); end

sig { returns(T::Class[T.anything]) }
def entryable_class; end

sig { returns(ActiveSupport::StringInquirer) }
def entryable_name; end

sig { returns(T.nilable(Message)) }
def message; end

sig { returns(T::Boolean) }
def message?; end

sig { returns(T.nilable(::Integer)) }
def message_id; end
end
end
RBI

assert_equal(expected, rbi_for(:Entry))
end
end
end
end
Expand Down
Loading