Skip to content

Commit

Permalink
Cleaned up helpers a bit.
Browse files Browse the repository at this point in the history
  • Loading branch information
grimen committed Jan 7, 2010
1 parent 446d916 commit ad7612a
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions lib/devise_facebook_connectable/view_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,28 @@ module Helpers

# Convenient sign in/out (connect) method. See below.
#
def facebook_link(options = {})
scope = auto_detect_scope(options.slice(:scope, :for))
options.except!(:scope, :for)

def facebook_link(*args)
scope = auto_detect_scope(*args)
unless signed_in?(scope)
facebook_sign_in_link(options.merge(:scope => scope))
facebook_sign_in_link(*args)
else
facebook_sign_out_link(options.merge(:scope => scope))
facebook_sign_out_link(*args)
end
end

# Deprecated in favor for +facebook_sign_in_link+.
#
def facebook_login_link(options = {})
::ActiveSupport::Deprecation.warn("facebook_login_link is deprecated. Use: facebook_sign_in_link")
::ActiveSupport::Deprecation.warn("DEPRECATION:" <<
" facebook_login_link is deprecated. Use: facebook_sign_in_link.")
facebook_sign_in_link(options)
end

# Deprecated in favor for +facebook_sign_in_link+.
#
def facebook_logout_link(options = {})
::ActiveSupport::Deprecation.warn("facebook_logout_link is deprecated. Use: facebook_sign_out_link")
::ActiveSupport::Deprecation.warn("DEPRECATION:" <<
" facebook_logout_link is deprecated. Use: facebook_sign_out_link.")
facebook_sign_out_link(options)
end

Expand All @@ -67,7 +67,8 @@ def facebook_logout_link(options = {})
# then this is the same as a traditional "create account".
#
def facebook_sign_in_link(options = {})
scope = auto_detect_scope(options.slice(:scope, :for))
scope = auto_detect_scope(*args)
options = args.extract_options!
options.except!(:scope, :for)
options.reverse_merge!(
:label => ::I18n.t(:sign_in, :scope => [:devise, :sessions, :facebook_actions]),
Expand All @@ -93,8 +94,9 @@ def facebook_sign_in_link(options = {})
# Agnostic Facebook Connect sign_out button/link. Logs out the current
# user from both the app/site and Facebook main site (for security reasons).
#
def facebook_sign_out_link(options = {})
scope = auto_detect_scope(options.slice(:scope, :for))
def facebook_sign_out_link(*args)
scope = auto_detect_scope(*args)
options = args.extract_options!
options.except!(:scope, :for)
options.reverse_merge!(
:label => ::I18n.t(:sign_out, :scope => [:devise, :sessions, :facebook_actions]),
Expand All @@ -114,7 +116,7 @@ def facebook_sign_out_link(options = {})
end
end

# Agnostic Facebook Connect disconnect button/link.
# TODO: Agnostic Facebook Connect disconnect button/link.
# Disconnects, i.e. deletes, user account. Identical as "Delete my account",
# but for Facebook Connect (which "un-installs" the app/site for the current user).
#
Expand All @@ -139,16 +141,25 @@ def facebook_disconnect_link(options = {})
# Used to make the link-helpers smart if - like in most cases -
# only one devise scope will be used, e.g. "user" or "account".
#
def auto_detect_scope(options = {})
scope = options[:scope] || options[:for]
def auto_detect_scope(*args)
options = args.extract_options!

if options.key?(:for)
options[:scope] = options[:for]
::ActiveSupport::Deprecation.warn("DEPRECATION: " <<
"Devise scope :for option is deprecated. " <<
"Use: facebook_*_link(:some_scope), or facebook_*_link(:scope => :some_scope)")
end

scope = args.detect { |arg| arg.is_a?(Symbol) } || options[:scope]
scope ||= ::Warden::Manager.default_scope
mapping = ::Devise.mappings[scope]

if mapping.for.include?(:facebook_connectable)
scope
else
error_message =
"devise/facebook_connectable: %s" <<
"%s" <<
" Did you forget to devise facebook_connect in your model? Like this: devise :facebook_connectable." <<
" You can also specify scope explicitly, e.g.: facebook_*link :for => :customer."
error_message %=
Expand Down

0 comments on commit ad7612a

Please sign in to comment.