Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR modernizes the ActiveModel integration to support both legacy positional hash initializers and new keyword argument syntax while preserving backward compatibility.
- Adds compatibility tests for positional hashes, keyword args, empty initialization, and attribute aliases
- Updates the generated
initializehelper to acceptparams = nil, **kwargsand dispatch correctly - Ensures positional arguments take precedence when both styles are provided
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| test/machine_initialization_compatibility_test.rb | New tests covering legacy and keyword initialization modes, empty init, attribute aliases, and precedence rules |
| lib/state_machines/integrations/active_model.rb | Refactored define_state_initializer to handle positional and keyword args and forward them to super |
Comments suppressed due to low confidence (1)
lib/state_machines/integrations/active_model.rb:356
- [nitpick] The parameter name
kwargsis less common in Ruby; consider renaming it tokeyword_paramsfor clarity and consistency with Ruby naming conventions.
def initialize(params = nil, **kwargs)
| params.transform_keys! do |key| | ||
| def initialize(params = nil, **kwargs) | ||
| # Support both positional hash and keyword arguments | ||
| attrs = params || kwargs |
There was a problem hiding this comment.
Using params || kwargs will treat an empty params hash as present and ignore keyword args even when they have values. Consider using attrs = params.nil? ? kwargs : params to explicitly check for an absent positional argument.
Suggested change
| attrs = params || kwargs | |
| attrs = params.nil? ? kwargs : params |
avoid breaking reality for apps that don't update the syntax
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Support legacy syntax.