Skip to content

Commit

Permalink
Merge pull request #1755 from olivier-thatch/olivier-activerecord-nor…
Browse files Browse the repository at this point in the history
…malizes

Add support for Rails 7.1 normalization to Active Record columns compiler
  • Loading branch information
KaanOzkan committed Jan 10, 2024
2 parents 1823cd3 + 30d7c70 commit 92857a3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/tapioca/dsl/helpers/active_record_column_type_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ def type_for_activerecord_value(column_type)
"::String"
when ActiveRecord::Type::Serialized
serialized_column_type(column_type)
when defined?(ActiveRecord::Normalization::NormalizedValueType) &&
ActiveRecord::Normalization::NormalizedValueType
type_for_activerecord_value(column_type.cast_type)
when defined?(ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Uuid) &&
ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Uuid
"::String"
Expand Down
35 changes: 35 additions & 0 deletions spec/tapioca/dsl/compilers/active_record_columns_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,41 @@ def will_save_change_to_publication_date?; end
assert_includes(output, expected)
end

it "discovers cast type for normalized attributes" do
# Support for normalization was added in Rails 7.1 so this test is only relevant
# for that version and above.
return unless rails_version(">= 7.1")

add_ruby_file("schema.rb", <<~RUBY)
ActiveRecord::Migration.suppress_messages do
ActiveRecord::Schema.define do
create_table :posts do |t|
t.string :title
end
end
end
RUBY

add_ruby_file("post.rb", <<~RUBY)
class Post < ActiveRecord::Base
normalizes :title, with: ->(title) { title.titleize }
end
RUBY

expected = indented(<<~RBI, 4)
sig { returns(T.nilable(::String)) }
def title; end
sig { params(value: T.nilable(::String)).returns(T.nilable(::String)) }
def title=(value); end
sig { returns(T::Boolean) }
def title?; end
RBI

assert_includes(rbi_for(:Post), expected)
end

it "discovers custom type from signature on deserialize method" do
add_ruby_file("schema.rb", <<~RUBY)
ActiveRecord::Migration.suppress_messages do
Expand Down

0 comments on commit 92857a3

Please sign in to comment.