Orbit/vendor/built_in_modules/announcement/app/models/bulletin.rb

101 lines
2.7 KiB
Ruby
Raw Normal View History

2012-01-11 12:31:52 +00:00
# encoding: utf-8
class Bulletin
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::MultiParameterAttributes
# field :category_id, :type => Integer
2012-02-06 07:23:27 +00:00
field :title
# has_one :title_variable, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy
# has_one :subtitle_variable, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy
# has_one :text_variable, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy
field :subtitle
field :text
2012-01-11 12:31:52 +00:00
field :postdate , :type => Date
field :deadline , :type => Date
2012-02-06 07:23:27 +00:00
# field :url
2012-01-12 10:38:50 +00:00
field :create_user_id
field :update_user_id
2012-01-11 12:31:52 +00:00
2012-01-13 11:15:03 +00:00
field :is_top, :type => Boolean, :default => false
2012-01-11 12:31:52 +00:00
mount_uploader :image, ImageUploader
belongs_to :bulletin_category
2012-02-06 07:23:27 +00:00
embeds_many :bulletin_links, :cascade_callbacks => true
2012-01-12 10:38:50 +00:00
embeds_many :bulletin_files, :cascade_callbacks => true
# 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-02-06 07:23:27 +00:00
# validates_presence_of :title_variable
validates_presence_of :title
2012-01-11 12:31:52 +00:00
2012-02-06 07:23:27 +00:00
after_save :save_bulletin_links
2012-01-11 12:31:52 +00:00
after_save :save_bulletin_files
2012-01-13 11:15:03 +00:00
2012-01-18 13:34:26 +00:00
def self.search( search = nil, category_id = nil )
if category_id.to_s.size > 0 and search.to_s.size > 0
key = /#{search}/
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
key = /#{search}/
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-01-18 13:34:26 +00:00
def self.widget_datas
2012-01-20 06:04:52 +00:00
date_now = Time.now
2012-01-18 13:34:26 +00:00
# find(:all, :conditions => {:postdate => {"$lte" => Date.today}, deadline: nil} ).desc( :is_top, :postdate)
2012-01-20 06:04:52 +00:00
# where( :postdate.lte => date_now ).where( :deadline => nil ).desc(:is_top, :postdate)
2012-01-18 13:34:26 +00:00
# any_of({ :title => "test" },{:deadline => nil, :title => "123"})
2012-02-09 06:58:59 +00:00
any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate)
2012-01-20 06:04:52 +00:00
2012-01-18 13:34:26 +00:00
end
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-06 07:23:27 +00:00
def save_bulletin_links
self.bulletin_links.each do |t|
if t.should_destroy
t.destroy
end
end
end
2012-01-11 12:31:52 +00:00
def save_bulletin_files
self.bulletin_files.each do |t|
if t.should_destroy
t.destroy
end
end
end
end