Refactor rails codes -
hi i've tried make shorter, codeclimate (code reviewer) says there still duplications , complexities.
so far, have after attempt refactor it.
these api codes app:
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 any workarounds, tips, recommendations on how make shorter , logically not duplicated appreciated.
update:
tried put before_filter no luck. guess there should way limit duplication.
you can re-factor follow,
create module in lib callback_helper.rb
module callbackhelper [:gmail, :googlecalendar, :yammer].each |callback_method| define_method("#{callback_method}") 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 in callback controller
include callbackhelper now controller have access method defined module.
make sure new module auto loaded. hope helps.
Comments
Post a Comment