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 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 field :rejected, :type => Boolean, :default => false field :rejection_reason field :is_preview, :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 :cache_dept, localize: true # field :cache_dept_id 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 # before_save :fetch_dept 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])} scope :is_approved, ->{where(:approved => true)} 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 get_member_list_attribute_field(role_type,field_key) @attribute = ::Role.where(:key => role_type).first return ::AttributeField.where(:key => field_key, :role_id => @attribute.id).first end def get_member_list_attribute_value(user_id,field_id) return ::AttributeValue.where(attribute_field_id: field_id, :member_profile_id => user_id).first end def refetch_dept user_id = User.where(:id => self.create_user_id).first.member_profile_id unit_field = get_member_list_attribute_field("staff","Unit") self[:cache_dept] = Hash.new Site.first.in_use_locales.each do |locale| self[:cache_dept][locale] = get_member_list_attribute_value(user_id,unit_field.id).get_value_by_locale(I18n.locale) rescue nil end end protected def fetch_dept refetch_dept if self.new_record? end end