Skip to content

Add Micro::Case#rollback_on_failure alias for #transaction#150

Open
serradura wants to merge 1 commit into
mainfrom
feature/rollback-on-failure-alias
Open

Add Micro::Case#rollback_on_failure alias for #transaction#150
serradura wants to merge 1 commit into
mainfrom
feature/rollback-on-failure-alias

Conversation

@serradura

Copy link
Copy Markdown
Member

Summary

  • Micro::Case#transaction is shadowed by the attribute reader when a use case declares attribute :transaction (a domain object — e.g. a PaymentTransaction record — that has nothing to do with the database). Inside call!, transaction { ... } then calls the reader and silently drops the block.
  • This PR adds alias_method :rollback_on_failure, :transaction as a verbose, unambiguous escape hatch. Same signature, same resolution order (call-site with: > class macro > global default), same transaction(:activerecord) back-compat shim — alias_method makes the equivalence a property of the language, not two implementations to keep in sync.
  • The name matches the sibling gem solid-process's rollback_on_failure, so the two APIs read alike.
  • Purely additive — #transaction is byte-for-byte unchanged. No new Micro::Case::Check method needed (the alias inherits transaction_owner! / activerecord_loaded! for free).
class RefundPayment < Micro::Case
  attribute :transaction # a domain object, not the helper

  def call!
    rollback_on_failure {
      payment = Payment.create!(transaction: transaction)
      Success(:refunded, result: { payment: payment })
    }
  end
end

Test plan

  • New test/micro/case/rollback_on_failure_test.rb covers:
    • alias identity (instance_method(:transaction) == instance_method(:rollback_on_failure))
    • explicit with: / class-macro fallback / global-default fallback resolution
    • rollback_on_failure(:activerecord) { ... } back-compat shim flows through the alias
    • rollback_on_failure(:other) { ... } raises the same ArgumentError
    • real-AR commit-on-success / rollback-on-failure semantics
    • attribute-shadowing regression: a Micro::Case with attribute :transaction calling rollback_on_failure { ... } rolls back correctly; the transaction attribute reader is untouched
  • All existing #transaction tests continue to pass unchanged.
  • bundle exec rake test (bare bundle): 795 runs / 0 failures.
  • bundle exec appraisal rails-8-1 rake test (with ActiveRecord): 1054 runs / 0 failures, both with and without ENABLE_TRANSITIONS=true.

Docs

  • CHANGELOG.md — entry under ## [Unreleased] (no version cut in this PR).
  • README.md + README.pt-BR.md — lockstep, alias note + example appended to the inline transaction { ... } section. No TOC or Documentation-table changes.

🤖 Generated with Claude Code

The `#transaction` instance helper is shadowed by the attribute reader
when a use case declares `attribute :transaction` (a domain object that
has nothing to do with the database). `rollback_on_failure` is an
`alias_method` of `#transaction` — same signature, same resolution order,
same back-compat shim — verbose enough to never realistically clash with
a user-defined attribute. The name matches the sibling gem
`solid-process`'s `rollback_on_failure` so the two APIs read alike.

Closes the issue at issue-1-rollback-on-failure.md.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@serradura serradura self-assigned this May 27, 2026
@serradura serradura added this to the 5.x milestone May 27, 2026
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