video-program/app/models/video_program.rb

81 lines
3.2 KiB
Ruby

class VideoProgram
include Mongoid::Document
include Mongoid::Timestamps
include OrbitModel::Status
include OrbitModel::Impression
# encoding: utf-8
include OrbitTag::Taggable
include OrbitCategory::Categorizable
include Slug
field :custom_carousel_image_width, type: String, default: ""
field :display_video_image, :type => Boolean, :default => false
mount_uploader :video_image, ImageUploader
field :video_image_description, localize: true
field :display_image, :type => Boolean, :default => false
mount_uploader :image, ImageUploader
field :image_description, localize: true
field :title, as: :slug_title, type: String, localize: true
field :subtitle, localize: true
field :program_notes, type: String, localize: true
field :actor_notes, type: String, localize: true
field :video_notes, type: String, localize: true
field :video_external_link, type: String
field :episodes, type: String
field :create_user_id
field :update_user_id
field :postdate , :type => DateTime, :default => Time.now
field :deadline , :type => DateTime
has_many :video_program_links, :autosave => true, :dependent => :destroy
has_many :video_program_files, :autosave => true, :dependent => :destroy
has_many :video_program_carousel_images, :autosave => true, :dependent => :destroy
accepts_nested_attributes_for :video_program_files, :allow_destroy => true
accepts_nested_attributes_for :video_program_links, :allow_destroy => true
accepts_nested_attributes_for :video_program_carousel_images, :allow_destroy => true
has_and_belongs_to_many :albums
accepts_nested_attributes_for :albums, :allow_destroy => true
scope :open_in_future, ->{where(:is_hidden.ne=>true,:postdate.gt=>Time.now).order(postdate: :asc)}
scope :can_display_and_sorted, ->{where(:is_hidden.ne=>true).valid_time_range.order(postdate: :desc)}
scope :can_display_and_sorted_according_today, ->{where(:is_hidden.ne=>true).order(postdate: :asc).valid_time_range.where(:postdate.gte => Date.today.to_time)}
scope :valid_time_range, ->{and_any_of([{"postdate"=>{"$lte"=> Time.now}, "deadline"=>{"$gte"=> Time.now}}, {"postdate"=>{"$lte"=> Time.now}, "deadline"=>nil}]).order((@manually_sort ? {is_top: :desc,postdate: :desc,id: :desc} : {is_top: :desc,postdate: :desc,id: :desc}))}
scope :is_approved_and_show, ->{where(:approved => true,:is_hidden.ne=>true)}
scope :filter_cats_and_tags, ->(cats,tags) {filter_by_widget_categories(cats,false).filter_by_tags(tags)}
before_save do
if @is_hidden_changed.nil? || @is_hidden_changed != true
@is_hidden_changed = self.is_hidden_changed?
end
end
index({postdate: 1}, { unique: false, background: true })
index({is_top: -1, postdate: -1, _id: -1}, { unique: false, background: true })
index({is_hidden: 1, is_top: -1, postdate: -1, _id: -1, deadline: -1}, { unique: false, background: true })
def update_user
User.find(update_user_id) rescue nil
end
def update_user=(user)
self.update_user_id = user.id
end
def expired?
(self.deadline < Time.now) rescue false
end
def carousel_image_width
(self.custom_carousel_image_width.blank? ? nil : self.custom_carousel_image_width)
end
end