2014-04-07 18:41:34 +00:00
|
|
|
class CampaignsController < ApplicationController
|
|
|
|
before_action :set_campaign, only: [:show, :edit, :update, :destroy]
|
2014-04-21 15:07:45 +00:00
|
|
|
before_action :set_api_key
|
2014-04-07 18:41:34 +00:00
|
|
|
|
|
|
|
def index
|
|
|
|
@campaigns = Campaign.all
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
2014-04-21 15:07:45 +00:00
|
|
|
@campaign
|
2014-04-07 18:41:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
|
|
|
@campaign = Campaign.new
|
2014-04-09 15:06:07 +00:00
|
|
|
@campaign.videolistings.build
|
2014-04-15 20:59:07 +00:00
|
|
|
@action = campaigns_path
|
2014-04-07 18:41:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
2014-04-29 17:03:46 +00:00
|
|
|
if @campaign.sent
|
2014-04-24 18:09:21 +00:00
|
|
|
redirect_to campaign_path(@campaign.listingcode), notice: 'Campaign has been submitted, Cannot edit.'
|
|
|
|
end
|
2014-04-22 15:02:40 +00:00
|
|
|
@action = campaign_path(@campaign.listingcode)
|
2014-04-07 18:41:34 +00:00
|
|
|
end
|
|
|
|
|
2014-04-09 21:39:48 +00:00
|
|
|
|
2014-04-07 18:41:34 +00:00
|
|
|
def create
|
2014-04-09 15:06:07 +00:00
|
|
|
@campaign = Campaign.new campaign_params
|
2014-04-08 20:02:51 +00:00
|
|
|
|
2014-04-07 18:41:34 +00:00
|
|
|
if @campaign.save
|
2014-04-08 19:27:38 +00:00
|
|
|
respond_to do |format|
|
|
|
|
format.json { render json: @campaign }
|
2014-04-22 15:02:40 +00:00
|
|
|
format.html { redirect_to campaign_path(@campaign.listingcode), notice: 'Campaign was successfully created.' }
|
2014-04-08 19:27:38 +00:00
|
|
|
end
|
2014-04-07 18:41:34 +00:00
|
|
|
else
|
2014-04-25 15:37:17 +00:00
|
|
|
show_errors('new')
|
2014-04-07 18:41:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2014-04-29 21:05:21 +00:00
|
|
|
notice = 'Campaign was saved for later use. It is not published yet.'
|
|
|
|
|
2014-04-07 18:41:34 +00:00
|
|
|
if @campaign.update(campaign_params)
|
2014-04-29 17:03:46 +00:00
|
|
|
if params[:send]
|
2014-04-30 17:57:58 +00:00
|
|
|
client = BetterVideo.new #(:url => "xxx")
|
|
|
|
bv_response = client.addVideo(campaign: @campaign.to_bettervideo_format)
|
2014-04-29 21:05:21 +00:00
|
|
|
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
|
2014-04-29 17:03:46 +00:00
|
|
|
end
|
2014-04-07 18:41:34 +00:00
|
|
|
else
|
2014-04-29 21:05:21 +00:00
|
|
|
return show_errors('edit')
|
2014-04-07 18:41:34 +00:00
|
|
|
end
|
2014-04-29 21:05:21 +00:00
|
|
|
redirect_to campaign_path(@campaign.listingcode), notice: notice
|
2014-04-07 18:41:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@campaign.destroy
|
|
|
|
redirect_to campaigns_url, notice: 'Campaign was successfully destroyed.'
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2014-04-25 15:37:17 +00:00
|
|
|
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
|
2014-04-07 18:41:34 +00:00
|
|
|
def set_campaign
|
2014-04-25 15:32:58 +00:00
|
|
|
@campaign = set_by_options(:campaign, :listingcode)
|
2014-04-07 18:41:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def campaign_params
|
2014-04-24 20:33:48 +00:00
|
|
|
params.require(:campaign).permit(:id, :address, :clientid, :advertisername, :awards, :background, :billingcode,
|
2014-04-24 18:09:21 +00:00
|
|
|
:businessphone, :categories, :city, :companycolors, :contactphone,
|
2014-04-21 15:07:45 +00:00
|
|
|
:customerfirstname, :customerlastname, :description, :emailaddress, :facebookurl,
|
|
|
|
:listingcode, :productsandservices, :state, :targetaudience, :tollfreephone,
|
|
|
|
:vpa, :websiteurl, :zip,
|
2014-04-21 21:22:26 +00:00
|
|
|
|
2014-04-24 00:33:59 +00:00
|
|
|
videolistings_attributes: [:_destroy, :id, :asseturls, :clientprovidedscript, :focus,
|
2014-04-24 18:09:21 +00:00
|
|
|
:keywords, :musicselection, :notes,
|
2014-04-21 15:07:45 +00:00
|
|
|
:videocode, :voiceoverselection,
|
|
|
|
])
|
2014-04-08 19:27:38 +00:00
|
|
|
end
|
|
|
|
|
2014-04-09 15:06:07 +00:00
|
|
|
def not_found
|
2014-04-24 20:33:48 +00:00
|
|
|
raise ActionController::RoutingError.new("Not Found by listing code")
|
2014-04-09 15:06:07 +00:00
|
|
|
end
|
2014-04-09 18:12:14 +00:00
|
|
|
|
2014-04-21 15:07:45 +00:00
|
|
|
def set_api_key
|
|
|
|
@BETTER_VIDEO_API_KEY = ENV['BETTER_VIDEO_API_KEY']
|
|
|
|
@BETTER_VIDEO_API_KEY ||= "propel test key"
|
|
|
|
end
|
2014-04-07 18:41:34 +00:00
|
|
|
end
|