151 lines
4.6 KiB
Ruby
151 lines
4.6 KiB
Ruby
class Bulletin
|
|
include Mongoid::Document
|
|
include Mongoid::Timestamps
|
|
|
|
include OrbitModel::Status
|
|
include OrbitModel::Impression
|
|
# encoding: utf-8
|
|
include OrbitTag::Taggable
|
|
include OrbitCategory::Categorizable
|
|
include Slug
|
|
require 'bulletin_model/cache'
|
|
include BulletinModel::Cache
|
|
SubPart.class_eval { include BulletinModel::Cache }
|
|
before_destroy do
|
|
AnnsCache.all.destroy
|
|
end
|
|
field :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
|
|
field :is_preview, :type => Boolean, :default => false
|
|
field :expirable_created_at, type: DateTime
|
|
field :rejected, :type => Boolean, :default => false
|
|
field :reapproval, :type => Boolean, :default => false
|
|
field :rejection_reason
|
|
field :is_external_link, :type => Boolean, :default => false
|
|
field :external_link
|
|
field :display_subtitle, :type => Boolean, :default => false
|
|
field :display_img, :type => Boolean, :default => false
|
|
|
|
field :email_id
|
|
field :email_sent, :type => Boolean, :default => false
|
|
field :email_sentdate , :type => DateTime
|
|
field :email_member_ids
|
|
field :other_mailaddress
|
|
field :image_description, localize: true
|
|
field :top_end_date, :type => DateTime
|
|
|
|
mount_uploader :image, ImageUploader
|
|
|
|
has_many :bulletin_links, :autosave => true, :dependent => :destroy
|
|
has_many :bulletin_files, :autosave => true, :dependent => :destroy
|
|
|
|
accepts_nested_attributes_for :bulletin_files, :allow_destroy => true
|
|
accepts_nested_attributes_for :bulletin_links, :allow_destroy => true
|
|
|
|
before_destroy :destroy_email
|
|
|
|
scope :can_display_and_sorted, ->{where(:is_hidden=>false,:is_preview => false).any_of({:postdate.lte=>Time.now, :deadline.gte=>Time.now},{:postdate.lte=>Time.now, :deadline=>nil}).order(is_top: :desc,postdate: :desc,id: :desc)}
|
|
scope :is_approved, ->{where(:approved => true)}
|
|
|
|
before_create :set_expire
|
|
before_save :check_limit
|
|
def check_limit
|
|
check_status_limit(update_user)
|
|
end
|
|
def check_status_limit(user,check_only=false)
|
|
role_ids = user.member_profile.roles.map(&:id) rescue []
|
|
status_settings = (role_ids.collect do |role_id|
|
|
AnnouncementSetting.first.anns_status_settings.select{|v| v.role_id.to_s == role_id.to_s}
|
|
end.flatten rescue [])
|
|
reach_limit = []
|
|
if status_settings.count != 0
|
|
reach_limit = status_settings.collect do |status_setting|
|
|
status = status_setting.status
|
|
if status_setting.top_limit.to_i <= Bulletin.where(:update_user_id.in => Role.find(status_setting.role_id).member_profiles.collect(&:user).flatten.uniq.map{|v| v.id},status => true).count
|
|
if !check_only
|
|
if self[status] && !Bulletin.where(id:self.id).first[status]
|
|
self[status] = false
|
|
nil
|
|
end
|
|
else
|
|
status
|
|
end
|
|
else
|
|
nil
|
|
end
|
|
end.compact
|
|
reach_limit = reach_limit.group_by{|v| v}.collect do |k,v|
|
|
if v.count >= user.member_profile.roles.count
|
|
k
|
|
else
|
|
nil
|
|
end
|
|
end.compact
|
|
end
|
|
reach_limit
|
|
end
|
|
def slug_title
|
|
doc = Nokogiri::HTML(self.title)
|
|
title = doc.text.gsub('/','-')
|
|
end
|
|
def set_expire
|
|
self.expirable_created_at = Time.now if self.is_preview
|
|
return true
|
|
end
|
|
|
|
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
|
|
|
|
def self.remove_expired_status
|
|
self.where(:is_top => true, :top_end_date.ne => nil, :top_end_date.lt => Time.now).each do |b|
|
|
b.is_top = false
|
|
b.top_end_date = nil
|
|
b.save
|
|
end
|
|
end
|
|
|
|
def display_subtitle?
|
|
self.display_subtitle rescue false
|
|
end
|
|
|
|
def display_img?
|
|
self.display_img rescue false
|
|
end
|
|
end
|