Skip to content

Commit

Permalink
Merge pull request #112 from Shopify/at-fix-ar-changes-types
Browse files Browse the repository at this point in the history
ActiveRecord changes return nilable types
  • Loading branch information
Morriar committed Aug 26, 2020
2 parents 1177461 + 518553f commit c2ddb09
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
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

0 comments on commit c2ddb09

Please sign in to comment.