2014-04-30 17:57:58 +00:00
|
|
|
require 'json'
|
|
|
|
|
2014-04-07 18:41:34 +00:00
|
|
|
class Campaign < ActiveRecord::Base
|
2014-04-09 18:12:14 +00:00
|
|
|
has_many :videolistings, :dependent => :delete_all
|
2014-04-24 00:33:59 +00:00
|
|
|
accepts_nested_attributes_for :videolistings,:reject_if => :all_blank, :allow_destroy => true
|
2014-04-08 21:42:43 +00:00
|
|
|
validates :listingcode, presence: true, uniqueness: true
|
|
|
|
validates :advertisername, presence: true
|
|
|
|
validates :websiteurl, presence: true
|
2014-04-21 18:26:46 +00:00
|
|
|
validates :description, presence: true
|
|
|
|
validates :productsandservices, presence: true
|
2014-04-24 19:47:47 +00:00
|
|
|
validate :has_at_least_one_videolisting
|
2014-04-24 21:13:39 +00:00
|
|
|
after_initialize :set_defaults
|
|
|
|
def set_defaults
|
|
|
|
self.clientid = 720
|
|
|
|
end
|
2014-04-30 17:57:58 +00:00
|
|
|
|
2014-04-24 19:47:47 +00:00
|
|
|
def has_at_least_one_videolisting
|
|
|
|
errors.add(:base, 'Must add at least one Videolisting') if self.videolistings.blank?
|
|
|
|
end
|
2014-04-15 17:04:58 +00:00
|
|
|
|
2014-04-28 18:16:43 +00:00
|
|
|
def video_completion_percent
|
|
|
|
published = videolistings.where({published: true}).length
|
|
|
|
((published.to_f / videolistings.count.to_f) * 100).to_i
|
|
|
|
rescue
|
|
|
|
0
|
|
|
|
end
|
2014-04-24 16:51:50 +00:00
|
|
|
|
2014-04-30 17:57:58 +00:00
|
|
|
def to_bettervideo_format
|
2014-05-05 18:07:33 +00:00
|
|
|
campaign = self.as_json(except: [:vpa_email,])
|
2014-05-02 20:55:27 +00:00
|
|
|
campaign[:videolistings] = self.videolistings.all.map {|vl| vl.as_json }
|
2014-05-02 19:57:31 +00:00
|
|
|
campaigns = {campaigns: [campaign,], key: "PRO1234"}
|
2014-04-30 17:57:58 +00:00
|
|
|
campaigns.to_json
|
|
|
|
end
|
|
|
|
|
2014-04-07 18:41:34 +00:00
|
|
|
end
|