vidpush/app/models/campaign.rb
2014-04-30 13:57:58 -04:00

51 lines
1.3 KiB
Ruby

require 'json'
class Campaign < ActiveRecord::Base
has_many :videolistings, :dependent => :delete_all
accepts_nested_attributes_for :videolistings,:reject_if => :all_blank, :allow_destroy => true
validates :listingcode, presence: true, uniqueness: true
validates :advertisername, presence: true
validates :websiteurl, presence: true
validates :description, presence: true
validates :productsandservices, presence: true
# after_save :send_to_better_video
validate :has_at_least_one_videolisting
after_initialize :set_defaults
def set_defaults
self.clientid = 720
end
def to_builder
Jbuilder.new do |json|
json.partial! 'campaigns/campaign', campaign: self
end
end
def has_at_least_one_videolisting
errors.add(:base, 'Must add at least one Videolisting') if self.videolistings.blank?
end
def video_completion_percent
published = videolistings.where({published: true}).length
((published.to_f / videolistings.count.to_f) * 100).to_i
rescue
0
end
def to_bettervideo_format
campaign = self.as_json(include: :videolistings)
campaigns = {campaigns: [campaign,]}
campaigns.to_json
end
# def send_to_better_video
# if self.sent_changed? && self.sent? == true
#
# end
# end
end