/app/cells/social_button.rb

https://github.com/bbruby1/arvut_system · Ruby · 110 lines · 90 code · 18 blank · 2 comment · 10 complexity · 35185f183c32e1170ff287ef272866b3 MD5 · raw file

  1. require 'net/http'
  2. require 'uri'
  3. require 'AESCrypt.rb'
  4. class SocialButton < Apotomo::Widget
  5. respond_to_event :button_press, :with => :button_press
  6. respond_to_event :button_clicks_edit, :with => :button_clicks_edit
  7. helper_method :current_user
  8. helper_method :get_button_click_set
  9. def get_button_click_set
  10. user = param :user
  11. (user.button_click_set.nil? || user.button_click_set == 0) ? 1 : user.button_click_set
  12. end
  13. def display
  14. user = param :user
  15. @status = ButtonClick.status(user.id)
  16. @button_class = @status ? 'we_button' : 'me_button'
  17. @timeout = ButtonClick.time_left(user.id)
  18. @button_click_set = get_button_click_set()
  19. calc_today_clicks(user.id, user.email, @button_click_set)
  20. set_push_authentication(user.email)
  21. render
  22. end
  23. def button_press
  24. user = param :user
  25. @status = ButtonClick.status(user.id)
  26. ButtonClick.create(:user_id => user.id) unless @status
  27. calc_today_clicks(user.id, user.email)
  28. set_push_authentication(user.email)
  29. determine_button_content
  30. render
  31. end
  32. def button_clicks_edit
  33. user = param :user
  34. # if params[:user]
  35. user.update_attributes(params[:user])
  36. @button_click_set = get_button_click_set()
  37. calc_today_clicks(user.id, user.email, @button_click_set)
  38. set_push_authentication(user.email)
  39. render
  40. end
  41. def current_user
  42. param :user
  43. end
  44. private
  45. def set_push_authentication(username)
  46. @push_username = AESCrypt.encrypt(username,
  47. ::Rails.configuration.comet_auth_key,
  48. ::Rails.configuration.comet_auth_iv,"AES-128-CBC")
  49. @push_authentication = AESCrypt.encrypt(@push_username,
  50. ::Rails.configuration.comet_auth_key,
  51. ::Rails.configuration.comet_auth_iv,"AES-128-CBC")
  52. end
  53. def calc_today_clicks(id, email, total = nil)
  54. @today_clicks = ButtonClick.today_clicks(id).count
  55. @today_total = total.blank? ? current_user.button_click_set : total
  56. if @today_total.blank? || @today_total < 1
  57. @today_total = 1
  58. end
  59. @today_all_clicks = ButtonClick.today_total_clicks.count
  60. @today_all_total = User.users_recent_button_click_set[0].total.to_i
  61. if @today_all_total < 1 || @today_all_total < @today_all_clicks
  62. @today_all_total = [1, @today_all_total.to_i].max
  63. end
  64. @users_group_id = UserList.group_id_by_email(email);
  65. if (@users_group_id.count < 1 or @users_group_id[0].users_group_id.blank?)
  66. @users_group_id = "null"
  67. else
  68. @users_group_id = @users_group_id[0].users_group_id
  69. end
  70. @today_group_clicks = ButtonClick.today_total_clics_by_gourp(email).count
  71. @today_group_total = User.users_recent_button_click_set_for_group(email)[0].total.to_i
  72. @today_group_clicks = @today_group_clicks.blank? ? 0 : @today_group_clicks
  73. @today_group_total = @today_group_total.blank? ? 0 : @today_group_total
  74. if @today_group_total < 1 || @today_group_total < @today_group_clicks
  75. @today_group_total = [1, @today_group_total.to_i].max
  76. end
  77. end
  78. # We use float constant in order to optimize calculations
  79. UPPER_LIMIT_TO_SHOW_BUTTON_CONTENT = 4.0
  80. def determine_button_content
  81. if Page.get_button_content_count > 0
  82. loc_today_clicks = @today_clicks <= 0 ? 1 : @today_clicks
  83. loc_today_total = @today_total <= 0 ? 1 : @today_total
  84. gen_step = loc_today_total / UPPER_LIMIT_TO_SHOW_BUTTON_CONTENT
  85. prev_click_range_num = ((loc_today_clicks - 1) / gen_step).to_i
  86. cur_click_range_num = (loc_today_clicks / gen_step).to_i
  87. @is_show_button_content = cur_click_range_num > prev_click_range_num
  88. else
  89. @is_show_button_content = false
  90. end
  91. end
  92. end