Modify "can_display" to add "postdate" and "deadline"

This commit is contained in:
chris 2013-10-17 02:18:32 +08:00
parent 68a379f8a4
commit 19bf2f2be5
5 changed files with 50 additions and 14 deletions

View File

@ -9,9 +9,12 @@ module OrbitModel
field :is_pending, :type => Boolean, :default => true field :is_pending, :type => Boolean, :default => true
field :is_rejected, :type => Boolean, :default => false field :is_rejected, :type => Boolean, :default => false
field :not_checked_reason field :not_checked_reason
class_variable_defined?(:@@can_display) ? \ query = {type: 'where', conditions: {is_checked: true, is_rejected: false, is_pending: false}}
class_variable_set(:@@can_display, class_variable_get(:@@can_display).merge({is_checked: true, is_rejected: false, is_pending: false})) : \ if class_variable_defined?(:@@can_display)
class_variable_set(:@@can_display, {is_checked: true, is_rejected: false, is_pending: false}) class_variable_set(:@@can_display, (class_variable_get(:@@can_display) << query))
else
class_variable_set(:@@can_display, [query])
end
send :include, InstanceMethods send :include, InstanceMethods
end end
end end
@ -54,9 +57,14 @@ module OrbitModel
module ClassMethods module ClassMethods
define_method(:can_display) do define_method(:can_display) do
where(class_variable_get(:@@can_display)) final_query = class_variable_get(:@@can_display).inject('') do |result, query_hash|
result << "." if result.present?
result << "#{query_hash[:type]}(#{query_hash[:conditions]})"
result
end
eval(final_query)
end unless method_defined? :can_display end unless method_defined? :can_display
end end
end end

View File

@ -8,9 +8,12 @@ module OrbitModel
field :is_top, :type => Boolean, :default => false field :is_top, :type => Boolean, :default => false
field :is_hot, :type => Boolean, :default => false field :is_hot, :type => Boolean, :default => false
field :is_hidden, :type => Boolean, :default => false field :is_hidden, :type => Boolean, :default => false
self.class_variable_defined?(:@@can_display) ? \ query = {type: 'where', conditions: {is_hidden: false}}
self.class_variable_set(:@@can_display, self.class_variable_get(:@@can_display).merge({is_hidden: false})) : \ if class_variable_defined?(:@@can_display)
self.class_variable_set(:@@can_display, {is_hidden: false}) class_variable_set(:@@can_display, (class_variable_get(:@@can_display) << query))
else
class_variable_set(:@@can_display, [query])
end
send :include, InstanceMethods send :include, InstanceMethods
end end
end end
@ -34,7 +37,12 @@ module OrbitModel
module ClassMethods module ClassMethods
define_method(:can_display) do define_method(:can_display) do
where(class_variable_get(:@@can_display)) final_query = class_variable_get(:@@can_display).inject('') do |result, query_hash|
result << "." if result.present?
result << "#{query_hash[:type]}(#{query_hash[:conditions]})"
result
end
eval(final_query)
end unless method_defined? :can_display end unless method_defined? :can_display
end end

View File

@ -3,10 +3,17 @@ module OrbitModel
module TimeFrame module TimeFrame
def self.included(base) def self.included(base)
base.extend ClassMethods
base.class_eval do base.class_eval do
field :postdate , :type => DateTime field :postdate , :type => DateTime
field :deadline , :type => DateTime field :deadline , :type => DateTime
before_save :check_deadline before_save :check_deadline
query = {type: 'any_of', conditions: "{deadline: nil, :postdate.lte => Time.now} , {:deadline.gte => Time.now, :postdate.lte => Time.now}"}
if class_variable_defined?(:@@can_display)
class_variable_set(:@@can_display, (class_variable_get(:@@can_display) << query))
else
class_variable_set(:@@can_display, [query])
end
send :include, InstanceMethods send :include, InstanceMethods
end end
end end
@ -27,6 +34,19 @@ module OrbitModel
end end
module ClassMethods
define_method(:can_display) do
final_query = class_variable_get(:@@can_display).inject('') do |result, query_hash|
result << "." if result.present?
result << "#{query_hash[:type]}(#{query_hash[:conditions]})"
result
end
eval(final_query)
end unless method_defined? :can_display
end
end end
end end

View File

@ -43,15 +43,15 @@ class Panel::Announcement::FrontEnd::BulletinsController < OrbitWidgetController
else else
date_now = Time.now date_now = Time.now
if !params[:category_id].blank? && !params[:tag_id].blank? if !params[:category_id].blank? && !params[:tag_id].blank?
@bulletins = Bulletin.available_for_lang(I18n.locale).can_display.where(:category_id => params[:category_id], :tagged_ids => params[:tag_id]).any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page_main]).per(@page_num) @bulletins = Bulletin.available_for_lang(I18n.locale).can_display.where(:category_id => params[:category_id], :tagged_ids => params[:tag_id]).desc( :is_top, :postdate).page( params[:page_main]).per(@page_num)
@current_category = BulletinCategory.from_id(params[:category_id]) rescue nil @current_category = BulletinCategory.from_id(params[:category_id]) rescue nil
elsif !params[:category_id].blank? elsif !params[:category_id].blank?
@bulletins = Bulletin.all.available_for_lang(I18n.locale).can_display.where(:category_id => params[:category_id]).any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page_main]).per(@page_num) @bulletins = Bulletin.all.available_for_lang(I18n.locale).can_display.where(:category_id => params[:category_id]).desc( :is_top, :postdate).page( params[:page_main]).per(@page_num)
@current_category = BulletinCategory.from_id(params[:category_id]) rescue nil @current_category = BulletinCategory.from_id(params[:category_id]) rescue nil
elsif !params[:tag_id].blank? elsif !params[:tag_id].blank?
@bulletins = Bulletin.available_for_lang(I18n.locale).can_display.where(:tagged_ids => params[:tag_id]).any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page_main]).per(@page_num) @bulletins = Bulletin.available_for_lang(I18n.locale).can_display.where(:tagged_ids => params[:tag_id]).desc( :is_top, :postdate).page( params[:page_main]).per(@page_num)
else else
@bulletins = Bulletin.all.available_for_lang(I18n.locale).can_display.any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page_main]).per(@page_num) @bulletins = Bulletin.all.available_for_lang(I18n.locale).can_display.desc( :is_top, :postdate).page( params[:page_main]).per(@page_num)
end end
delayed_impressionist(@tag) if @tag delayed_impressionist(@tag) if @tag
end end

View File

@ -32,7 +32,7 @@ module Announcement
widgets do widgets do
default_widget do default_widget do
query 'Bulletin.any_of( {deadline: nil,:postdate.lte => Time.now} , {:deadline.gte => Time.now,:postdate.lte => Time.now} )' query 'Bulletin'
enable ["typeA", "typeB_style3", "typeC"] enable ["typeA", "typeB_style3", "typeC"]
image :image image :image
field :postdate field :postdate