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

ActiveRecord changes return nilable types #112

Merged
merged 1 commit into from
Aug 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 14 additions & 8 deletions lib/tapioca/compilers/dsl/active_record_columns.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,25 +196,25 @@ def add_methods_for_attribute(klass, constant, column_name, attribute_name = col
klass,
"#{attribute_name}_before_last_save",
methods_to_add,
return_type: getter_type
return_type: as_nilable_type(getter_type)
)
add_method(
klass,
"#{attribute_name}_change_to_be_saved",
methods_to_add,
return_type: "[#{getter_type}, #{getter_type}]"
return_type: "T.nilable([#{getter_type}, #{getter_type}])"
)
add_method(
klass,
"#{attribute_name}_in_database",
methods_to_add,
return_type: getter_type
return_type: as_nilable_type(getter_type)
)
add_method(
klass,
"saved_change_to_#{attribute_name}",
methods_to_add,
return_type: "[#{getter_type}, #{getter_type}]"
return_type: "T.nilable([#{getter_type}, #{getter_type}])"
)
add_method(
klass,
Expand All @@ -235,7 +235,7 @@ def add_methods_for_attribute(klass, constant, column_name, attribute_name = col
klass,
"#{attribute_name}_change",
methods_to_add,
return_type: "[#{getter_type}, #{getter_type}]"
return_type: "T.nilable([#{getter_type}, #{getter_type}])"
)
add_method(
klass,
Expand All @@ -252,13 +252,13 @@ def add_methods_for_attribute(klass, constant, column_name, attribute_name = col
klass,
"#{attribute_name}_was",
methods_to_add,
return_type: getter_type
return_type: as_nilable_type(getter_type)
)
add_method(
klass,
"#{attribute_name}_previous_change",
methods_to_add,
return_type: "[#{getter_type}, #{getter_type}]"
return_type: "T.nilable([#{getter_type}, #{getter_type}])"
)
add_method(
klass,
Expand All @@ -270,7 +270,7 @@ def add_methods_for_attribute(klass, constant, column_name, attribute_name = col
klass,
"#{attribute_name}_previously_was",
methods_to_add,
return_type: getter_type
return_type: as_nilable_type(getter_type)
)
add_method(
klass,
Expand Down Expand Up @@ -381,6 +381,12 @@ def lookup_arg_type_of_method(column_type, method)

arg_type.to_s
end

sig { params(type: String).returns(String) }
def as_nilable_type(type)
return type if type.start_with?("T.nilable(")
"T.nilable(#{type})"
end
end
end
end
Expand Down
24 changes: 12 additions & 12 deletions spec/tapioca/compilers/dsl/active_record_columns_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ def id_before_type_cast; end
sig { returns(T::Boolean) }
def id_came_from_user?; end

sig { returns([T.nilable(::Integer), T.nilable(::Integer)]) }
sig { returns(T.nilable([T.nilable(::Integer), T.nilable(::Integer)])) }
def id_change; end

sig { returns([T.nilable(::Integer), T.nilable(::Integer)]) }
sig { returns(T.nilable([T.nilable(::Integer), T.nilable(::Integer)])) }
def id_change_to_be_saved; end

sig { returns(T::Boolean) }
Expand All @@ -123,7 +123,7 @@ def id_changed?; end
sig { returns(T.nilable(::Integer)) }
def id_in_database; end

sig { returns([T.nilable(::Integer), T.nilable(::Integer)]) }
sig { returns(T.nilable([T.nilable(::Integer), T.nilable(::Integer)])) }
def id_previous_change; end

sig { returns(T::Boolean) }
Expand All @@ -141,7 +141,7 @@ def id_will_change!; end
sig { void }
def restore_id!; end

sig { returns([T.nilable(::Integer), T.nilable(::Integer)]) }
sig { returns(T.nilable([T.nilable(::Integer), T.nilable(::Integer)])) }
def saved_change_to_id; end

sig { returns(T::Boolean) }
Expand Down Expand Up @@ -440,10 +440,10 @@ def author_before_type_cast; end
sig { returns(T::Boolean) }
def author_came_from_user?; end

sig { returns([T.nilable(::String), T.nilable(::String)]) }
sig { returns(T.nilable([T.nilable(::String), T.nilable(::String)])) }
def author_change; end

sig { returns([T.nilable(::String), T.nilable(::String)]) }
sig { returns(T.nilable([T.nilable(::String), T.nilable(::String)])) }
def author_change_to_be_saved; end

sig { returns(T::Boolean) }
Expand All @@ -452,7 +452,7 @@ def author_changed?; end
sig { returns(T.nilable(::String)) }
def author_in_database; end

sig { returns([T.nilable(::String), T.nilable(::String)]) }
sig { returns(T.nilable([T.nilable(::String), T.nilable(::String)])) }
def author_previous_change; end

sig { returns(T::Boolean) }
Expand All @@ -475,7 +475,7 @@ def restore_author!; end
assert_includes(output, expected)

expected = indented(<<~RUBY, 2)
sig { returns([T.nilable(::String), T.nilable(::String)]) }
sig { returns(T.nilable([T.nilable(::String), T.nilable(::String)])) }
def saved_change_to_author; end

sig { returns(T::Boolean) }
Expand Down Expand Up @@ -533,10 +533,10 @@ def body_before_type_cast; end
sig { returns(T::Boolean) }
def body_came_from_user?; end

sig { returns([T.nilable(::String), T.nilable(::String)]) }
sig { returns(T.nilable([T.nilable(::String), T.nilable(::String)])) }
def body_change; end

sig { returns([T.nilable(::String), T.nilable(::String)]) }
sig { returns(T.nilable([T.nilable(::String), T.nilable(::String)])) }
def body_change_to_be_saved; end

sig { returns(T::Boolean) }
Expand All @@ -545,7 +545,7 @@ def body_changed?; end
sig { returns(T.nilable(::String)) }
def body_in_database; end

sig { returns([T.nilable(::String), T.nilable(::String)]) }
sig { returns(T.nilable([T.nilable(::String), T.nilable(::String)])) }
def body_previous_change; end

sig { returns(T::Boolean) }
Expand All @@ -571,7 +571,7 @@ def restore_body!; end
assert_includes(output, expected)

expected = indented(<<~RUBY, 2)
sig { returns([T.nilable(::String), T.nilable(::String)]) }
sig { returns(T.nilable([T.nilable(::String), T.nilable(::String)])) }
def saved_change_to_body; end

sig { returns(T::Boolean) }
Expand Down