lib/volt/volt/users.rb in volt-0.9.6 vs lib/volt/volt/users.rb in volt-0.9.7.pre2
- old
+ new
@@ -45,13 +45,25 @@
user_id = user_or_id.is_a?(Volt::Model) ? user_or_id.id : user_or_id
previous_id = Thread.current['with_user_id']
Thread.current['with_user_id'] = user_id
- yield
+ # In the console, you can use without a block
+ if block_given?
+ begin
+ result = yield
+ ensure
+ Thread.current['with_user_id'] = previous_id
+ end
+ else
+ # No block given, only allowed in the console
+ unless Volt.try(:console?)
+ raise "as_user requires a block, except in the console."
+ end
+ end
- Thread.current['with_user_id'] = previous_id
+ result
end
unless RUBY_PLATFORM == 'opal'
# Takes a user and returns a signed string that can be used for the
# user_id cookie to login a user.
@@ -114,10 +126,10 @@
end
def logout
# Notify the backend so we can remove the user_id from the user's channel
UserTasks.logout
-
+
# Remove the cookie so user is no longer logged in
Volt.current_app.cookies.delete(:user_id)
end
# Fetches the user_id+signature from the correct spot depending on client