nccu-news/app/models/news_bulletin.rb

107 lines
2.9 KiB
Ruby
Raw Permalink Normal View History

2014-11-07 08:44:11 +00:00
class NewsBulletin
include Mongoid::Document
include Mongoid::Timestamps
include OrbitModel::Status
include OrbitModel::Impression
# encoding: utf-8
include OrbitTag::Taggable
include OrbitCategory::Categorizable
include Slug
2023-09-24 07:15:32 +00:00
field :is_edit, type: Boolean, default: false #use to check whether the preview record changed
field :copy_id
2014-11-07 08:44:11 +00:00
field :title, as: :slug_title, type: String, localize: true
field :subtitle, localize: true
field :text, localize: true
field :create_user_id
field :update_user_id
field :public, :type => Boolean, :default => true
field :postdate , :type => DateTime, :default => Time.now
field :deadline , :type => DateTime
field :rss2_sn
field :approved, :type => Boolean, :default => false
2014-11-14 09:50:18 +00:00
field :rejected, :type => Boolean, :default => false
2015-03-26 10:10:59 +00:00
field :reapproval, :type => Boolean, :default => false
2014-11-14 09:50:18 +00:00
field :rejection_reason
2014-11-07 08:44:11 +00:00
field :is_preview, :type => Boolean, :default => false
2015-01-12 06:40:36 +00:00
field :image_description, localize: true
2015-03-06 01:31:21 +00:00
field :expirable_created_at, type: DateTime
2014-11-07 08:44:11 +00:00
field :email_id
field :email_sent, :type => Boolean, :default => false
field :email_sentdate , :type => DateTime
field :email_member_ids
field :other_mailaddress
mount_uploader :image, ImageUploader
belongs_to :unit
belongs_to :department
has_many :news_bulletin_links, :autosave => true, :dependent => :destroy
has_many :news_bulletin_files, :autosave => true, :dependent => :destroy
accepts_nested_attributes_for :news_bulletin_files, :allow_destroy => true
accepts_nested_attributes_for :news_bulletin_links, :allow_destroy => true
before_destroy :destroy_email
2015-03-06 01:31:21 +00:00
before_create :set_expire
def set_expire
self.expirable_created_at = Time.now if self.is_preview
return true
end
2014-11-07 08:44:11 +00:00
scope :can_display, ->{where(:is_hidden=>false).any_of({:postdate.lt=>Time.now, :deadline.gt=>Time.now},{:postdate.lt=>Time.now, :deadline=>nil}).order_by([:is_top, :desc])}
2015-03-06 01:31:21 +00:00
scope :can_display_postdate, ->{where(:is_hidden=>false,:postdate.lt=>Time.now).order_by([:is_top, :desc])}
2014-11-07 08:44:11 +00:00
scope :is_approved, ->{where(:approved => true)}
2023-09-24 07:15:32 +00:00
def get_org_model
if self.is_preview
org_model = nil
if self.copy_id
org_model = self.class.find(self.copy_id) rescue nil
end
org_model.nil? ? self : org_model
else
self
end
end
2014-11-07 08:44:11 +00:00
def update_user
User.find(update_user_id) rescue nil
end
def update_user=(user)
self.update_user_id = user.id
end
def email_members
MemberProfile.find(self.email_member_ids) rescue []
end
def email_addresses
addresses = self.email_members.collect{|member| member.email} rescue []
addresses = addresses +[self.other_mailaddress] if !self.other_mailaddress.blank?
addresses.flatten
end
def email
mail = Email.find(self.email_id) rescue nil
end
def expired?
(self.deadline < Time.now) rescue false
end
def destroy_email
mail = Email.find(self.email_id) rescue nil
mail.destroy if !mail.nil?
end
end