3

I was recommend in an earlier question to use a gem called Wisper. I am very happy to learn about it, as it is exactly the solution I'm looking for. What I can't understand from the documentation on Wisper is how listeners register themselves.

Here is my code:

app/models/subscription.rb

class Subscription < ActiveRecord::Base
include Wisper::Publisher

  def some_method
    # some code here
    broadcast(:subscription_paused)
  end
end

app/models/offer.rb

class Offer < ActiveRecord::Base
  def subscription_paused
    binding.pry # or whatever
  end
end

So I'm not entirely sure about this part here. I've tried a variety of subscribing techniques, but I think it just comes down to me not really understanding this aspect of it:

config/initializers/wisper.rb

Wisper.subscribe(Offer.new)

I also tried, similar to the example in the Wiki:

subscription = Subscription.new
subscription.subscribe(Offer.new)

What am I missing here? (I'm not really sure if the above code should even go in an initializer.)

user3281384
  • 511
  • 1
  • 7
  • 17
  • The code you have is correct, can you confirm that `Offer.new.subscription_paused` will start pry. May I also suggest you raise an exception instead of `binding.pry` just in case. – Kris Feb 03 '15 at 09:21
  • You can also get a list of all globally subscribed listeners with `Wisper::GlobalListeners.listeners`, is an `Offer` object in that list? – Kris Feb 03 '15 at 09:30

1 Answers1

4

If the tables exists for Offer and Subscription model then the code should work.

Try this in the rails console:

# class Subscription < ActiveRecord::Base
class Subscription
  include Wisper::Publisher

  def some_method
    # some code here
    broadcast(:subscription_paused)
  end
end

# class Offer < ActiveRecord::Base
class Offer
  def subscription_paused
    puts "jeijjj"
  end
end


Wisper.subscribe(Offer.new)

Subscription.new.some_method

It should generate an output:

"jeijjj"
Boti
  • 3,275
  • 1
  • 29
  • 54
  • 1
    Thanks Boti. My problem was a stupid one. I was accidentally defining my listener methods in the protected section of Offer. Everything works perfectly now! :) – user3281384 Feb 08 '15 at 09:50