vidpush/app/controllers/campaigns_controller.rb

91 lines
2.9 KiB
Ruby
Raw Normal View History

2014-04-07 18:41:34 +00:00
class CampaignsController < ApplicationController
before_action :set_campaign, only: [:show, :edit, :update, :destroy]
before_action :set_api_key
2014-04-07 18:41:34 +00:00
def index
@campaigns = Campaign.all
end
def show
@campaign
2014-04-07 18:41:34 +00:00
end
def new
@campaign = Campaign.new
@campaign.videolistings.build
@action = campaigns_path
2014-04-07 18:41:34 +00:00
end
def edit
2014-04-24 18:09:21 +00:00
if @campaign.processed
redirect_to campaign_path(@campaign.listingcode), notice: 'Campaign has been submitted, Cannot edit.'
end
@action = campaign_path(@campaign.listingcode)
2014-04-07 18:41:34 +00:00
end
2014-04-07 18:41:34 +00:00
def create
@campaign = Campaign.new campaign_params
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 }
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-21 21:22:26 +00:00
respond_to do |format|
format.json { render json: {error: @campaign.errors.full_messages } }
format.html { render action: 'new' }
2014-04-21 21:22:26 +00:00
end
2014-04-07 18:41:34 +00:00
end
end
def update
if @campaign.update(campaign_params)
redirect_to campaign_path(@campaign.listingcode), notice: 'Campaign was successfully updated.'
2014-04-07 18:41:34 +00:00
else
2014-04-21 21:22:26 +00:00
respond_to do |format|
format.json { render json: {error: @campaign.errors.full_messages } }
format.html { render action: 'edit' }
2014-04-21 21:22:26 +00:00
end
2014-04-07 18:41:34 +00:00
end
end
def destroy
@campaign.destroy
redirect_to campaigns_url, notice: 'Campaign was successfully destroyed.'
end
private
def set_campaign
listingcode = params[:listingcode]
if params[:campaign]
@campaign = Campaign.find_by_listingcode(params[:campaign][:listingcode]) || not_found
else
@campaign = Campaign.find_by_listingcode(listingcode) || not_found
end
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,
:customerfirstname, :customerlastname, :description, :emailaddress, :facebookurl,
:listingcode, :productsandservices, :state, :targetaudience, :tollfreephone,
:vpa, :websiteurl, :zip,
2014-04-21 21:22:26 +00:00
videolistings_attributes: [:_destroy, :id, :asseturls, :clientprovidedscript, :focus,
2014-04-24 18:09:21 +00:00
:keywords, :musicselection, :notes,
:videocode, :voiceoverselection,
])
2014-04-08 19:27:38 +00:00
end
def not_found
2014-04-24 20:33:48 +00:00
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
2014-04-07 18:41:34 +00:00
end