Restforce重构映射形式参数

时间:2015-02-26 18:01:08

标签: ruby-on-rails salesforce

我在rails应用程序中使用Restforce gem将rails应用程序连接到Salesforce。我在Rails中创建了一个表单,用于创建一个扬声器请求表单,该表单是Salesforce中的一个对象。我目前有一个需要重构的代码,因为它太复杂了。关于如何重构此代码的任何建议?

class RequestASpeakerController < ApplicationController
  def index
  end

  def create
    create_request_a_speaker_form_mapping
    redirect_to page_path('thanks')
  end

  private

  def salesforce_connect
    client = Restforce.new :username => ENV['SALESFORCE_USERNAME'],
      :password       => ENV['SALESFORCE_PASSWORD'],
      :client_id      => ENV['SALESFORCE_CLIENT_ID'],
      :client_secret  => ENV['SALESFORCE_SECRET'],
      :host           => ENV['SALESFORCE_HOST']
    client.authenticate!
    client
  end

  def create_request_a_speaker_form_mapping
    salesforce_connect.create(
      'Thrive_Request_Speaker__c',
      First_Name__c: params[:first_name],
      Last_Name__c: params[:last_name],
      Email__c: params[:email],
      Phone__c: params[:phone],
      Event_Type__c: params[:event_type],
      Event_Name__c: params[:event_name],
      Organization_Hosting_the_Event__c: params[:organization_hosting_the_event],
      Event_Street_Address__c: params[:event_street_address],
      Event_City__c: params[:event_city],
      Event_State__c: params[:event_state],
      Event_Zip__c: params[:event_zip_code],
      Event_Date__c: params[:event_date],
      Event_Hour__c: params[:event_hour],
      Event_Minute__c: params[:event_minute],
      Event_AM_or_PM__c: params[:event_am_or_pm],
      Event_Estimated_Attendance__c: params[:event_estimated_attendance],
      Event_Description__c: params[:event_description],
      Event_Audience_Description__c: params[:event_audience_description],
      Cover_Travel_Expenses__c: params[:cover_travel_expenses],
      Cover_Speaker_Honorarium__c: params[:cover_speaker_honorarium],
      Subscribe_To_Thrive_Communication__c: params[:subscribe_to_our_newsletter]
    )
  end
end

1 个答案:

答案 0 :(得分:0)

我认为您的代码简单,清晰且可接受。我不确定你应该重构它但是如果你想要你可以“简化”参数映射使用这样的东西:

params = {first_name: 'Max'}
options = params.except(:action, :controller).map {|(k, v)| ["#{k.to_s.titleize.gsub(/\s/, '_')}__c", v]}.to_hash

salesforce_connect.create('Thrive_Request_Speaker__c', **options)