From majestic-rails
Implements Rails business logic with ActiveInteraction operations and AASM state machines. Covers combined patterns, transactions, and routes to specialized skills.
How this skill is triggered — by the user, by Claude, or both
Slash command
/majestic-rails:business-logic-coderThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
Orchestrator for structured business logic in Rails.
Orchestrator for structured business logic in Rails.
| Need | Skill | Use When |
|---|---|---|
| Typed operations | active-interaction-coder | Creating business operations, refactoring service objects |
| State machines | aasm-coder | Implementing workflows, managing state transitions |
Use for operations - things that happen once:
# Example: One-time operation with typed inputs
outcome = Users::Create.run(email: email, name: name)
Use for state management - things with lifecycle:
# Example: Stateful entity with transitions
order.pay! # pending → paid
order.ship! # paid → shipped
Often used together - interactions trigger state changes:
module Orders
class Process < ActiveInteraction::Base
object :order, class: Order
def execute
order.process! # AASM transition
fulfill_order(order)
order
end
end
end
# Gemfile
gem "active_interaction", "~> 5.3"
gem "aasm", "~> 5.5"
ActiveInteraction:
.run - Returns outcome (check .valid?).run! - Raises on failurecompose - Call nested interactionsAASM:
.event! - Transition or raise.event - Transition or return false.may_event? - Check if transition valid.aasm.events - List available eventsWhen business logic involves multi-step database operations, enforce proper transaction boundaries.
# PROBLEM: Partial failure leaves inconsistent state
def transfer_funds(from, to, amount)
from.update!(balance: from.balance - amount)
to.update!(balance: to.balance + amount) # May fail!
end
# SOLUTION: Transaction wrapper with locking
def transfer_funds(from, to, amount)
ActiveRecord::Base.transaction do
from.lock!
to.lock!
from.update!(balance: from.balance - amount)
to.update!(balance: to.balance + amount)
end
end
# Default: READ COMMITTED (each query sees latest committed data)
# REPEATABLE READ: All reads see same snapshot
ActiveRecord::Base.transaction(isolation: :repeatable_read) do
# Consistent reads for reports
end
# SERIALIZABLE: Full isolation (may cause serialization failures)
ActiveRecord::Base.transaction(isolation: :serializable) do
# Strictest isolation, retry on failure
end
Rules:
# PROBLEM: Deadlock risk (inconsistent lock order)
def swap_owners(item_a, item_b)
transaction do
item_a.lock! # Thread 1 locks A
item_b.lock! # Thread 2 locks B -> deadlock!
end
end
# SOLUTION: Consistent lock order
def swap_owners(item_a, item_b)
items = [item_a, item_b].sort_by(&:id)
transaction do
items.each(&:lock!)
# Safe: always locks lower ID first
end
end
| Anti-Pattern | Problem | Solution |
|---|---|---|
| Long transactions | Lock contention | Split into smaller units |
| API calls inside | Timeout blocks DB | Call outside, then transact |
| User input inside | Indefinite locks | Validate first, transact last |
| Nested transactions | Savepoint confusion | Use requires_new: true explicitly |
# PROBLEM: Inner rollback doesn't work as expected
transaction do
create_order!
transaction do # This is a savepoint, not new transaction
charge_card! # Failure rolls back to savepoint only
end
end
# SOLUTION: Explicit new transaction
transaction do
create_order!
transaction(requires_new: true) do
charge_card! # Failure rolls back only this block
end
end
event-sourcing-coder - Record domain events from state transitionsnpx claudepluginhub majesticlabs-dev/majestic-marketplace --plugin majestic-railsImplements state machines with AASM gem in Rails apps for workflow management. Covers transitions, guards, callbacks, error handling, and RSpec testing.
Finite state machines, event sourcing, CQRS (Command Query Responsibility Segregation), and state modeling.
Provides Rails Active Record patterns for models, associations, queries, validations, callbacks, and scopes. Useful for robust model implementation and optimization.