bullet-train-pro / bullet_train-action_models

Other
5 stars 1 forks source link

Bail out early if ProcessesAsync discovers it has been included in the wrong order #43

Closed petekeen-cf closed 1 year ago

petekeen-cf commented 1 year ago

ActionModel::ProcessesAsync must be included after ActionModel::Base and if it isn't bad things happen.

In this case:

class SomeAction < ApplicationRecord
  include ActionModel::TargetsMany
  include ActionModel::ProcessesAsync
end

TargetsMany requires Base which defines the dispatch method to just call perform. ProcessesAsync overrides the dispatch method to instead kick a Sidekiq job.

However, in the inverse case:

class SomeAction < ApplicationRecord
  include ActionModel::ProcessesAsync
  include ActionModel::TargetsMany
end

Base's dispatch will override ProcessesAsync's dispatch and cause the job to run inline, which we can safely assume is never the author's intent.

This PR fatally errors when this situation is detected. We exit instead of raise because an exception thrown in included is sometimes, but not always, caught in loading and I couldn't figure out why or how.