2016-05-16 10 views
0

をクリーンアップrubocopだからここに神々がいる。私は警官に合格し、学ぶための勧告を探しています。ありがとうOAuthのは、私はさわやかなOAuthのトーク​​ンを支援するために、次の方法を設定している支援

+0

あなたは、モデル全体を投稿してくださいもらえますか?また、あなたのコードが正しいと確信していますか?すべてのブランチをテストするのにTDDを使用していますか?たとえば、ifブランチのコードが 'if access_token'のコードが正しくないとは思わない。 –

+0

@EmilianoDellaCasaこれは実際には懸念事項の一部であり、私はこのモジュールの完全なコードを投稿したばかりです。 –

答えて

1

refresh_token!の実装では、あまりにも多くのことが間違いありません。あなたは常に一つのことと一つのことをするためのメソッドを保持したい。これは、テスト(例:特定のメソッドをスタブする)、デバッグおよび可読性を簡単にします。

次の場合に役立ちます参照してください:

module WhiplashOmniAuthentication 
    extend ActiveSupport::Concern 

    module ClassMethods 
    def from_omniauth(auth) 
     Rails.logger.debug auth.inspect 
     where(provider: auth.provider, uid: auth.uid).first_or_create do |user| 
     user.provider = auth.provider 
     user.uid = auth.uid 
     user.email = auth.info.email 
     user.store_token(auth.credentials) 
     end 
    end 
    end 

    def refresh_token! 
    access_token ? refresh_access_token! : false 
    end 

    def refresh_access_token! 
    result = access_token.refresh! 
    store_token(result) 
    save 
    rescue OAuth2::Error 
    false 
    end 

    def settings 
    @settings ||= Devise.omniauth_configs[:whiplash].strategy 
    end 

    def strategy 
    @strategy ||= OmniAuth::Strategies::Whiplash.new(nil, settings.client_id, settings.client_secret, client_options: settings.client_options) 
    end 

    def client 
    @client ||= strategy.client 
    end 

    def access_token 
    OAuth2::AccessToken.new(client, token, refresh_token: refresh_token) 
    end 

    def store_token(auth_token) 
    self.token = auth_token.token 
    self.refresh_token = auth_token.refresh_token 
    self.token_expires_at = Time.at(auth_token.expires_at).to_datetime 
    end 

    def token_expired? 
    Time.now > token_expires_at 
    end 
end 
+0

働いてくれてありがとう@dharam! –

+0

このフォローアップに関する考えhttp://stackoverflow.com/questions/37847397/rails-4-oauth-model-concern-rspec-stubs-and-mocks –