Search code examples
ruby-on-railsnomethoderrormixpanel

Passing set to method_missing (mixpanel)


I'm currently settings up mixpanel for our service and it's all working great, however I want to pass mixpanel as a method_missing if in development/test environment.

I have this in my application_controller.rb:

before_filter :initialize_mixpanel

def initialize_mixpanel
  if ENV.has_key?('MIXPANEL_PROJECT_TOKEN')
    @tracker = Mixpanel::Tracker.new('MIXPANEL_PROJECT_TOKEN', request.env)
  else
    @tracker = DummyMixpanel.new
  end
end

and this in my mixpanel.rb:

unless ENV.has_key?('MIXPANEL_PROJECT_TOKEN')
  class DummyMixpanel
    def method_missing(m, *args, &block)
      true
    end
  end
end

this works for a normal tracker like @tracker.track(.. but when trying to pass .set method to it, it returns NoMethodError: undefined method `set' for true:TrueClass for example using this code:

@tracker.people.set(current_subscriber.id, {
  '$name'             => current_subscriber.name,
  '$email'            => current_subscriber.email,
  '$hometown'       => current_subscriber.hometown,
  '$birth_year'            => current_subscriber.birth_year,
}, ip=current_subscriber.ip);

How would I best handle this?

All the best,


Solution

  • Try returning self in #method_missing like:

    class DummyMixpanel
      def method_missing(m, *args, &block)
        self
      end
    end
    

    More info: http://devblog.avdi.org/2011/05/30/null-objects-and-falsiness/