Class: WeixinPam::UserAccount

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/weixin_pam/user_account.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_omniauth(public_account, auth, auto_create = true) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'app/models/weixin_pam/user_account.rb', line 9

def self.from_omniauth(, auth, auto_create = true)
  .user_accounts.where(uid: auth.uid).send(auto_create ? "first_or_create" : "first_or_initialize") do |u|
    u.nickname = auth.info.nickname
    u.sex = auth.info.sex
    u.province = auth.info.province
    u.city = auth.info.city
    u.country = auth.info.country
    u.headimgurl = auth.info.headimgurl
  end
end

.sync_from_server(public_account) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/models/weixin_pam/user_account.rb', line 20

def self.sync_from_server()
  transaction do
    update_all subscribed: false
    .client.followers.result['data']['openid'].each do |openid|
      u = find_or_initialize_by uid: openid
      u. = .id
      u.populate_with_api_info
      yield u if block_given?
      u.save if u.changed?
    end
  end
end

Instance Method Details

#api_infoObject



33
34
35
# File 'app/models/weixin_pam/user_account.rb', line 33

def api_info
  .client.user(uid)
end

#populate_with_api_infoObject



45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/weixin_pam/user_account.rb', line 45

def populate_with_api_info
  weixin_api_info = api_info
  if weixin_api_info.result['subscribe'] != 1
    self.subscribed = false
    return
  end
  self.subscribed = true
  [:nickname, :sex, :city, :province, :country, :headimgurl].each do |attr|
    send("#{attr}=", weixin_api_info.result[attr])
  end
end

#same_with_headimgObject



37
38
39
40
41
42
43
# File 'app/models/weixin_pam/user_account.rb', line 37

def same_with_headimg
  set_headimg_fingerprint
  return [] if headimg_fingerprint.blank?
  records = UserAccount.where(headimg_fingerprint: headimg_fingerprint).all.to_a
  records.delete_if { |r| r.id == id } if id
  records
end