require_dependency "leads_to_highrise/application_controller" require 'highrise' module LeadsToHighrise class LeadsController < ApplicationController before_action :set_lead, only: [:show, :edit, :update, :destroy] # GET /leads def index @leads = Lead.all end # GET /leads/1 def show end # GET /leads/new def new @lead = Lead.new end # GET /leads/1/edit def edit end # POST /leads def create @lead = Lead.new(lead_params) if @lead.save redirect_to @lead, notice: 'Lead was successfully created.' else render :new end end # PATCH/PUT /leads/1 def update if @lead.update(lead_params) redirect_to @lead, notice: 'Lead was successfully updated.' else render :edit end end # DELETE /leads/1 def destroy @lead = Lead.find(params[:id]) @lead.destroy redirect_to leads_url, notice: 'Lead was successfully destroyed.' end def to_highrise @lead = Lead.find(params[:id]) add_person @lead end private def add_person(lead) @lead = lead Highrise::Base.site = current_user.highrise_site Highrise::Base.user = current_user.highrise_user Highrise::Base.format = :xml p = Highrise::Person.new( first_name: @lead[:name], last_name: @lead[:last_name], title: @lead[:job_title], company_name: @lead[:company], contact_data: { email_addresses: [ { address: @lead[:emial]} ], phone: @lead[:phone], website: @lead[:website] }) if p.save redirect_to main_app.root_path else redirect_to main_app.root_path end end # Use callbacks to share common setup or constraints between actions. def set_lead @lead = Lead.find(params[:id]) end # Only allow a trusted parameter "white list" through. def lead_params params.require(:lead).permit(:name, :last_name, :email, :company, :job_title, :phone, :website) end end end