Skip to content

Instantly share code, notes, and snippets.

@mnishiguchi
Last active August 29, 2015 14:26
Show Gist options
  • Select an option

  • Save mnishiguchi/3aa6fa4a757efa59bef7 to your computer and use it in GitHub Desktop.

Select an option

Save mnishiguchi/3aa6fa4a757efa59bef7 to your computer and use it in GitHub Desktop.
Ruby - 呼び出されているmethod名を検知してrefactoring ref: http://qiita.com/mnishiguchi/items/919a2d7903ac534852b6
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
user = User.from_omniauth(request.env["omniauth.auth"])
if user.persisted?
set_flash_message(:notice, :success, kind: "Facebook") if is_navigational_format?
sign_in_and_redirect user, event: :authentication
else
session["devise.user_attributes"] = user.attributes
redirect_to new_user_registration_url
end
end
def twitter
user = User.from_omniauth(request.env["omniauth.auth"])
if user.persisted?
set_flash_message(:notice, :success, kind: "Twitter") if is_navigational_format?
sign_in_and_redirect user, event: :authentication
else
session["devise.user_attributes"] = user.attributes
redirect_to new_user_registration_url
end
end
end
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def callback_for_all_providers
provider = __callee__.to_s # エイリアスメソッド名を読みこみ、文字列として利用。
user = User.from_omniauth(request.env["omniauth.auth"])
if user.persisted?
sign_in_and_redirect user, event: :authentication
set_flash_message(:notice, :success, kind: provider.capitalize) if is_navigational_format?
else
session["devise.user_attributes"] = user.attributes
redirect_to new_user_registration_url
end
end
alias_method :twitter, :callback_for_all_providers
alias_method :facebook, :callback_for_all_providers
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment