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
Strategy pattern and implications #9
Comments
Please correct me if I wrong, but why not to use inheritance for different "strategies"? |
@IvanShamatov I don't have anything against it, it's another viable option. |
Confirmed the reason that errors.details[reflection_attribute] << error
errors.details[reflection_attribute].uniq! This is needed to be able to match the type of the error with:
... on nested errors. |
What if you avoid
Not sure what code do you mean exactly, the presentation of the error to the user? What problem are you solving with merging the validation messages to the same namespace? I see a good space for ambiguity, e.g. when root action and nested action accept the parameters with identical names, but that are semantically different, e.g. I side with Ivan,
seems cleaner to me than:
|
@pirj I think you are completely right about the matcher. I was migrating the specs which already did this style of testing when you About the strategies, the point with the pattern is that it should be decided at runtime, so the user might not even know which one is going to be used, and does not necessarily need to know either. In my real use case, the strategy is decided at runtime based on some other attribute, not passed as an argument. When I talk about namespaced errors as a problem, I mean that the keys of the errors are part of the interface of the action. If your keys are namespaced just be cause of how the action works, you are changing the interface of the action for no reason, and forcing everyone to work around it. IRL, my migration broke some specs on other files because they expected the errors to be on other keys. |
Does this issue still stand? |
Hey, I've pinged a few people in search of more input about this topic. I'm sure there will be some more comments. Otherwise, I'll be happy to close this. |
Closing due to lack of input. |
TL;DR:
raise_validation_error
does not work with errors from nested actionsvalidates :some_nested_action, nested_action: {merge_errors: true}
. By merging the errors we'd also circunvent the problems of point 1, but the problem would still persist.I've used nested actions to implement a strategy pattern, but found some problems that forced me to introduce some terrible monkeypatches.
Consider this example of a strategy pattern implemented using nested actions:
The problem that forces me to monkey patch around is that the provided matcher
raise_validation_error
relies onerror.details
, which is not populated in the same manner that with non nested validations. This is most probably related to how the validator works, which actually comes fromactive_data
gem.The other problem is also more or less of a blocker for using this kind of approach: the messages are namespaced under
strategy_action
, which may make sense in most situations, but not here, because both the strategies and the parent caller action would accept the same attributes, and the code that must do something with the errors does not expect the error not to be namespaced.I propose adding our own version of the nested action validator, which accepts the option
merge_errors
.In other notes, I think is worth considering adding a DSL for strategies so we could convert my first code example into something like this (with the option to move the implementation of the strategies into another class):
The text was updated successfully, but these errors were encountered: