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

Support performs on private methods #1

Merged
merged 1 commit into from
Jan 12, 2023
Merged
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
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
## [Unreleased]

- Support `performs` on private methods

Method jobs will now call methods with `send`, in case you only want to expose the generated later method to the outside world.

```ruby
class Post < ActiveRecord::Base
performs :something_low_level

private

# We don't want other objects to call this, they should always use the generated later method.
def something_low_level
# …
end
end
```

Here, the generated `Post#something_low_level_later` is public and available but can still call into the immediate version of `something_low_level`.

## [0.1.1] - 2022-09-27

- Fixed: extend ActiveRecord::Base with ActiveJob::Performs as the README says.
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,21 @@ class Post < ActiveRecord::Base
end
```

#### Private methods

`ActiveJob::Performs` also works with private methods in case you only want to expose the generated `_later` method.

```ruby
class Post < ActiveRecord::Base
performs :publish # Generates the public `publish_later` instance method.

# Private implementation, only call `publish_later` please!
private def publish
end
end
```

### Usage with ActiveRecord::AssociatedObject

The [`ActiveRecord::AssociatedObject`](https://github.com/kaspth/active_record-associated_object) gem also implements `GlobalID::Identification`, so you can do this too:
Expand Down
2 changes: 1 addition & 1 deletion lib/active_job/performs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def performs(method = nil, **configs, &block)

job.class_eval <<~RUBY, __FILE__, __LINE__ + 1 unless job.instance_method(:perform).owner == job
def perform(object, *arguments, **options)
object.#{method}(*arguments, **options)
object.send(:#{method}, *arguments, **options)
end
RUBY

Expand Down
8 changes: 8 additions & 0 deletions test/active_job/test_performs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ class ActiveJob::TestPerforms < ActiveSupport::TestCase
assert Post::Publisher.performed
end

test "supports private methods" do
assert_output "private_method\n" do
assert_performed_with job: Post::Publisher::PrivateMethodJob, args: [ @publisher ] do
@publisher.private_method_later
end
end
end

test "wait is forwarded" do
assert_enqueued_with job: Post::Publisher::RetractJob, args: [ @publisher, reason: "Some reason" ], at: 5.minutes.from_now do
@publisher.retract_later reason: "Some reason"
Expand Down
6 changes: 6 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,10 @@ def retract(reason:)
def social_media_boost
puts "social media soooo boosted"
end

performs :private_method

private def private_method
puts __method__
end
end