pocke / rbs_rails

Apache License 2.0
285 stars 34 forks source link

generated rbs for activerecord models does not respect `serialize` #227

Open tlubz opened 2 years ago

tlubz commented 2 years ago

The rbs generated for an ActiveRecord model that uses serialize :some_field, Array does not generate the correct RBS for it. ex

My situation is as follows:

schema.rb:

...
create_table "foos", id: :serial, force: :cascade do |t|
    ...
    t.text "photos"
    ...
  end
...

foo.rb:

class Foo  < ApplicationRecord
  serialize :photos, Array
  ...

it generates the following RBS in foo.rbs:

...
    def photos: () -> String?
    def photos=: (String?) -> String?
    def photos?: () -> bool
    def photos_changed?: () -> bool
    def photos_change: () -> [ String?, String? ]
    def photos_will_change!: () -> void
    def photos_was: () -> String?
    def photos_previously_changed?: () -> bool
    def photos_previous_change: () -> Array[String?]?
    def photos_previously_was: () -> String?
    def photos_before_last_save: () -> String?
    def photos_change_to_be_saved: () -> Array[String?]?
    def photos_in_database: () -> String?
    def saved_change_to_photos: () -> Array[String?]?
    def saved_change_to_photos?: () -> bool
    def will_save_change_to_photos?: () -> bool
    def restore_photos!: () -> void
    def clear_photos_change: () -> void
...

in this case, these should really be typed as Array[String]?, Array[Object]?, Array[untyped] or something similar...

e.g.

    def photos: () -> Array[String]?
    def photos=: (Array[String]?) -> Array[String]?
    def photos?: () -> bool
    def photos_changed?: () -> bool
    def photos_change: () -> [ Array[String]?, Array[String]? ]
    def photos_will_change!: () -> void
    def photos_was: () -> Array[String]?
    def photos_previously_changed?: () -> bool
    def photos_previous_change: () -> Array[Array[String]?]?
    def photos_previously_was: () -> Array[String]?
    def photos_before_last_save: () -> Array[String]?
    def photos_change_to_be_saved: () -> Array[Array[String]?]?
    def photos_in_database: () -> Array[String]?
    def saved_change_to_photos: () -> Array[Array[String]?]?
    def saved_change_to_photos?: () -> bool
    def will_save_change_to_photos?: () -> bool
    def restore_photos!: () -> void
    def clear_photos_change: () -> void