Action cable def with params
Closed this issue · 6 comments
Hi,
def test doesn't work but def test1 works
When I have a def with params like
TestChannel < ApplicationCable::Channel
def test(data)
@test
end
def test1
@test
end
before :test do
@test = Test.new
end
end
[ArgumentError - wrong number of arguments (given 0, expected 1)]
It works when I remove:
before :test do
@test = Test.new
end
Hey there,
test
takes argeuments (data) so the block expects to be given arguments, if you don't care about the arguments just do:
before :test do |_|
@test = Test.new
end
that should work
Thanks for your fast answer, but it doesn't work for me.
I use Rails and ActionCable, outside them I have tested it and it works good, in my case I need inside the ActionCableChannel.
Outside Channel:
class Class1
extends AfterDo
def hello(attr)
end
before :hello do |_|
puts _
end
end
Class1.new.hello('1234')
=> 1234
Inside Channel:
class ClassChannel < ApplicationCable::Channel
extends AfterDo
def hello(attr)
end
before :hello do |_|
puts _
end
end
When I call this from js the output is:
=> :hello
argh... I have a bad feeling that maybe ActionCable or something there already defines a before
method which would be a very unfortunate name clash. I'll have a look when I got time, please feel free to ping me if I don't come back to you within 4 days
So yeah it is as I suspected, in rails their own :before/:after methods are defined by some home grown callback functionality.
Proposed solution from my side would be an alternative interface into AfterDo where methods are named differently... for lack of a better naming scheme I'll probably name space the methods like ad_before
and ad_after
but as it should be easy I'll provide a small example how to make your interface/own method names in case of name clashes.
Sounds good?
Yeah! thats sounds really good.
Thank you for your support and your fast answer.
Hey!
I'll work on this and hopefully get it out soon, a quick look at the code also reminded me that you can totally already do this, e.g. here is the definition of after
and before
:
def after(*methods, &block)
_after_do_define_callback(:after, methods, block)
end
# This method works much like .after - except blocks are executed
# before the method is called.
def before(*methods, &block)
_after_do_define_callback(:before, methods, block)
end
You could just call these method (probably via :send
) yourself :)