Skip to content
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

Add <kind>_<action> method to handle callback #566

Open
wants to merge 3 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1053,25 +1053,35 @@ interaction's lifecycle.

``` rb
class Increment < ActiveInteraction::Base
set_callback :filter, :before, -> { puts 'before filter' }
before_validate do
puts 'before validate'
end
# or you can use the `set_callback` method
# set_callback :validate, :before, -> { puts 'before validate' }

integer :x

set_callback :validate, :after, -> { puts 'after validate' }
after_validate :echo_after_validate

validates :x,
numericality: { greater_than_or_equal_to: 0 }

set_callback :execute, :around, lambda { |_interaction, block|
around_execute do |_interaction, block|
puts '>>>'
block.call
puts '<<<'
}
end

def execute
puts 'executing'
x + 1
end

private

def echo_after_validate
puts 'after validate'
end
end

Increment.run!(x: 1)
Expand All @@ -1083,6 +1093,8 @@ Increment.run!(x: 1)
# => 2
```

Of course, you can set callback using `set_callback` manually.

In order, the available callbacks are `filter`, `validate`, and `execute`.
You can set `before`, `after`, or `around` on any of them.

Expand Down
30 changes: 30 additions & 0 deletions lib/active_interaction/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,36 @@ def filters
# rubocop:enable Naming/MemoizedInstanceVariableName
end

# Set a callback that will be run before {#filter}.
def before_filter(*args, &block)
set_callback(:filter, :before, *args, &block)
end

# Set a callback that will be run after {#filter}.
def after_filter(*args, &block)
set_callback(:filter, :after, *args, &block)
end

# Set a callback that will be run around {#filter}.
def around_filter(*args, &block)
set_callback(:filter, :around, *args, &block)
end

# Set a callback that will be run before {#validate}.
def before_validate(*args, &block)
set_callback(:validate, :before, *args, &block)
end

# Set a callback that will be run after {#validate}.
def after_validate(*args, &block)
set_callback(:validate, :after, *args, &block)
end

# Set a callback that will be run around {#validate}.
def around_validate(*args, &block)
set_callback(:validate, :around, *args, &block)
end

private

# rubocop:disable Style/MissingRespondToMissing
Expand Down
15 changes: 15 additions & 0 deletions lib/active_interaction/concerns/runnable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,21 @@ def run(*args)
def run!(*args)
new(*args).send(:run!)
end

# Set a callback that will be run before {#execute}.
def before_execute(*args, &block)
set_callback(:execute, :before, *args, &block)
end

# Set a callback that will be run after {#execute}.
def after_execute(*args, &block)
set_callback(:execute, :after, *args, &block)
end

# Set a callback that will be run around {#execute}.
def around_execute(*args, &block)
set_callback(:execute, :around, *args, &block)
end
end
end
end