# encoding: utf-8 class Bulletin include Mongoid::Document include Mongoid::Timestamps include Mongoid::MultiParameterAttributes # field :category_id, :type => Integer field :title field :subtitle field :text field :postdate , :type => Date field :deadline , :type => Date field :url field :create_user_id field :update_user_id field :is_top, :type => Boolean, :default => false mount_uploader :image, ImageUploader belongs_to :bulletin_category # embeds_many :bulletin_files embeds_many :bulletin_files, :cascade_callbacks => true # has_many :bulletin_files, :autosave => true, :dependent => :destroy accepts_nested_attributes_for :bulletin_files, :allow_destroy => true validates_presence_of :title after_save :save_bulletin_files 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 end def self.widget_datas date_now = Time.now # 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"}) any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).limit(5) end def is_top? self.is_top end def save_bulletin_files self.bulletin_files.each do |t| if t.should_destroy t.destroy end end end end