2013-09-25 16:01:54 +00:00
|
|
|
class UserTokenCreator
|
|
|
|
|
2013-09-25 16:22:44 +00:00
|
|
|
def initialize(clock = DateTime)
|
|
|
|
@clock = clock
|
|
|
|
end
|
|
|
|
|
2013-09-25 16:01:54 +00:00
|
|
|
def create(user, token)
|
|
|
|
user_token = user.add_user_token(token)
|
|
|
|
|
|
|
|
if user_token.persisted?
|
2013-09-25 16:22:44 +00:00
|
|
|
update_asciicasts(user_token.token, user)
|
2013-09-25 16:01:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2013-09-25 16:22:44 +00:00
|
|
|
attr_reader :clock
|
|
|
|
|
|
|
|
def update_asciicasts(token, user)
|
2013-09-25 16:01:54 +00:00
|
|
|
Asciicast.where(:user_id => nil, :user_token => token).
|
2013-09-25 16:22:44 +00:00
|
|
|
update_all(:user_id => user.id, :user_token => nil,
|
|
|
|
:updated_at => clock.now)
|
2013-09-25 16:01:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|