AaronLasseigne / active_interaction

:briefcase: Manage application specific business logic.
MIT License
2.06k stars 136 forks source link

Issue with the `interface` filter after upgrading from 3.8.2 to 4.1.0 #520

Closed jahammo2 closed 2 years ago

jahammo2 commented 2 years ago

Hey!

First of all, I've been using this gem for like 5 years and love the work you all have done. Thank you.

Second, I'm having an issue with the filters after upgrading.

The problem

I've upgraded to 4.1.0 and the interface filter is no longer working as expected. Specifically, I used to be able to pass something like User.first to an interaction with a filter like interface :user but not anymore. Now it that will throw an ActiveModel::Error. Here are the code details.

The interaction

class PublicStudySet::Add < ActiveInteraction::Base
  interface :public_study_set
  interface :user

  def execute
    ...
  end
end

The code I run

from rails c:

PublicStudySet::Add.run(public_study_set: StudySet.first, user: User.first).errors

The full result I get

# < PublicStudySet::Add: 0x00007f8314b57860 @_interaction_raw_inputs = {
  : public_study_set => # < StudySet id: 1,
  title: "Health Science1.4354016743100768",
  ...
  is_public: true > ,
  : user => # < User id: 1,
  provider: "email",
  ...
  login_token_valid_until: nil >
}, @_interaction_inputs = {
  : public_study_set => # < StudySet id: 1,
  title: "Health Science1.4354016743100768",
  ...
  is_public: true > ,
  : user => # < User id: 1,
  provider: "email",
  ...
  login_token_valid_until: nil >
}, @public_study_set = # < StudySet id: 1, title: "Health Science1.4354016743100768", ..., is_public: true > , @user = # < User id: 1, provider: "email", ..., login_token_valid_until: nil > , @_interaction_errors = # < ActiveInteraction::Errors: 0x00007f831563bec0 @base = # < PublicStudySet::Add: 0x00007f8314b57860... > ,
  @errors = [
    # < ActiveModel::Error attribute = public_study_set, type = invalid_type, options = { : type => "interface" } > ,
    # < ActiveModel::Error attribute = user, type = invalid_type, options = { : type => "interface" } >
  ] > ,
  @_interaction_result = nil, @validation_context = nil, @_interaction_valid = false >

My gem versions

ruby '2.7.1'

gem 'rails', '~> 6.1'

gem 'active_interaction', '~> 4.1.0'

Worth noting

Follow ups

Is there something missing that I was supposed to do in the upgrade? Perhaps interface no longer functions as simply as it used to and I need to change it in some way?

Thank you!

jahammo2 commented 2 years ago

I have figured out the issue.

Something might be different about interface now. However, I was probably not using it properly from the beginning. I have since switched over to using object or record and things are working now. However, it appears you must add the class option (or the from option). That's where I went wrong when I initially tried using object and record. I should have done this:

  record :public_study_set, class: StudySet
  object :user, class: User

I admittedly don't know when I should use record vs object. I just know they both seem to work interchangeably for my app.

jahammo2 commented 2 years ago

I am going to close this issue now. But I really want to let you know, Aaron, that I am so thankful for the work you have put into this gem. It's close to a decade of you maintaining it and I am so, so appreciative of it. I know it's not easy and takes a lot of your time. So, thank you very, very much.