Skip to content
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

Better support for interactions that wrap another interaction or model. #245

Closed
AaronLasseigne opened this issue Dec 22, 2014 · 9 comments
Closed

Comments

@AaronLasseigne
Copy link
Owner

Goal

Reduce redundancy in interactions that wrap an underlying interaction or model. Often these are used in form objects and/or replace the CUD operations.

Areas of Redundancy

  1. Filters
  2. Validation
@AaronLasseigne
Copy link
Owner Author

Pulling validation information off of a model is basically impossible so whatever we do we'll have to send the values to the underlying model/interaction, run the validation, and merge the results back into the wrapping interaction.

@AaronLasseigne
Copy link
Owner Author

A thought I had recently is that it might be good to just create two new compose type methods that allow for the creation or updating of an AR model. Something like, new_model and update_model.

@tfausak
Copy link
Collaborator

tfausak commented Apr 7, 2015

How would that look?

class CreateThing < ActiveInteraction::Base
  # ...
  def execute
    new_model(Thing, inputs) # ?
  end
end

@AaronLasseigne
Copy link
Owner Author

Yup, something like that.

@tfausak
Copy link
Collaborator

tfausak commented Apr 7, 2015

For some reason I feel weird about adding helper methods for ActiveRecord specifically. ActiveInteraciton doesn't rely on ActiveRecord, only ActiveModel. A more general solution may be to pass an optional method parameter to compose. Or something else, I'm not sure.

I think it might help to see what you'd have to do to create a new record spelled out, before we try to compress it into a helper method.

class CreateThing < ActiveInteraction::Base
  # ...
  def execute
    thing = Thing.new(inputs)
    errors.merge!(thing) unless thing.save
    thing
  end
end

This assumes that the interaction inputs map one-to-one to the record's attributes. And that the record does not use protected attributes (attr_protected or attr_accessible).

@AaronLasseigne
Copy link
Owner Author

Right, the fact that this is a common pattern and we can provide an error mapping feature makes it worth consideration in my mind.

@tfausak
Copy link
Collaborator

tfausak commented Apr 8, 2015

Currently compose calls run, then checks valid?, then returns the result. If it fails, it goes with merge_errors_onto_base instead of just merge! (#87).

new_model would call new, then check save, then return itself. If it failed, it would use merge!.

Would it make sense to allow setting those things when calling compose?

# new_model
compose(Thing, inputs, run: :new, check: :save, return: :itself)

Heck, it could even take a proc.

@AaronLasseigne
Copy link
Owner Author

I could get on board with that.

@tfausak
Copy link
Collaborator

tfausak commented Nov 7, 2016

#291 (comment)

This has been open for more than a year. We've been getting along fine without it. No one else has asked for it. I think we don't need it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants