Guest User

Untitled

a guest
Jan 17th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. class UsersActivityTable
  2.  
  3. def initialize(user_ids, content_ids, start_date, end_date)
  4. @user_ids, @content_ids, @start_date, @end_date = user_ids, content_ids, start_date, end_date
  5. end
  6.  
  7. def rows
  8. @rows ||= @user_ids.map do |user_id|
  9. OpenStruct.new( :full_name => sum_users(user_id),
  10. :contributed => sum_contributed(user_id),
  11. :interacted => sum_interacted(user_id)
  12. )
  13. end
  14. end
  15.  
  16. private
  17.  
  18. def sum_users(user_id)
  19. @sum_users ||= Hash[ User.value_of(:id, :first_name, :last_name).map{|usr| [usr[0], "#{usr[1]} #{usr[2]}"] } ]
  20. @sum_users[user_id]
  21. end
  22.  
  23. def sum_contributed(user_id)
  24. @sum_contributed ||= Content.where(:user_id => @user_ids, :id => @content_ids)
  25. .where('date(created_at) between ? and ? ', @start_date, @end_date)
  26. .group(:user_id)
  27. .count
  28. @sum_contributed[user_id].to_i
  29. end
  30.  
  31. def sum_interacted(user_id)
  32. sum_comments(user_id).to_i + sum_views(user_id).to_i + sum_likes(user_id).to_i
  33. end
  34.  
  35. def sum_comments(user_id)
  36. @sum_comments ||= Comment.where(:user_id => @user_ids, :content_id => @content_ids)
  37. .where('date(created_at) between ? and ? ', @start_date, @end_date)
  38. .group(:user_id)
  39. .count
  40. @sum_comments[user_id]
  41. end
  42.  
  43. def sum_likes(user_id)
  44. @sum_likes ||= Like.where(:user_id => @user_ids, :content_id => @content_ids)
  45. .where('date(created_at) between ? and ? ', @start_date, @end_date)
  46. .group(:user_id)
  47. .count
  48. @sum_likes[user_id]
  49. end
  50.  
  51. def sum_views(user_id)
  52. @sum_views ||= View.where(:viewable_type => 'Content', :user_id => @user_ids, :viewable_id => @content_ids)
  53. .where('date(created_at) between ? and ? ', @start_date, @end_date)
  54. .group(:user_id)
  55. .count
  56. @sum_views[user_id]
  57. end
  58. end
  59.  
  60. .where('date(created_at) between ? and ? ', @start_date, @end_date)
  61. .group(:user_id)
  62. .count
  63.  
  64. # pseudo code
  65. group_results = lambda{|el| el.where('date(created_at) between ? and ? ', @start_date, @end_date)
  66. .group(:user_id)
  67. .count }
  68. Content.where(...).tap{|collection| collection.defune_method .... }.group_results
Add Comment
Please, Sign In to add comment