Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed current user list not being updated when user logs in #326

Merged
merged 1 commit into from Apr 10, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions app/models/user.rb
Expand Up @@ -9,6 +9,8 @@ class User < ActiveRecord::Base

has_many :activities
before_save :ensure_gravatar_hash
before_save :ensure_authentication_token
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All these methods come from a gist here:

https://gist.github.com/josevalim/fb706b1e933ef01e4fb6#file-1_unsafe_token_authenticatable-rb

Where it talks about what to do if you still need the authentication_token attribute for a user.


before_create :mark_registration_status_depending_on_app_settings

after_create :ensure_at_least_one_admin
Expand Down Expand Up @@ -41,6 +43,12 @@ def ensure_gravatar_hash
self.gravatar_hash = Digest::MD5.hexdigest self.email
end

def ensure_authentication_token
if self.authentication_token.blank?
self.authentication_token = generate_authentication_token
end
end

# We never want an app without an admin so let's ensure there is at least one user
def ensure_at_least_one_admin
if User.count == 1
Expand Down Expand Up @@ -120,4 +128,12 @@ def check_external_avatar
end
end

private
def generate_authentication_token
loop do
token = Devise.friendly_token
break token unless User.where(authentication_token: token).first
end
end

end
7 changes: 7 additions & 0 deletions db/migrate/20140409193213_fix_authentication_tokens.rb
@@ -0,0 +1,7 @@
class FixAuthenticationTokens < ActiveRecord::Migration
def change
User.all.each do |user|
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made this a migration so that way you'll be forced to "Migrate" your users, even though all it's really doing is going back through and saving the users (which will add an auth token if one is not already present)

user.save!
end
end
end
2 changes: 1 addition & 1 deletion db/schema.rb
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20130623202749) do
ActiveRecord::Schema.define(:version => 20140409193213) do

create_table "activities", :force => true do |t|
t.text "content"
Expand Down