Skip to content

Commit 2adf21e

Browse files
committed
Command handler can use caller private methods
There are a number of private methods that it would be useful to access from within a command handler block when used from inside a Rails controller. One reported case is #20 where the reporter would like to access the cookies collection. This is a private Rails controller method that is normally accessible within an action. The handler blocks delegate calls to the caller using method missing. In that method with were checking first to confirm if the caller could respond to the message but only included public methods. We now also check private methods and if supported by the caller we use `#send` to forward the message onto the caller (public or private methods). This should fix #20 and also fix #15.
1 parent 9529c9d commit 2adf21e

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

Diff for: lib/rectify/command.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def transaction(&block)
1818
end
1919

2020
def method_missing(method_name, *args, &block)
21-
if @caller.respond_to?(method_name)
21+
if @caller.respond_to?(method_name, true)
2222
@caller.send(method_name, *args, &block)
2323
else
2424
super

Diff for: spec/lib/rectify/command_spec.rb

+17-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ def failure
3030
@failure = true
3131
end
3232

33-
it "calls methods on the caller" do
33+
def something_private
34+
@private = true
35+
end
36+
37+
private :something_private
38+
39+
it "calls public methods on the caller" do
3440
@success = false
3541
@failure = false
3642

@@ -42,5 +48,15 @@ def failure
4248
expect(@success).to be(true)
4349
expect(@failure).to be(false)
4450
end
51+
52+
it "calls private methods on the caller" do
53+
@private = false
54+
55+
SuccessCommand.call do
56+
on(:success) { something_private }
57+
end
58+
59+
expect(@private).to be(true)
60+
end
4561
end
4662
end

0 commit comments

Comments
 (0)