Skip to content

Conditional block executed in the context of serializer method#119

Merged
Bhacaz merged 3 commits intomasterfrom
conditional_instance_exec
Jul 9, 2025
Merged

Conditional block executed in the context of serializer method#119
Bhacaz merged 3 commits intomasterfrom
conditional_instance_exec

Conversation

@Bhacaz
Copy link
Collaborator

@Bhacaz Bhacaz commented Jun 27, 2025

Allow the proc passed to the conditional option to call an instance method of the serializer.

class UserSerializer
  include BrightSerializer::Serializer
  
  attributes :first_name, :last_name
  attribute :name, if: proc { |_object| add_name? } do |object|
    "#{object.first_name} #{object.last_name}"
  end

  def add_name?
    false
  end
end

Before the add_name? in the proc could not use the add_name? defined in the serializer.

With this change it can.

@Bhacaz Bhacaz requested a review from Copilot June 27, 2025 17:34

This comment was marked as resolved.

@Bhacaz Bhacaz requested a review from IsaBella-Marchand June 27, 2025 18:20
alias attribute attributes

def has_one(key, serializer:, **options, &block) # rubocop:disable Naming/PredicateName
def has_one(key, serializer:, **options, &block) # rubocop:disable Naming/PredicatePrefix

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pourquoi cette modification sur le nom de la cop qui est disable?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Le Gemfile.lock est pas commit dans ce project (c'est une pratique qui existe avec les gem qui est discutable, perso j'aime pas)

Puisque le Gemfile.lock est pas commit, le CI install toujours la dernière version de Rubocop, et une certaine version à modifier le nom de la cop.

@Bhacaz Bhacaz requested a review from IsaBella-Marchand July 7, 2025 14:45
@Bhacaz Bhacaz merged commit bbd61d8 into master Jul 9, 2025
8 checks passed
@Bhacaz Bhacaz deleted the conditional_instance_exec branch July 9, 2025 13:09
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