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

Be able to pass in a model that isn't strictly a certain class #169

Closed
axsuul opened this issue Apr 9, 2014 · 4 comments
Closed

Be able to pass in a model that isn't strictly a certain class #169

axsuul opened this issue Apr 9, 2014 · 4 comments
Labels

Comments

@axsuul
Copy link

axsuul commented Apr 9, 2014

We work with concerns, a lot, and we constantly need to pass in a variety of models that share the same concern into an interaction, but it appears that only model is supported and it seems to complain if it isn't a specific class that's passed in. So far how we are doing it is:

class ChangeBankAccountInteraction < ActiveInteraction::Base
  string :account_number

  def initialize(params = {})
    @bank_accountable = params[:bank_accountable]

    super
  end

  def execute
    @bank_accountable.change_bank_account!(account_number)
  end
end

# How we pass it in
@change_bank_account = ChangeBankAccountInteraction.run(bank_accountable: @customer)
@change_bank_account = ChangeBankAccountInteraction.run(bank_accountable: @worker)

but this is cringe worthy. Is there a recommended way to do this or any plans to support it?

@AaronLasseigne
Copy link
Owner

As a hack that's perhaps a bit cleaner you can specify the class of the model to be a common ancestor of the two.

class ChangeBankAccountInteraction < ActiveInteraction::Base
  model :bank_accountable, class: Object
  ...
end

It seems like what you really want is an interface check. We've discussed the possibility of doing that but we do not currently have it on our roadmap.

@tfausak
Copy link
Collaborator

tfausak commented Apr 9, 2014

The model filter should gracefully handle concerns out of the box.

module Concern
  extend ActiveSupport::Concern
end

class Model
  include Concern
end

class Interaction < ActiveInteraction::Base
  model :concern

  def execute
    inputs
  end
end

Interaction.run!(concern: Model.new)
# => {:concern=>#<Model:0x007ff22e944d50>}

@AaronLasseigne
Copy link
Owner

Right, the included modules act as ancestors so the check works. Tada, we do support it!

@axsuul
Copy link
Author

axsuul commented Apr 9, 2014

Woohoo! I should've thought about trying that, argh. Is this a good time to say ActiveInteraction rocks? Seriously, my socks are off. Keep up the great work guys, thanks!

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

No branches or pull requests

3 participants