diff --git a/CHANGELOG.md b/CHANGELOG.md index 950490c..b3786df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/README.md b/README.md index d3632be..5b8ef40 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/lib/active_job/performs.rb b/lib/active_job/performs.rb index 14c85b1..e6bd5e6 100644 --- a/lib/active_job/performs.rb +++ b/lib/active_job/performs.rb @@ -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 diff --git a/test/active_job/test_performs.rb b/test/active_job/test_performs.rb index 2fb66d9..dcbaf17 100644 --- a/test/active_job/test_performs.rb +++ b/test/active_job/test_performs.rb @@ -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" diff --git a/test/test_helper.rb b/test/test_helper.rb index 6bebe0c..08cb8c6 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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