1

こんにちは、私はこれを短くしようとしましたが、CodeClimate (コード レビュー担当者) は、まだいくつかの重複と複雑さがあると常に言っています。

これまでのところ、これはリファクタリングを試みた後のものです。

これらは私のアプリの API コードです:

class CallbackController < ApplicationController
  def gmail
    unless params[:error].present?
      code = current_user.tokens.for(:gmail).create(:hash_key => params[:code], :hash_type => "code")

      response = API::Gmail.new(gmail_callback_index_url).generate_tokens(params[:code])

      if response['error'].present?
        current_user.tokens.for(:gmail).using(:code).destroy_all
        redirect_to(network_path(current_user.network), alert: "Authentication failed. Invalid request.")
      else
        access_token = current_user.tokens.for(:gmail).create(:hash_key => response['access_token'], :hash_type => "access_token", :primary => true)
        id_token = current_user.tokens.for(:gmail).create(:hash_key => response['id_token'], :hash_type => "id_token")
        refresh_token = current_user.tokens.for(:gmail).create(:hash_key => response['refresh_token'], :hash_type => "refresh_token")
        Resque.enqueue(Jobs::Gmail::Today, current_user.id)
        redirect_to network_path(current_user.network), notice: "GMail Access granted."
      end
    else
      redirect_to network_path(current_user.network), alert: "GMail Access denied."
    end
  end

  def googlecalendar
    unless params[:error].present?
      code = current_user.tokens.for(:googlecalendar).create(:hash_key => params[:code], :hash_type => "code")

      response = API::Googlecalendar.new(googlecalendar_callback_index_url).generate_tokens(params[:code])

      if response['error'].present?
        current_user.tokens.for(:googlecalendar).using(:code).destroy_all
        redirect_to(network_path(current_user.network), alert: "Authentication failed. Invalid request.")
      else
        access_token = current_user.tokens.for(:googlecalendar).create(:hash_key => response['access_token'], :hash_type => "access_token", :primary => true)
        id_token = current_user.tokens.for(:googlecalendar).create(:hash_key => response['id_token'], :hash_type => "id_token")
        refresh_token = current_user.tokens.for(:googlecalendar).create(:hash_key => response['refresh_token'], :hash_type => "refresh_token")
        #Resque.enqueue(Jobs::Googlecalendar::Today, current_user.id)
        redirect_to network_path(current_user.network), notice: "Google Calendar Access granted."
      end
    else
      redirect_to network_path(current_user.network), alert: "Google Calendar Access denied."
    end
  end

  def yammer
    unless params[:error].present?
      code = current_user.tokens.for(:yammer).create(:hash_key => params[:code], :hash_type => "code")

      response =  API::Yammer.new.generate_tokens(params[:code])

      if response['error'].present?
        current_user.tokens.for(:yammer).using(:code).destroy_all
        redirect_to network_path(current_user.network), alert: "Authentication failed. Invalid request."
      else
        access_token = current_user.tokens.for(:yammer).create(:hash_key => response['access_token']['token'], :hash_type => "access_token", :primary => true)
        Resque.enqueue(Jobs::Yammer::Latest, current_user.id)
        redirect_to network_path(current_user.network), notice: "Yammer Access granted."  
      end      
    else
      redirect_to network_path(current_user.network), alert: "Yammer Access denied."
    end
  end
end

それを短くし、論理的に重複しないようにする方法に関する回避策、ヒント、推奨事項は高く評価されます。

アップデート:

before_filter を入れようとしましたが、うまくいきませんでした。重複を制限する方法が必要だと思います。

4

1 に答える 1

1

これを次のようにリファクタリングできます。

lib に callback_helper.rb として別のモジュールを作成します。

module CallbackHelper

    [:gmail, :googlecalendar, :yammer].each do |callback_method|
        define_method("#{callback_method}") do 
            unless params[:error].present?
              code = current_user.tokens.for(callback_method).create(:hash_key => params[:code], :hash_type => "code")

              response =  "API::#{callback_method.to_s.capitalize}".constantize.new.generate_tokens(params[:code])

              if response['error'].present?
                current_user.tokens.for(callback_method).using(:code).destroy_all
                redirect_to network_path(current_user.network), alert: "Authentication failed. Invalid request."
              else
                access_token = current_user.tokens.for(callback_method).create(:hash_key => response['access_token']['token'], :hash_type => "access_token", :primary => true)
                Resque.enqueue("Jobs::#{callback_method.to_s.capitalize}::Today".constantize, current_user.id)
                redirect_to network_path(current_user.network), notice: "#{callback_method} Access granted."  
              end      
            else
              redirect_to network_path(current_user.network), alert: "#{callback_method} Access denied."
            end
        end
    end
end

これをコールバックコントローラーに含めます

  include CallbackHelper

これで、コントローラーはモジュールで定義されたすべてのメソッドにアクセスできるようになりました。

新しいモジュールが自動ロードされていることを確認してください。お役に立てれば。

于 2013-08-08T09:33:09.123 に答える