Skip to content

Commit

Permalink
Support array types in typed_store compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
amomchilov committed Jun 14, 2024
1 parent e786469 commit 3b5f87f
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions lib/tapioca/dsl/compilers/active_record_typed_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ def decorate
stores.values.each do |store_data|
store_data.accessors.each do |accessor, name|
field = store_data.fields.fetch(accessor)
type = type_for(field.type_sym)
type = as_nilable_type(type) if field.null
type = type_for(field)
name ||= field.name # support < 1.5.0

generate_methods(store_accessors_module, name.to_s, type)
Expand Down Expand Up @@ -136,9 +135,21 @@ def gather_constants
T::Hash[Symbol, String],
)

sig { params(attr_type: Symbol).returns(String) }
def type_for(attr_type)
TYPES.fetch(attr_type, "T.untyped")
sig { params(field: ActiveRecord::TypedStore::Field).returns(String) }
def type_for(field)
type = TYPES.fetch(field.type_sym, "T.untyped")

type = if field.array
# `null: false` applies to the array itself, not the elements, which always nilable.
# https://github.com/byroot/activerecord-typedstore/blob/2f3fb98/spec/support/models.rb#L46C34-L46C45
# https://github.com/byroot/activerecord-typedstore/blob/2f3fb98/spec/active_record/typed_store_spec.rb#L854-L857
nilable_element_type = as_nilable_type(type)
"T::Array[#{nilable_element_type}]"
else
type
end

field.null ? as_nilable_type(type) : type
end

sig do
Expand Down

0 comments on commit 3b5f87f

Please sign in to comment.