Skip to content

Fix conditional procedures with lambdas - #59

Merged
stas merged 1 commit into
jsonapi-serializer:masterfrom
attilahorvath:fix-conditional-procedures-with-lambdas
Feb 21, 2020
Merged

Fix conditional procedures with lambdas#59
stas merged 1 commit into
jsonapi-serializer:masterfrom
attilahorvath:fix-conditional-procedures-with-lambdas

Conversation

@attilahorvath

Copy link
Copy Markdown
Contributor

What is the current behavior?

Conditional procedures for scalars and relationships don't work when used with lambdas, for example:

class MovieSerializer
  include FastJsonapi::ObjectSerializer

  attribute :release_year, if: ->(record) { record.release_year >= 2000 }
  has_many :actors, if: ->(record) { record.actors.any? }
end

These currently throw an error: ArgumentError: wrong number of arguments (given 2, expected 1).

This is very similar to the &:proc shorthand issue I've been working on at #58.

What is the new behavior?

This change fixes the behavior to be able to use lambdas as well as Procs with either one or two parameters.

This also includes the arity check for lambdas, which could be extracted to a common place.

Checklist

Please make sure the following requirements are complete:

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been reviewed and added / updated if needed (for bug fixes /
    features)
  • All automated checks pass (CI/CD)

@stas stas mentioned this pull request Feb 14, 2020
3 tasks
@attilahorvath
attilahorvath requested a review from stas February 21, 2020 12:43
@attilahorvath

Copy link
Copy Markdown
Contributor Author

@stas I've updated this to use the new method introduced in the previous PR #58. I've also made sure this is being used everywhere in the gem where procs are called, which means everything should work now with either do..end blocks, lambdas, procs and with the &:proc shorthand syntax.

Comment thread lib/fast_jsonapi/object_serializer.rb
@stas
stas merged commit 3faca2d into jsonapi-serializer:master Feb 21, 2020
@stas

stas commented Feb 21, 2020

Copy link
Copy Markdown
Collaborator

Thanks @attilahorvath 🙇‍♂️

@attilahorvath
attilahorvath deleted the fix-conditional-procedures-with-lambdas branch February 21, 2020 14:49
@stas stas mentioned this pull request Feb 27, 2020
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.

2 participants