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

Model filter fails for instances that override is_a? #179

Closed
tfausak opened this Issue May 2, 2014 · 4 comments

Comments

Projects
None yet
2 participants
@tfausak
Collaborator

tfausak commented May 2, 2014

Since the model filter uses a case statement (i.e., ===), you can confuse it by overriding #is_a?.

class Model
  def self.===(_)
    false
  end
end

class OtherModel
  def is_a?(_)
    true
  end
end

class Interaction < ActiveInteraction::Base
  model :model
  def execute; end
end

model = OtherModel.new
Model === model
# => false
model.is_a?(Model)
# => true
Interaction.run!(model: model)
# ActiveInteraction::InvalidInteractionError: Model is not a valid model

@tfausak tfausak added the bug label May 2, 2014

@tfausak

This comment has been minimized.

Show comment
Hide comment
@tfausak

tfausak May 2, 2014

Collaborator

On the other hand, you can override .=== on the class you're using for the model filter.

class Model
  def self.===(_)
    true
  end
end

class OtherModel
  def is_a?(_)
    false
  end
end

class Interaction < ActiveInteraction::Base
  model :model
  def execute; end
end

model = OtherModel.new
Model === model
# => true
model.is_a?(Model)
# => false
Interaction.run!(model: model)
# => nil
Collaborator

tfausak commented May 2, 2014

On the other hand, you can override .=== on the class you're using for the model filter.

class Model
  def self.===(_)
    true
  end
end

class OtherModel
  def is_a?(_)
    false
  end
end

class Interaction < ActiveInteraction::Base
  model :model
  def execute; end
end

model = OtherModel.new
Model === model
# => true
model.is_a?(Model)
# => false
Interaction.run!(model: model)
# => nil
@AaronLasseigne

This comment has been minimized.

Show comment
Hide comment
@AaronLasseigne

AaronLasseigne May 2, 2014

Owner

This is particularly problematic if you're testing in rspec with mock_model.

Owner

AaronLasseigne commented May 2, 2014

This is particularly problematic if you're testing in rspec with mock_model.

@tfausak

This comment has been minimized.

Show comment
Hide comment
@tfausak

tfausak May 2, 2014

Collaborator

How should we handle #instance_of??

Collaborator

tfausak commented May 2, 2014

How should we handle #instance_of??

@tfausak tfausak self-assigned this May 2, 2014

@AaronLasseigne

This comment has been minimized.

Show comment
Hide comment
@AaronLasseigne

AaronLasseigne May 2, 2014

Owner

I'm not sure instance_of? is necessary. Why would you overwrite instance_of? and not is_a??

Owner

AaronLasseigne commented May 2, 2014

I'm not sure instance_of? is necessary. Why would you overwrite instance_of? and not is_a??

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