assaf / vanity

Experiment Driven Development for Ruby
http://vanity.labnotes.org
MIT License
1.55k stars 269 forks source link

[Metrics] track! method doesn't work in metric definition #320

Closed Looooong closed 7 years ago

Looooong commented 7 years ago

I tried a modified piece of code from the example

metric '[Milestone] Level 5' do
  description 'Measure how many people reached level 5.'

  User.after_save do |user|
    track! if user.level_changed? && user.level == 5
  end
end

When user reaches level 5, exception is thrown: undefined method 'track!' for #<User:0x007f201750b480>

phillbaker commented 7 years ago

@Looooong the track! method isn't defined globally, it's only available on the Vanity object, does Vanity.track! work there?

Looooong commented 7 years ago

@phillbaker Yes, it does work. I have to rewrite the code to Vanity.track!(:milestone_level_5).

It doesn't seem to be slick to me, however.

You should either update the document, or update the code to work as expected in the document.

phillbaker commented 7 years ago

Ah, thanks @Looooong, I fixed most of the doc references, but looks like I missed that one. Can you take a look at this commit to make sure I fixed the right spot: https://github.com/assaf/vanity/commit/831c9859ada1a7320c5ee3518efeffb69d2c71ce?

Looooong commented 7 years ago

I have just tested the code.

metric '[Milestone] Level 5' do
  description 'Measure how many people reached level 5.'

  User.after_save do |user|
    Vanity.track! if user.level_changed? && user.level == 5
  end
end

And it throws ArgumentError: wrong number if arguments (given 0, expected 1..2)

And worse, with this pattern, Rails doesn't show the exact line where this exception is thrown.

So, I will stick to Vanity.track!(:metric)