require_dependency "rd_station/application_controller" require 'highrise' module RdStation 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.destroy redirect_to leads_url, notice: 'Lead was successfully destroyed.' end def send_to_highrise @lead = Lead.find(params[:id]) if current_user.highrise_site == nil || current_user.highrise_user == nil redirect_to root_path, notice: "plese add you setings of Highrise on edit profile page" else 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 root_path, notice: "This contact has ben sended to Highrise" else redirect_to root_path end end end private # 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, :emial, :company, :job_title, :phone, :website) end end end