Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve attribute methods generation #2471

Merged
merged 1 commit into from
May 31, 2024

Conversation

casperisfine
Copy link

@casperisfine casperisfine commented May 31, 2024

I'm currently working through Ruby warnings in our app, and one of the top source is from ActiveModel Serializer sub classes.

Usually they look like:

class SomethingSerializer < AMS
  attributes :title, :body

  def title # method redefinition warning
    titleize
  end
end

One solution would be to call attributes at the end of the method, so that attributes skips defining these, but it's quite unatural.

Instead we can use the self alias trick to mark these generated methods as OK to redefine. An alternative would be to define them in an included module like Active Model does, but not sure if it's worth it.

While I was at it, I improved the way these methods are generated.

cc @bf4

lib/active_model/serializer.rb Outdated Show resolved Hide resolved
@bf4
Copy link
Member

bf4 commented May 31, 2024

@casperisfine @byroot looks fine to merge with the comment typo fixed. Can you add a comment to the https://github.com/rails-api/active_model_serializers/blob/0-9-stable/CHANGELOG.md while there? I can release a new patch after merge if you like.

I'm currently working through Ruby warnings in our app, and one of the
top source is from ActiveModel Serializer sub classes.

Usually they look like:

```
class SomethingSerializer < AMS
  attributes :title, :body

  def title # method redefinition warning
    titleize
  end
end
```

One solution would be to call `attributes` at the end of the method,
so that `attributes` skips defining these, but it's quite unatural.

Instead we can use the self alias trick to mark these generated
methods as OK to redefine. An alternative would be to define them
in an included module like Active Model does, but not sure if it's
worth it.

While I was at it, I improved the way these methods are generated.
@casperisfine
Copy link
Author

@bf4 done!

@bf4 bf4 merged commit 597fd24 into rails-api:0-9-stable May 31, 2024
7 checks passed
@casperisfine
Copy link
Author

@bf4 I just realized now that I forgot to ask for a release :P

I'm trying to grind down our warnings again, a release would be super appreciated.

@bf4
Copy link
Member

bf4 commented Sep 17, 2024

@casperisfine Done. Release 0.9.13

@byroot
Copy link

byroot commented Sep 17, 2024

🙇 ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants