2012-01-11 12:31:52 +00:00
|
|
|
# encoding: utf-8
|
2012-07-20 07:47:40 +00:00
|
|
|
# require "impressionist"
|
2012-01-11 12:31:52 +00:00
|
|
|
class Bulletin
|
|
|
|
include Mongoid::Document
|
|
|
|
include Mongoid::Timestamps
|
|
|
|
include Mongoid::MultiParameterAttributes
|
2012-08-03 01:30:12 +00:00
|
|
|
include Sunspot::Mongo
|
2012-05-15 13:26:09 +00:00
|
|
|
include Impressionist::Impressionable
|
2012-07-11 09:17:44 +00:00
|
|
|
|
|
|
|
BelongsToCategory = :bulletin_category
|
|
|
|
include OrbitCoreLib::BelongsToCategoryMayDisable
|
2012-09-04 04:31:27 +00:00
|
|
|
include OrbitCoreLib::Preview
|
2012-11-22 01:30:25 +00:00
|
|
|
|
2012-07-30 09:57:55 +00:00
|
|
|
is_impressionable :counter_cache => { :column_name => :view_count }
|
2012-02-13 09:33:48 +00:00
|
|
|
|
2012-07-25 18:27:43 +00:00
|
|
|
field :title, localize: true
|
|
|
|
field :subtitle, localize: true
|
|
|
|
field :text, localize: true
|
|
|
|
|
2012-02-15 07:18:11 +00:00
|
|
|
has_and_belongs_to_many :tags, :class_name => "AnnouncementTag"
|
2012-02-13 11:34:06 +00:00
|
|
|
|
2012-05-17 01:59:49 +00:00
|
|
|
field :postdate , :type => DateTime
|
|
|
|
field :deadline , :type => DateTime
|
2012-02-06 07:23:27 +00:00
|
|
|
# field :url
|
2012-08-07 11:21:54 +00:00
|
|
|
|
2012-01-12 10:38:50 +00:00
|
|
|
field :create_user_id
|
2012-04-29 15:39:28 +00:00
|
|
|
field :update_user_id, :class_name => "User"
|
2012-01-11 12:31:52 +00:00
|
|
|
|
2012-01-13 11:15:03 +00:00
|
|
|
field :is_top, :type => Boolean, :default => false
|
2012-02-13 11:34:06 +00:00
|
|
|
field :is_hot, :type => Boolean, :default => false
|
|
|
|
field :is_hidden, :type => Boolean, :default => false
|
2012-04-23 16:46:34 +00:00
|
|
|
field :is_checked, :type => Boolean, :default => false
|
|
|
|
field :is_pending, :type => Boolean, :default => true
|
|
|
|
field :is_rejected, :type => Boolean, :default => false
|
2012-05-15 13:26:09 +00:00
|
|
|
field :view_count, :type => Integer, :default => 0
|
2012-03-16 03:09:44 +00:00
|
|
|
|
|
|
|
field :not_checked_reason
|
2012-01-13 11:15:03 +00:00
|
|
|
|
2012-02-14 16:32:20 +00:00
|
|
|
field :public, :type => Boolean, :default => true
|
|
|
|
|
2012-08-16 10:55:34 +00:00
|
|
|
scope :can_display, where(is_checked: true, is_rejected: false, is_pending: false)
|
2012-07-31 10:22:40 +00:00
|
|
|
scope :available_for_lang, ->(locale){ where("available_for_#{locale}".to_sym => true) }
|
|
|
|
|
2012-01-11 12:31:52 +00:00
|
|
|
mount_uploader :image, ImageUploader
|
2012-01-12 10:38:50 +00:00
|
|
|
|
2012-03-14 12:05:03 +00:00
|
|
|
has_many :bulletin_links, :autosave => true, :dependent => :destroy
|
|
|
|
has_many :bulletin_files, :autosave => true, :dependent => :destroy
|
2012-01-11 12:31:52 +00:00
|
|
|
|
|
|
|
accepts_nested_attributes_for :bulletin_files, :allow_destroy => true
|
2012-02-06 07:23:27 +00:00
|
|
|
accepts_nested_attributes_for :bulletin_links, :allow_destroy => true
|
2012-01-11 12:31:52 +00:00
|
|
|
|
2012-07-31 06:25:46 +00:00
|
|
|
validates :title, :at_least_one => true
|
2012-08-07 11:21:54 +00:00
|
|
|
|
2012-12-31 10:03:13 +00:00
|
|
|
before_save :check_deadline, :update_avliable_language, :clean_values#, :save_bulletin_links, :save_bulletin_files
|
2012-06-20 06:50:51 +00:00
|
|
|
|
2012-08-03 01:30:12 +00:00
|
|
|
searchable do
|
|
|
|
text :titles do
|
|
|
|
title_translations.to_a.collect{|t| t[1]}
|
|
|
|
end
|
|
|
|
text :texts do
|
2012-08-03 04:45:47 +00:00
|
|
|
text_translations.to_a.collect{|t| Nokogiri::HTML(t[1]).text}
|
2012-08-03 01:30:12 +00:00
|
|
|
end
|
2012-09-24 10:30:19 +00:00
|
|
|
|
2012-09-24 07:37:28 +00:00
|
|
|
string :available_lang , :multiple => true do
|
|
|
|
VALID_LOCALES.collect{|t| enabled_for_lang(t.to_sym) ? t : nil}.delete_if{|t| t.nil?}
|
|
|
|
end
|
2012-09-24 10:30:19 +00:00
|
|
|
|
2012-08-03 01:30:12 +00:00
|
|
|
# text :text do
|
|
|
|
# a = (title_translations["zh_tw"] + title_translations["en"])
|
|
|
|
# a
|
|
|
|
# end
|
|
|
|
boolean :frontend_search do
|
2012-08-03 04:45:47 +00:00
|
|
|
( !is_hidden && !is_pending && is_checked && !is_rejected )
|
2012-08-03 01:30:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
integer :view_count
|
|
|
|
string :bulletin_category_id
|
|
|
|
end
|
2012-09-19 07:09:08 +00:00
|
|
|
|
2013-01-04 10:42:25 +00:00
|
|
|
def bulletin_category_with_title
|
|
|
|
self.bulletin_category.title
|
|
|
|
end
|
|
|
|
|
2012-09-19 07:09:08 +00:00
|
|
|
def share_item
|
|
|
|
Item.first(conditions:{name: 'announcement'})
|
|
|
|
end
|
2012-07-31 10:22:40 +00:00
|
|
|
|
2012-08-03 04:45:47 +00:00
|
|
|
def proc_check(check,not_pass_info = "")
|
|
|
|
self.is_checked = true
|
|
|
|
if check =="true"
|
|
|
|
self.is_rejected = false
|
|
|
|
elsif check == "false"
|
|
|
|
self.is_rejected = true
|
|
|
|
self.not_checked_reason = not_pass_info
|
|
|
|
end
|
|
|
|
end
|
2012-07-27 11:21:39 +00:00
|
|
|
|
|
|
|
def de_pending
|
|
|
|
self.is_pending = false
|
|
|
|
end
|
2012-07-11 09:17:44 +00:00
|
|
|
|
2012-08-03 04:45:47 +00:00
|
|
|
def de_pending!
|
|
|
|
de_pending
|
|
|
|
self.save!
|
|
|
|
end
|
|
|
|
|
2012-08-07 11:21:54 +00:00
|
|
|
def enabled_for_lang(lang)
|
|
|
|
eval("self.available_for_#{lang}")
|
|
|
|
end
|
|
|
|
|
2012-07-27 11:21:39 +00:00
|
|
|
def publish_month
|
|
|
|
published_at.strftime("%B %Y")
|
|
|
|
end
|
2012-01-13 11:15:03 +00:00
|
|
|
|
2012-01-18 13:34:26 +00:00
|
|
|
def self.search( search = nil, category_id = nil )
|
2012-03-22 06:33:59 +00:00
|
|
|
if category_id.to_s.size > 0 and search.to_s.size > 0
|
2012-01-18 13:34:26 +00:00
|
|
|
key = /#{search}/
|
2012-03-22 06:33:59 +00:00
|
|
|
find(:all, :conditions => {title: key, bulletin_category_id: category_id}).desc( :is_top, :postdate )
|
|
|
|
elsif category_id.to_s.size > 0 and search.to_s.size < 1
|
|
|
|
find(:all, :conditions => {bulletin_category_id: category_id}).desc( :is_top, :postdate )
|
|
|
|
elsif search.to_s.size > 0 and category_id.to_s.size < 1
|
2012-01-18 13:34:26 +00:00
|
|
|
key = /#{search}/
|
2012-03-22 06:33:59 +00:00
|
|
|
find(:all, :conditions => {title: key}).desc( :is_top, :postdate )
|
|
|
|
else
|
|
|
|
find(:all).desc( :is_top, :postdate)
|
|
|
|
end
|
2012-01-13 11:15:03 +00:00
|
|
|
end
|
|
|
|
|
2012-01-11 12:31:52 +00:00
|
|
|
|
2012-02-16 09:52:49 +00:00
|
|
|
def self.widget_datas( category_id = nil )
|
2012-01-18 13:34:26 +00:00
|
|
|
|
2012-02-16 09:52:49 +00:00
|
|
|
date_now = Time.now
|
2012-02-15 02:37:07 +00:00
|
|
|
|
2012-02-16 09:52:49 +00:00
|
|
|
# find(:all, :conditions => {:postdate => {"$lte" => Date.today}, deadline: nil} ).desc( :is_top, :postdate)
|
|
|
|
# where( :postdate.lte => date_now ).where( :deadline => nil ).desc(:is_top, :postdate)
|
|
|
|
# any_of({ :title => "test" },{:deadline => nil, :title => "123"})
|
|
|
|
|
|
|
|
if category_id.to_s.size > 0
|
|
|
|
find(:all, :conditions => {bulletin_category_id: category_id}).any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate)
|
|
|
|
else
|
|
|
|
any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate)
|
|
|
|
end
|
|
|
|
|
2012-01-18 13:34:26 +00:00
|
|
|
end
|
2012-03-22 06:33:59 +00:00
|
|
|
|
|
|
|
def is_expired?
|
|
|
|
Date.today > self.deadline ? true : false rescue false
|
|
|
|
#some dates might sat as nil so rescue false
|
|
|
|
end
|
2012-01-18 13:34:26 +00:00
|
|
|
|
2012-01-13 11:15:03 +00:00
|
|
|
def is_top?
|
|
|
|
self.is_top
|
|
|
|
end
|
2012-01-11 12:31:52 +00:00
|
|
|
|
2012-02-13 11:34:06 +00:00
|
|
|
def is_hot?
|
|
|
|
self.is_hot
|
|
|
|
end
|
|
|
|
|
|
|
|
def is_hidden?
|
|
|
|
self.is_hidden
|
2012-02-06 07:23:27 +00:00
|
|
|
end
|
2012-07-17 02:21:46 +00:00
|
|
|
|
2012-03-22 06:33:59 +00:00
|
|
|
def is_checked?
|
2012-07-17 02:21:46 +00:00
|
|
|
!self.is_pending && self.is_checked && (self.is_rejected == false)
|
2012-03-22 06:33:59 +00:00
|
|
|
end
|
2012-04-23 16:46:34 +00:00
|
|
|
|
|
|
|
def is_pending?
|
|
|
|
self.is_pending
|
|
|
|
end
|
|
|
|
|
|
|
|
def is_rejected?
|
2012-07-17 02:21:46 +00:00
|
|
|
!self.is_pending && self.is_rejected && (self.is_rejected == true)
|
2012-03-22 06:33:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2012-03-14 12:05:03 +00:00
|
|
|
def save_bulletin_links
|
|
|
|
self.bulletin_links.each do |t|
|
|
|
|
if t.should_destroy
|
|
|
|
t.destroy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def save_bulletin_files
|
|
|
|
self.bulletin_files.each do |t|
|
|
|
|
if t.should_destroy
|
|
|
|
t.destroy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-02-24 07:30:49 +00:00
|
|
|
|
|
|
|
def self.filter(bulletins)
|
|
|
|
bulletins.each do |bulletin|
|
|
|
|
p "#{bulletin.id}/#{bulletin.is_top}/#{bulletin.is_hot}/#{bulletin.is_hidden}"
|
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def sorted_tags
|
|
|
|
tags.order_by(I18n.locale, :asc)
|
|
|
|
end
|
2012-02-13 11:34:06 +00:00
|
|
|
|
2012-09-07 09:55:59 +00:00
|
|
|
|
|
|
|
def to_preview
|
|
|
|
preview_object = Preview.new(:expired_at=>DateTime.now+30.minutes,:preview_at_link=>"panel_announcement_front_end_bulletin_path",:object_class_type=>"Bulletin")
|
|
|
|
|
|
|
|
self.bulletin["bulletin_files_attributes"].each_with_index do |atr,idx|
|
|
|
|
preview_object.preview_files.build(:file=>self.bulletin["bulletin_files_attributes"][idx.to_s],:field_name_for_rebuild=>'bulletin_files',:file_in_array=>true)
|
|
|
|
end unless self.bulletin["bulletin_files_attributes"].nil?
|
2012-09-10 06:19:39 +00:00
|
|
|
|
|
|
|
self.bulletin["bulletin_links_attributes"].each_with_index do |atr,idx|
|
|
|
|
preview_object.preview_associations.build(:object=>self.bulletin["bulletin_links_attributes"][idx.to_s],:field_name_for_rebuild=>'bulletin_links',:object_in_array=>true)
|
|
|
|
end unless self.bulletin["bulletin_links_attributes"].nil?
|
|
|
|
|
|
|
|
|
|
|
|
preview_object.object = self.bulletin.except("bulletin_files_attributes","bulletin_links_attributes")
|
2012-09-07 09:55:59 +00:00
|
|
|
preview_object
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2012-02-13 11:34:06 +00:00
|
|
|
protected
|
|
|
|
|
2012-06-29 03:17:35 +00:00
|
|
|
def check_deadline
|
2012-08-14 05:49:24 +00:00
|
|
|
if(!self.deadline.nil? and (self.deadline < self.postdate ))
|
|
|
|
self.deadline = nil
|
2012-06-29 03:17:35 +00:00
|
|
|
end
|
|
|
|
end
|
2012-01-11 12:31:52 +00:00
|
|
|
|
2012-07-31 10:22:40 +00:00
|
|
|
def update_avliable_language
|
|
|
|
VALID_LOCALES.each do |locale|
|
2012-07-31 16:58:59 +00:00
|
|
|
if (title_translations[locale].blank? rescue true)
|
2012-07-31 10:22:40 +00:00
|
|
|
self["available_for_#{locale}".to_sym] = false
|
|
|
|
else
|
|
|
|
self["available_for_#{locale}".to_sym] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-08-07 11:21:54 +00:00
|
|
|
|
2012-09-12 11:12:50 +00:00
|
|
|
def clean_values
|
|
|
|
self.bulletin_links.each do |link|
|
|
|
|
link.delete if link.url.blank? && link.title.blank?
|
|
|
|
end
|
2012-08-14 06:38:21 +00:00
|
|
|
self.tag_ids.delete('')
|
|
|
|
end
|
|
|
|
|
2012-09-07 09:55:59 +00:00
|
|
|
|
2012-08-08 09:56:00 +00:00
|
|
|
end
|