Skip to content

Add protection of stubbed methods from being called within a db transaction#9

Open
nepalez wants to merge 2 commits intomasterfrom
feature/isolate
Open

Add protection of stubbed methods from being called within a db transaction#9
nepalez wants to merge 2 commits intomasterfrom
feature/isolate

Conversation

@nepalez
Copy link
Owner

@nepalez nepalez commented Sep 21, 2019

Use the within_transaction: false for the protection:

---
- class: GoogleTranslateDiff
- chain: translate
- within_transaction: false
- arguments:
    - Color
    - :from: en
      :to:   de
- actions:
    - return: Farbe

This method will raise an exception if the method is executed in a transaction:

GoogleTranslateDiff.translate "Color", from: "en", to: "de"
# => "Farbe"  

# but not within a transaction
ActiveRecord::Base.transaction do
  GoogleTranslateDiff.translate "Color", from: "en", to: "de"
  # => raise #<Isolator::UnsafeOperationError ...>
end

With a new setting `within_transaction: false` you can prevent
calling a stabbed method within a database transaction.

```yaml
---
- class: Foo
  chain:
    - bar
    - baz
  within_transaction: false
  actions:
    - return: 0
```

If you call the method inside a transaction, the call will raise
an exception `::Isolator::UnsafeOperationError` pointing to
the method `Foo.bar.baz(*)`.

Notice that this setting is argument-specific. You can allow it
to be called inside a transaction with some arguments, and
<explicitly> forbid for some other arguments at the same time.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant