added sorting DSL
This commit is contained in:
parent
1ccfd6e48c
commit
e7aa7ea1fa
|
@ -11,23 +11,26 @@ class DefaultWidgetController< OrbitWidgetController
|
||||||
end
|
end
|
||||||
|
|
||||||
def query_for_default_widget
|
def query_for_default_widget
|
||||||
|
|
||||||
@ori_class_name = @default_widget["query"].split('.')[0]
|
@ori_class_name = @default_widget["query"].split('.')[0]
|
||||||
@ori_query = @default_widget["query"]
|
@ori_query = @default_widget["query"]
|
||||||
result = nil
|
result = nil
|
||||||
result_objects = nil
|
result_objects = nil
|
||||||
if !params["tag_id"].blank?
|
if !params["tag_id"].blank?
|
||||||
if params["category_id"].blank? #has tag no cate
|
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
|
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
|
end
|
||||||
elsif params["category_id"].blank? #no tag no cate
|
elsif params["category_id"].blank? #no tag no cate
|
||||||
result = eval(@ori_query)
|
result = eval(@ori_query)
|
||||||
else #no tag has cate
|
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
|
end
|
||||||
result.available_for_lang(I18n.locale).can_display.desc(:created_at)
|
eval("result.#{@default_widget[:sorting_query]}")
|
||||||
end
|
end
|
||||||
|
|
||||||
def search_result
|
def search_result
|
||||||
|
@ -95,9 +98,9 @@ class DefaultWidgetController< OrbitWidgetController
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_category_field_name
|
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|
|
@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
|
end
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
|
@ -102,12 +102,17 @@ module OrbitApp
|
||||||
def initialize(&block)
|
def initialize(&block)
|
||||||
@query = nil
|
@query = nil
|
||||||
@image = nil
|
@image = nil
|
||||||
|
@sorting_query = nil
|
||||||
@more_link = {}
|
@more_link = {}
|
||||||
@fields = []
|
@fields = []
|
||||||
@enabled_styles = STYLE
|
@enabled_styles = STYLE
|
||||||
block.arity < 1 ? instance_eval(&block) : block.call(self) if block_given?
|
block.arity < 1 ? instance_eval(&block) : block.call(self) if block_given?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def sorting(query)
|
||||||
|
@sorting_query = query
|
||||||
|
end
|
||||||
|
|
||||||
def enable(args)
|
def enable(args)
|
||||||
@enabled_styles = args
|
@enabled_styles = args
|
||||||
end
|
end
|
||||||
|
@ -122,7 +127,7 @@ module OrbitApp
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_module_app_format
|
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
|
end
|
||||||
|
|
||||||
def link_field(field_name,setting)
|
def link_field(field_name,setting)
|
||||||
|
|
|
@ -29,6 +29,7 @@ module Announcement
|
||||||
|
|
||||||
widgets do
|
widgets do
|
||||||
default_widget 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} )'
|
query 'Bulletin.any_of( {deadline: nil,:postdate.lte => Time.now} , {:deadline.gte => Time.now,:postdate.lte => Time.now} )'
|
||||||
enable ["typeA","typeB_style3","typeC"]
|
enable ["typeA","typeB_style3","typeC"]
|
||||||
image :image
|
image :image
|
||||||
|
|
Loading…
Reference in New Issue