Skip to content
This repository has been archived by the owner on Jul 5, 2023. It is now read-only.

Support for serialize? #49

Closed
DMA57361 opened this issue Jun 28, 2019 · 3 comments
Closed

Support for serialize? #49

DMA57361 opened this issue Jun 28, 2019 · 3 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@DMA57361
Copy link

Just trying out Sorbet and this gem for the first time, so maybe there's a way to handle this already?

I have an ActiveRecord class that uses serialize field_name, Hash and the rake rails_rbi:models command has generated an rbi file where both the getter and setter methods for field_name are down as using the datatype T.nilable(String), which is the datatype of the underlying database field, rather than referencing Hash which is what those methods use thanks to serialize.

I've edited the generated rbi file for now, but wondered if there's a sig or something I should be writing on the class to make this work already? Or maybe this is a feature request for the gem?

@hdoan741 hdoan741 added enhancement New feature or request good first issue Good for newcomers labels Jun 28, 2019
@hdoan741
Copy link
Contributor

hdoan741 commented Jun 28, 2019

Hi @DMA57361. We are mainly based on Database type, but we'll look into support for serialize and custom types. Meanwhile, you can actually override the generated RBI instead of editing the file directly.

If you add your own model.rbi and redefine the methods with correct types, Sorbet should pick up the correct type.

app/models/model_name.rbi

class ModelName
  sig { returns(T::Hash[...]) }
  def field_name; end

  sig { params(obj: T::Hash[....]).void }
  def field_name=(obj); end
end

This way, your overridden signature don't go away when you regenerate the files. We supported this in 0.2.0. I'll add this to the README.

You are also welcome to submit a PR to fix the problem.

By the way, could you tell us your environment so that we can reproduce the bug when working on this?

Version Used:

  • Rails:
  • Sorbet:
  • Sorbet-rails:

@hdoan741
Copy link
Contributor

Added the workaround to documentation in #51

@hdoan741
Copy link
Contributor

Supported in #385! Thanks @alexdunae!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants