Class: User

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

Overview

Schema Information

Table name: users

id                     :integer          not null, primary key
email                  :string(255)      default(""), not null
encrypted_password     :string(255)      default(""), not null
reset_password_token   :string(255)
reset_password_sent_at :datetime
remember_created_at    :datetime
          :integer          default(0), not null
     :datetime
        :datetime
     :inet
        :inet
created_at             :datetime
updated_at             :datetime
name                   :string(255)
confirmation_token     :string(255)
confirmed_at           :datetime
confirmation_sent_at   :datetime
unconfirmed_email      :string(255)
role                   :integer

Constant Summary collapse

TEMP_EMAIL_PREFIX =

when the user comes from auth methods without email, we put a temporary email so that it is sent to finish_signup when he enters again.

'[email protected]'
TEMP_EMAIL_REGEX =
/\[email protected]/

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_for_oauth(auth, signed_in_resource = nil) ⇒ Object


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/models/user.rb', line 60

def self.find_for_oauth(auth, signed_in_resource = nil)

  # Get the identity and user if they exist
  identity = Identity.find_for_oauth(auth)

  # If a signed_in_resource is provided it always overrides the existing user
  # to prevent the identity being locked with accidentally created accounts.
  # Note that this may leave zombie accounts (with no associated identity) which
  # can be cleaned up at a later date.
  user = signed_in_resource ? signed_in_resource : identity.user

  # Create the user if needed
  if user.nil?

    # Get the existing user by email if the provider gives us a verified email.
    # If no verified email was provided we assign a temporary email and ask the
    # user to verify it on the next step via UsersController.finish_signup
    email = auth.info.email
    user = User.where(:email => email).first if email

    # Create the user if it's a new registration
    if user.nil?
      user = User.new(
        name: auth.extra.raw_info.name,
        #username: auth.info.nickname || auth.uid,
        email: email ? email : "#{TEMP_EMAIL_PREFIX}-#{auth.uid}-#{auth.provider}.com",
        password: Devise.friendly_token[0,20]
      )
      user.skip_confirmation!
      user.save!
      if auth.provider == "facebook"
        # we should link every friend
        Koala.config.api_version = "v2.2"
        @graph = Koala::Facebook::API.new(auth.credentials.token)
        friends = @graph.get_connections("me", "friends")
        friends.each do |f|
          user_identity = Identity.find_by(provider: "facebook", uid: f["id"])
          if(user_identity.present?)
            user_friend = user_identity.user
            user_friend.follow(user)
            user.follow(user_friend)
          end
        end
      end
    end
  end

  # Associate the identity with the user if needed
  if identity.user != user
    identity.user = user
    identity.save!
  end
  user
end

Instance Method Details

#create_game_shelvesObject


40
41
42
43
44
45
46
# File 'app/models/user.rb', line 40

def create_game_shelves
  GameShelf.create(:title => "Wishlist", :shelf_type => GameShelf::shelf_types[:wishlist], :user_id => id)
  GameShelf.create(:title => "Backlog", :shelf_type => GameShelf::shelf_types[:backlog], :user_id => id)
  GameShelf.create(:title => "Playing", :shelf_type => GameShelf::shelf_types[:playing], :user_id => id)
  GameShelf.create(:title => "Finished", :shelf_type => GameShelf::shelf_types[:finished], :user_id => id)
  GameShelf.create(:title => "Played", :shelf_type => GameShelf::shelf_types[:played], :user_id => id)
end

#email_verified?Boolean

Returns:

  • (Boolean)

119
120
121
# File 'app/models/user.rb', line 119

def email_verified?
  self.email && self.email !~ TEMP_EMAIL_REGEX
end

#game_maker_or_more?Boolean

Returns:

  • (Boolean)

115
116
117
# File 'app/models/user.rb', line 115

def game_maker_or_more?
  self.gm? or self.reviewer? or self.admin?
end

#set_default_roleObject


36
37
38
# File 'app/models/user.rb', line 36

def set_default_role
  self.role ||= :user
end