vidpush/app/controllers/campaigns_controller.rb
2014-05-05 14:07:33 -04:00

100 lines
3.2 KiB
Ruby

class CampaignsController < ApplicationController
before_action :set_campaign, only: [:show, :edit, :update, :destroy]
before_action :set_api_key
def index
@campaigns = Campaign.all
end
def show
@campaign
end
def new
@campaign = Campaign.new
@campaign.videolistings.build
@action = campaigns_path
end
def edit
if @campaign.sent
redirect_to campaign_path(@campaign.listingcode), notice: 'Campaign has been submitted, Cannot edit.'
end
@action = campaign_path(@campaign.listingcode)
end
def create
@campaign = Campaign.new campaign_params
if @campaign.save
respond_to do |format|
format.json { render json: @campaign }
format.html { redirect_to campaign_path(@campaign.listingcode), notice: 'Campaign was successfully created.' }
end
else
show_errors('new')
end
end
def update
notice = 'Campaign was saved for later use. It is not published yet.'
if @campaign.update(campaign_params)
if params[:send]
client = BetterVideo.new #(:url => "xxx")
bv_response = client.addVideo(campaign: @campaign.to_bettervideo_format)
if bv_response[:success]
notice = bv_response[:message]
@campaign.update({sent: true})
return redirect_to campaign_path(@campaign.listingcode), notice: notice
else
flash.now[:alert] = bv_response[:message]
return show_errors('edit')
end
end
else
return show_errors('edit')
end
redirect_to campaign_path(@campaign.listingcode), notice: notice
end
def destroy
@campaign.destroy
redirect_to campaigns_url, notice: 'Campaign was successfully destroyed.'
end
private
def show_errors(where_to)
respond_to do |format|
format.json { render json: {error: @campaign.errors.full_messages } }
format.html { render action: where_to }
end
end
def set_campaign
@campaign = set_by_options(:campaign, :listingcode)
end
def campaign_params
params.require(:campaign).permit(:id, :address, :clientid, :advertisername, :awards, :background, :billingcode,
:businessphone, :categories, :city, :companycolors, :contactphone,
:customerfirstname, :customerlastname, :description, :emailaddress, :facebookurl,
:listingcode, :productsandservices, :state, :targetaudience, :tollfreephone,
:vpa, :vpa_email, :websiteurl, :zip,
videolistings_attributes: [:_destroy, :id, :asseturls, :clientprovidedscript, :focus,
:keywords, :musicselection, :notes,
:toneofvideo, :videocode, :voiceoverselection,
])
end
def not_found
raise ActionController::RoutingError.new("Not Found by listing code")
end
def set_api_key
@BETTER_VIDEO_API_KEY = ENV['BETTER_VIDEO_API_KEY']
@BETTER_VIDEO_API_KEY ||= "propel test key"
end
end