added sorting DSL

This commit is contained in:
Matt K. Fu 2013-09-02 16:33:50 +08:00
parent 1ccfd6e48c
commit e7aa7ea1fa
3 changed files with 17 additions and 8 deletions

View File

@ -11,23 +11,26 @@ class DefaultWidgetController< OrbitWidgetController
end
def query_for_default_widget
@ori_class_name = @default_widget["query"].split('.')[0]
@ori_query = @default_widget["query"]
result = nil
result_objects = nil
if !params["tag_id"].blank?
if params["category_id"].blank? #has tag no cate
result = eval("#{@ori_query}.where(:tagged_ids.in => params['tag_id'])")
result = eval(@ori_query)
result.selector[:tagged_ids] = { "$in" => params['tag_id'].collect{|t| BSON::ObjectId(t)}}
else #has tag and cate
result = eval("#{@ori_query}.where(:#{get_category_field_name}.in=>params['category_id'], :tagged_ids.in => params['tag_id'])")
result = eval(@ori_query)
result.selector[get_category_field_name] = { "$in" => params['category_id'].collect{|t| BSON::ObjectId(t)}}
result.selector[:tagged_ids] = { "$in" => params['tag_id'].collect{|t| BSON::ObjectId(t)}}
end
elsif params["category_id"].blank? #no tag no cate
result = eval(@ori_query)
else #no tag has cate
result = eval("#{@ori_query}.where(:#{get_category_field_name}.in=>params['category_id'])")
result = eval(@ori_query)
result.selector[get_category_field_name] = { "$in" => params['category_id'].collect{|t| BSON::ObjectId(t)}}
end
result.available_for_lang(I18n.locale).can_display.desc(:created_at)
eval("result.#{@default_widget[:sorting_query]}")
end
def search_result
@ -95,9 +98,9 @@ class DefaultWidgetController< OrbitWidgetController
end
def get_category_field_name
@ori_class_name = @ori_class_name.constantize
@ori_class_name = @ori_class_name.constantize unless @ori_class_name.is_a? Class
@ori_class_name.fields.each_key do |key|
return key if key.include?('category_id')
return key.to_s if key.include?('category_id')
end
nil
end

View File

@ -102,12 +102,17 @@ module OrbitApp
def initialize(&block)
@query = nil
@image = nil
@sorting_query = nil
@more_link = {}
@fields = []
@enabled_styles = STYLE
block.arity < 1 ? instance_eval(&block) : block.call(self) if block_given?
end
def sorting(query)
@sorting_query = query
end
def enable(args)
@enabled_styles = args
end
@ -122,7 +127,7 @@ module OrbitApp
end
def to_module_app_format
{"query"=>@query,"image"=>@image,"more_link"=>@more_link,"enabled_styles"=>@enabled_styles} rescue nil
{"query"=>@query,"image"=>@image,"more_link"=>@more_link,"enabled_styles"=>@enabled_styles,:sorting_query=>@sorting_query} rescue nil
end
def link_field(field_name,setting)

View File

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