scale_user.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # encoding:utf-8
  2. class ScaleUser < ActiveRecord::Base
  3. has_paper_trail
  4. self.table_name = "scale_users"
  5. belongs_to :wx_user
  6. has_many :scale_results, :foreign_key => :user_id, :dependent => :destroy
  7. attr_accessor :v_avatar
  8. #after_create :update_paied_time
  9. SEX_ENUM = [["男",0],["女",1]]
  10. def wx_user_contact
  11. return "-" if wx_user.blank?
  12. user = wx_user.user
  13. return "-" if user.blank?
  14. contact = user.tel
  15. contact = user.email if contact.blank?
  16. contact.blank? ? "-" : contact
  17. end
  18. def get_avatar_img
  19. return "https://x-encrpt-bucket.s3.ap-southeast-2.amazonaws.com/encript/1766455493300.jpeg" if self.avatar.blank?
  20. url = "http://#{Ali::Oss::CDN_URL_FOR_HOST}/#{self.avatar}"
  21. return url
  22. end
  23. rails_admin do
  24. navigation_label '体脂秤用户'
  25. parent ScaleDevice
  26. weight 1
  27. list do
  28. filters [:wx_user,:created_at]
  29. field :id
  30. field :wx_user_id
  31. field :wx_user do
  32. formatted_value do
  33. bindings[:object].wx_user_contact
  34. end
  35. end
  36. field :nick_name
  37. field :avatar do
  38. formatted_value do
  39. bindings[:view].tag(:img,{:src => bindings[:object].get_avatar_img,
  40. :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px',
  41. :onClick => "javascript:window.open('#{bindings[:object].get_avatar_img}')"})
  42. end
  43. end
  44. field :sex, :enum do
  45. filterable true
  46. enum do
  47. SEX_ENUM
  48. end
  49. end
  50. field :height
  51. field :age
  52. field :target_weight
  53. field :birthday
  54. field :created_at
  55. field :updated_at
  56. end
  57. show do
  58. field :wx_user_id
  59. field :wx_user do
  60. formatted_value do
  61. bindings[:object].wx_user_contact
  62. end
  63. end
  64. field :nick_name
  65. field :avatar do
  66. formatted_value do
  67. bindings[:view].tag(:img,{:src => bindings[:object].get_avatar_img,
  68. :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px',
  69. :onClick => "javascript:window.open('#{bindings[:object].get_avatar_img}')"})
  70. end
  71. end
  72. field :sex, :enum do
  73. filterable true
  74. enum do
  75. SEX_ENUM
  76. end
  77. end
  78. field :height
  79. field :age
  80. field :target_weight
  81. field :birthday
  82. field :created_at
  83. field :updated_at
  84. end
  85. edit do
  86. # field :wx_user_id
  87. field :wx_user
  88. field :nick_name
  89. field :v_avatar, :file_upload do
  90. pretty_value do
  91. bindings[:view].tag(:img, {:src => bindings[:object].get_avatar_img, :class => 'preview'})
  92. end
  93. end
  94. field :sex, :enum do
  95. filterable true
  96. enum do
  97. SEX_ENUM
  98. end
  99. end
  100. field :height
  101. field :age
  102. field :target_weight
  103. field :birthday
  104. field :created_at
  105. field :updated_at
  106. end
  107. end
  108. def v_avatar=file
  109. unless file.blank?
  110. clear_avatar_img
  111. file_name = "#{UUID.new.generate[0...8].downcase}.jpg"
  112. file_path = "#{IMG_STORE_PATH}/#{file_name}"
  113. Ali::Oss.store(Ali::Oss::BUCKET_NAME_PUBLIC_READ, file_path, file.read)
  114. self.head = file_path
  115. self.save
  116. end
  117. end
  118. def clear_avatar_img
  119. file_path = "#{self.head}"
  120. Ali::Oss.delete_object(Ali::Oss::BUCKET_NAME_PUBLIC_READ, file_path)
  121. end
  122. end