-
Notifications
You must be signed in to change notification settings - Fork 142
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
Suggestion to map fields for composed interactions #405
Comments
This sounds a lot like #245. I agree that something like this would be conceptually nice. So far, it hasn't been worth the effort. |
Done proof of concept class RemapOptions
def initialize interaction, options
@interaction = interaction
@options = options
end
def compose(*args)
@interaction.send :compose, *args
rescue ActiveInteraction.const_get(:Interrupt) => interrupt
remaped_errors = ActiveInteraction::Errors.new(interrupt.errors.instance_variable_get("@base"))
interrupt.errors.each do |attr, error|
to = @options[attr] || attr
remaped_errors.add to, error
end
raise ActiveInteraction.const_get(:Interrupt), remaped_errors
end
end
class A < ActiveInteraction::Base
string :phone
def execute
map_errors({different_field: :phone}).
compose(B, different_field: phone)
end
def map_errors opts
RemapOptions.new(self, opts)
end
end
class B < ActiveInteraction::Base
string :different_field
validates :different_field, length: { minimum: 10, maximum: 15 }
def execute
end
end
outcome = A.run(phone: 'asd')
puts outcome.errors.full_messages.join(' and ') outcome
|
Cool! I should have clarified, though. We haven't held off on doing this because we don't know how. Rather, we don't think it's worth the additional complexity, both in terms of ActiveInteraction's interface and implementation. |
I think this is a problem worth solving. It doesn't come up a ton but when it does it's rather annoying. My problem with solutions like compose(B, different_field: phone) do
{ different_field: :phone }
end is that they force you to repeat yourself. I was thinking about this and we might be able to add a method that fixes this. By wrapping the value passed in a method we can send the value and map the error. compose(B, different_field: is(:phone)) The Thoughts? |
I like the proposed solution. The It also should be possible to pass values manually, but I think the method can accept the optional second parameter. compose(B, different_field: link_errors(:phone, formatted_phone)) |
Version 4.0.0 does away with the idea of automatically linking errors for you. Instead they'll all need to be manually linked. There are two ways to do this. One is like we've discussed above using a method called class I < ActiveInteraction::Base
float :a, :b
def execute
compose(Add, x: link(:a), y: link(:b))
end
end The second is a shortcut for when interactions share a variable name and you want them linked. It creates a hash of the appropriate links. class I < ActiveInteraction::Base
float :x, :y
def execute
compose(Add, autolink(:x, :y))
end
end This will take care of the mapping filters when composing interactions. |
After thinking about that i think it should be opt-out rather than opt-in. And here is why If I don't want autolinking I just use On the second note. Does |
Using compose(I, inputs) # => all errors moved to :base
compose(I, autolink(*inputs.keys)) # => all errors moved to their exactly named counterparts I think automatically moving errors is probably more bad than good. Right now I don't have it accepting a custom value. It seems like a reasonable request. I'll give it some thought. |
ah, right, that makes sense to merge them to Thanks for the explanation and great gem |
this relates to
it's a big assumption and restriction to keep the field names consistent between interaction. My suggestion is to provide options to
compose
which will map nested errors into new fields on the parent. In other words it will remap error fields.For example
As a result
outcome.errors
will have error forphone
field.Another idea for the syntax
Why?
The text was updated successfully, but these errors were encountered: