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

Action cable def with params #22

Closed
MTEKode opened this issue Jan 5, 2017 · 6 comments
Closed

Action cable def with params #22

MTEKode opened this issue Jan 5, 2017 · 6 comments

Comments

@MTEKode
Copy link

MTEKode commented Jan 5, 2017

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
@PragTob
Copy link
Owner

PragTob commented Jan 5, 2017

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

@MTEKode
Copy link
Author

MTEKode commented Jan 5, 2017

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

@PragTob
Copy link
Owner

PragTob commented Jan 5, 2017

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

@PragTob
Copy link
Owner

PragTob commented Jan 7, 2017

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?

@MTEKode
Copy link
Author

MTEKode commented Jan 9, 2017

Yeah! thats sounds really good.
Thank you for your support and your fast answer.

@PragTob
Copy link
Owner

PragTob commented Jan 14, 2017

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants