Advertisement
Guest User

Untitled

a guest
Jul 4th, 2015
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. # Old code
  2. i = 0
  3. @views.each do |view|
  4. while (view[1].to_s != (@startDate+i).to_s) && (i < 30) do
  5. #if no data for that day put 0 views in
  6. @monthViewArray.push(0)
  7. i+=1
  8. end
  9. #otherwise put in the number of views
  10. @monthViewArray.push(view[0])
  11. i+=1
  12. end
  13.  
  14. # new, even more awesome code
  15. @monthViewArray = (30.days.ago.to_date..Date.today).map{ |date| # go through each of the last 30 days
  16. row = @views.find {|row| row[1].to_s==date.to_s} # find a row with this date (returns nil if there isn't one)
  17. row.nil? ? 0 : row[0] # if you found a row use it, otherwise use 0
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement