archive module: sort for backend/frontend. move get_sorted_and_filtered from orbit_backend_controller.rb to application_controller.rb

This commit is contained in:
iCross 2013-06-04 10:53:31 +08:00
parent dd206437d8
commit c63ac751ea
15 changed files with 352 additions and 286 deletions

View File

@ -229,6 +229,174 @@ class ApplicationController < ActionController::Base
redirect_to ret redirect_to ret
end end
def get_sorted_and_filtered(object_class, query = nil, objects = nil)
objects ||= get_objects(object_class, query)
object_class = object_class.classify.constantize
if !params[:sort].blank?
options = params[:sort_options]
options = [options] if !options.class.eql?(Array)
options.each do |option|
if object_class.fields.include?(option)
if object_class.fields[option].type.to_s.eql?('Object') && !object_class.relations[option].nil?
objects = get_objects_from_referenced_objects(object_class.fields[option].options[:class_name].constantize, objects, option)
else
(objects = objects.order_by(option, params[:direction])) rescue nil
end
elsif object_class.relations.include?(option)
case object_class.relations[option].macro
when :references_one
a = Array.new
objects.each { |object| a << [get_string_value_from_object(object), object] }
sorted = params[:direction].eql?('asc') ? a.sort : a.sort.reverse!
objects = sorted.collect {|x| x[1] }
when :references_many, :references_and_referenced_in_many
objects = get_objects_from_self(object_class, objects, option)
when :referenced_in
objects = get_objects_from_referenced_objects(object_class.relations[option].class_name.constantize, objects, "#{option}_id")
end
elsif option.eql?('tags')
tag_array = @module_app.tags.inject([]){ |result, value|
result << [value.name, value]
}
params[:direction].eql?('asc') ? tag_array.sort : tag_array.sort.reverse!
sorted_objects = Array.new
tag_array.each do |x|
taggings = x[1].taggings
taggings.each {|tagging| sorted_objects << tagging.taggable }
end
# debugger
sorted_objects.flatten!
sorted_objects.uniq!
objects = get_with_nil(objects, option, sorted_objects)
end
end
end
if @filter
@filter.each do |key, value|
case key
when 'status'
a = Array.new
objects.each do |object|
value.each do |v|
a << object if object[v]
end
end
objects = a.uniq
when 'categories'
a = Array.new
objects.each do |object|
a << object if (value.include?(object.send("#{object.class.to_s.underscore}_category").id.to_s) rescue nil)
end
objects = a.uniq
when 'tags'
a = Array.new
objects.each do |object|
object.tags.each do |tag|
a << object if value.include?(tag.id.to_s)
end
end
objects = a.uniq
end if value.size > 0
end
end
Kaminari.paginate_array(filter_authorized_objects(objects)).page(params[:page]).per(10)
end
def get_viewable(object_class, query=nil)
objects = get_objects(object_class,query).order_by(:created_at, :desc)
Kaminari.paginate_array(objects).page(params[:page]).per(10)
end
def get_objects(object_class, query=nil)
object_class = object_class.classify.constantize
# debugger
# a=1
if query
if object_class.include?(Mongoid::Sortable)
objects = object_class.default_sort(params[:sort]).where(query)
else
objects = object_class.where(query)
end
else
if object_class.include?(Mongoid::Sortable)
objects = object_class.default_sort(params[:sort]).all
else
objects = object_class.all
end
end
objects
end
def get_objects_from_referenced_objects(object_class, objects, option)
referer_ids = objects.distinct(option)
referenced_objects = object_class.find(referer_ids) rescue nil
if referenced_objects
a = Array.new
referenced_objects.to_a.each { |referer| a << [get_string_value_from_object(referer), referer.id] }
sorted = params[:direction].eql?('asc') ? a.sort : a.sort.reverse!
sorted_objects = sorted.collect {|x| objects.where(option => x[1]).entries }
sorted_objects.flatten!
sorted_objects.uniq!
get_with_nil(objects, option, sorted_objects)
else
objects
end
end
def get_objects_from_self(object_class, objects, option)
referenced_class = object_class.relations[option].class_name.constantize
referenced_objects = referenced_class.all rescue nil
if referenced_objects
reverse_relation = nil
referenced_class.relations.each { |relation| reverse_relation = relation[1].name.to_s if relation[1].class_name.eql?(object_class.to_s) }
a = Array.new
referenced_objects.each { |referenced_object| a << [get_string_value_from_object(referenced_object), referenced_object] }
a.compact!
sorted = params[:direction].eql?('asc') ? a.sort : a.sort.reverse!
sorted_objects = Array.new
sorted.each {|x| sorted_objects << x[1].send(reverse_relation) }
sorted_objects.flatten!
sorted_objects.uniq!
get_with_nil(objects, option, sorted_objects)
else
objects
end
end
def get_string_value_from_object(object)
s = object.name_translations[I18n.locale.to_s] unless s rescue nil
s = object.title_translations[I18n.locale.to_s] unless s rescue nil
s = object.name unless s rescue nil
s = object.title unless s rescue nil
s.downcase rescue ''
end
def get_with_nil(objects, option, sorted_objects)
tmp = Array.new
objects.each { |object| tmp << [get_string_value_from_object(object), object] if (object.send(option).blank? || (object.send(option).size == 0 rescue nil)) }
sorted = params[:direction].eql?('asc') ? tmp.sort : tmp.sort.reverse!
sorted_tmp = sorted.collect {|a| a[1] }
a = params[:direction].eql?('asc') ? (sorted_tmp + sorted_objects) : (sorted_objects + sorted_tmp)
a.flatten
end
def filter_authorized_objects(objects)
if(!is_admin? || !is_manager?)
objects.delete_if{ |object|
if object.is_pending == true
if check_permission(:manager)
object.create_user_id != current_user.id
else
!object.send("#{object.class.to_s.underscore}_category").authed_users('fact_check').include?(current_user) rescue false
end
else
false
end
}
end
objects
end
private private

View File

@ -35,166 +35,4 @@ class OrbitBackendController < ApplicationController
redirect_to root_url redirect_to root_url
end end
end end
def get_sorted_and_filtered(object_class, query=nil)
objects = get_objects(object_class, query)
object_class = object_class.classify.constantize
if !params[:sort].blank?
options = params[:sort_options]
options = [options] if !options.class.eql?(Array)
options.each do |option|
if object_class.fields.include?(option)
if object_class.fields[option].type.to_s.eql?('Object') && !object_class.relations[option].nil?
objects = get_objects_from_referenced_objects(object_class.fields[option].options[:class_name].constantize, objects, option)
else
(objects = objects.order_by(option, params[:direction])) rescue nil
end
elsif object_class.relations.include?(option)
case object_class.relations[option].macro
when :references_one
a = Array.new
objects.each { |object| a << [get_string_value_from_object(object), object] }
sorted = params[:direction].eql?('asc') ? a.sort : a.sort.reverse!
objects = sorted.collect {|x| x[1] }
when :references_many, :references_and_referenced_in_many
objects = get_objects_from_self(object_class, objects, option)
when :referenced_in
objects = get_objects_from_referenced_objects(object_class.relations[option].class_name.constantize, objects, "#{option}_id")
end
elsif option.eql?('tags')
tag_array = @module_app.tags.inject([]){ |result, value|
result << [value.name, value]
}
params[:direction].eql?('asc') ? tag_array.sort : tag_array.sort.reverse!
sorted_objects = Array.new
tag_array.each do |x|
taggings = x[1].taggings
taggings.each {|tagging| sorted_objects << tagging.taggable }
end
# debugger
sorted_objects.flatten!
sorted_objects.uniq!
objects = get_with_nil(objects, option, sorted_objects)
end
end
end
if @filter
@filter.each do |key, value|
case key
when 'status'
a = Array.new
objects.each do |object|
value.each do |v|
a << object if object[v]
end
end
objects = a.uniq
when 'categories'
a = Array.new
objects.each do |object|
a << object if (value.include?(object.send("#{object.class.to_s.underscore}_category").id.to_s) rescue nil)
end
objects = a.uniq
when 'tags'
a = Array.new
objects.each do |object|
object.tags.each do |tag|
a << object if value.include?(tag.id.to_s)
end
end
objects = a.uniq
end if value.size > 0
end
end
Kaminari.paginate_array(filter_authorized_objects(objects)).page(params[:page]).per(10)
end
def get_string_value_from_object(object)
s = object.name_translations[I18n.locale.to_s] unless s rescue nil
s = object.title_translations[I18n.locale.to_s] unless s rescue nil
s = object.name unless s rescue nil
s = object.title unless s rescue nil
s.downcase rescue ''
end
def get_objects_from_referenced_objects(object_class, objects, option)
referer_ids = objects.distinct(option)
referenced_objects = object_class.find(referer_ids) rescue nil
if referenced_objects
a = Array.new
referenced_objects.to_a.each { |referer| a << [get_string_value_from_object(referer), referer.id] }
sorted = params[:direction].eql?('asc') ? a.sort : a.sort.reverse!
sorted_objects = sorted.collect {|x| objects.where(option => x[1]).entries }
sorted_objects.flatten!
sorted_objects.uniq!
get_with_nil(objects, option, sorted_objects)
else
objects
end
end
def get_objects_from_self(object_class, objects, option)
referenced_class = object_class.relations[option].class_name.constantize
referenced_objects = referenced_class.all rescue nil
if referenced_objects
reverse_relation = nil
referenced_class.relations.each { |relation| reverse_relation = relation[1].name.to_s if relation[1].class_name.eql?(object_class.to_s) }
a = Array.new
referenced_objects.each { |referenced_object| a << [get_string_value_from_object(referenced_object), referenced_object] }
a.compact!
sorted = params[:direction].eql?('asc') ? a.sort : a.sort.reverse!
sorted_objects = Array.new
sorted.each {|x| sorted_objects << x[1].send(reverse_relation) }
sorted_objects.flatten!
sorted_objects.uniq!
get_with_nil(objects, option, sorted_objects)
else
objects
end
end
def get_with_nil(objects, option, sorted_objects)
tmp = Array.new
objects.each { |object| tmp << [get_string_value_from_object(object), object] if (object.send(option).blank? || (object.send(option).size == 0 rescue nil)) }
sorted = params[:direction].eql?('asc') ? tmp.sort : tmp.sort.reverse!
sorted_tmp = sorted.collect {|a| a[1] }
a = params[:direction].eql?('asc') ? (sorted_tmp + sorted_objects) : (sorted_objects + sorted_tmp)
a.flatten
end
def get_viewable(object_class, query=nil)
objects = get_objects(object_class,query).order_by(:created_at, :desc)
Kaminari.paginate_array(objects).page(params[:page]).per(10)
end
def get_objects(object_class, query=nil)
object_class = object_class.classify.constantize
# debugger
# a=1
if query
objects = object_class.where(query)
else
objects = object_class.all
end
objects
end
def filter_authorized_objects(objects)
if(!is_admin? || !is_manager?)
objects.delete_if{ |object|
if object.is_pending == true
if check_permission(:manager)
object.create_user_id != current_user.id
else
!object.send("#{object.class.to_s.underscore}_category").authed_users('fact_check').include?(current_user) rescue false
end
else
false
end
}
end
objects
end
end end

View File

@ -303,4 +303,12 @@ module ApplicationHelper
"http://#{request.host}:2#{site_number}00" "http://#{request.host}:2#{site_number}00"
end end
def sortable(column, title = nil, options = {})
options[:remote] ||= true
title ||= column.titleize
css_class = column.to_s == params[:sort] ? "current #{params[:direction]}" : 'asc'
direction = column.to_s == params[:sort] && params[:direction] == 'asc' ? 'desc' : 'asc'
link_to title, params.merge({direction: direction, sort: column, sort_options: column}), {:class => "#{css_class} sortable", :remote => options[:remote]}
end
end end

View File

@ -0,0 +1,34 @@
module Mongoid
module Sortable
extend ActiveSupport::Concern
included do
field :sort_number, type: Integer
after_initialize :set_sort_number
end
module ClassMethods
def default_sort(sorted = true)
if !sorted
asc(:sort_number)
else
asc
end
end
end
private
def set_sort_number
if self.sort_number.nil?
last_record = self.class.desc(:sort_number).first
if last_record
self.sort_number = (last_record.sort_number + 10) / 10 * 10
else
self.sort_number = 10
end
end
end
end
end

View File

@ -503,3 +503,4 @@ zh_tw:
visitors_this_year: 今年造訪人次 visitors_this_year: 今年造訪人次
visitors_today: 今日造訪人次 visitors_today: 今日造訪人次
yes_: "是" yes_: "是"
sort_number: 排序數

View File

@ -1,4 +1,5 @@
class Panel::Archive::Widget::ArchiveFilesController < OrbitWidgetController class Panel::Archive::Widget::ArchiveFilesController < OrbitWidgetController
include AdminHelper
def initialize def initialize
super super
@ -28,16 +29,20 @@ class Panel::Archive::Widget::ArchiveFilesController < OrbitWidgetController
date_now = Time.now date_now = Time.now
if !params[:category_id].blank? if !params[:category_id].blank?
@archive_files = ArchiveFile.all.can_display.any_in(:archive_file_category_id => params[:category_id]).merge(ArchiveFileCategory.excludes('disable' => true)).desc( :is_top, :created_at ).page( params[:page_main] ).per(@page_num) @archive_files = ArchiveFile.default_sort(params[:sort]).all.can_display.any_in(:archive_file_category_id => params[:category_id]).merge(ArchiveFileCategory.excludes('disable' => true)).desc( :is_top ).page( params[:page_main] ).per(@page_num)
@archive_file_categorys = ArchiveFileCategory.any_in(:_id => params[:category_id]).excludes('disable' => true) @archive_file_categorys = ArchiveFileCategory.any_in(:_id => params[:category_id]).excludes('disable' => true)
elsif !params[:tag_id].blank? elsif !params[:tag_id].blank?
@archive_files = ArchiveFile.all.can_display.any_in(:tagged_ids => params[:tag_id]).merge(ArchiveFileCategory.excludes('disable' => true)).desc( :is_top, :created_at ).page( params[:page_main] ).per(@page_num) @archive_files = ArchiveFile.default_sort(params[:sort]).all.can_display.any_in(:tagged_ids => params[:tag_id]).merge(ArchiveFileCategory.excludes('disable' => true)).desc( :is_top ).page( params[:page_main] ).per(@page_num)
get_categorys get_categorys
else else
@archive_files = ArchiveFile.all.can_display.merge(ArchiveFileCategory.excludes('disable' => true)).desc( :is_top, :created_at ).page( params[:page_main] ).per(@page_num) @archive_files = ArchiveFile.default_sort(params[:sort]).all.can_display.merge(ArchiveFileCategory.excludes('disable' => true)).desc( :is_top).page( params[:page_main] ).per(@page_num)
get_categorys get_categorys
end end
if params[:sort]
@archive_files = get_sorted_and_filtered('archive_file', nil, @archive_files)
end
end end
def get_categorys def get_categorys

View File

@ -4,6 +4,7 @@ class ArchiveFile
include Mongoid::Document include Mongoid::Document
include Mongoid::Timestamps include Mongoid::Timestamps
include Mongoid::MultiParameterAttributes include Mongoid::MultiParameterAttributes
include Mongoid::Sortable
BelongsToCategory = :archive_file_category BelongsToCategory = :archive_file_category
include OrbitCoreLib::BelongsToCategoryMayDisable include OrbitCoreLib::BelongsToCategoryMayDisable

View File

@ -4,6 +4,9 @@
<%= check_box_tag 'to_delete[]', archive_file.id, false, :class => "checkbox_in_list" %> <%= check_box_tag 'to_delete[]', archive_file.id, false, :class => "checkbox_in_list" %>
<% end -%> <% end -%>
</td> </td>
<td>
<%= archive_file.sort_number %>
</td>
<td> <td>
<div class="label-group"> <div class="label-group">
<div class="label-td"> <div class="label-td">

View File

@ -45,6 +45,9 @@
<%= f.label :category %> <%= f.label :category %>
<%= f.select :archive_file_category_id, @archive_file_categorys.collect {|t| [ t.title, t.id ]} %> <%= f.select :archive_file_category_id, @archive_file_categorys.collect {|t| [ t.title, t.id ]} %>
<%= f.label :sort_number %>
<%= f.text_field :sort_number %>
<ul class="nav nav-tabs"> <ul class="nav nav-tabs">
<% @site_valid_locales.each_with_index do |locale, i| %> <% @site_valid_locales.each_with_index do |locale, i| %>
<%# site_valid_locales_default_head.each_with_index do |locale, i| %> <%# site_valid_locales_default_head.each_with_index do |locale, i| %>

View File

@ -1,4 +1,5 @@
<%= render_sort_bar(true, delete_panel_archive_back_end_archive_files_path(:direction => params[:direction], :sort => params[:sort], :sort_options => params[:sort_options]), <%= render_sort_bar(true, delete_panel_archive_back_end_archive_files_path(:direction => params[:direction], :sort => params[:sort], :sort_options => params[:sort_options]),
['sort_number', 'sort_number', 'span1', :sort_number],
['status', ['is_top', 'is_hot', 'is_hidden'], 'span1', :status], ['status', ['is_top', 'is_hot', 'is_hidden'], 'span1', :status],
['category', 'archive_file_category', 'span2', :category], ['category', 'archive_file_category', 'span2', :category],
['title', 'title','span3', :title], ['title', 'title','span3', :title],

View File

@ -3,6 +3,7 @@
<table class="table main-list"> <table class="table main-list">
<thead> <thead>
<tr> <tr>
<th class="span1"></th>
<th class="span1"></th> <th class="span1"></th>
<th class="span1"></th> <th class="span1"></th>
<th class="span2"></th> <th class="span2"></th>

View File

@ -8,13 +8,12 @@
<table> <table>
<thead> <thead>
<tr> <tr>
<th class="column-ctrl col-title"><%= t("archive.Title")%></th> <th class="column-ctrl col-title"><%= sortable(:title, t("archive.Title"))%></th>
<th class="column-ctrl col-file"><%= t("archive.Files")%></th> <th class="column-ctrl col-file"><%= t("archive.Files")%></th>
<th class="column-ctrl col-category"><%= t("archive.Category")%></th> <th class="column-ctrl col-category"><%= sortable(:archive_file_category, t("archive.Category"))%></th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<% @archive_files.each_with_index do |post,i| %> <% @archive_files.each_with_index do |post,i| %>
<tr <% if ( (i + 1) % 2 ) == 0 %> class="even" <% end %>> <tr <% if ( (i + 1) % 2 ) == 0 %> class="even" <% end %>>
<td><div class="o-archives-title"><%= post.title %></div></td> <td><div class="o-archives-title"><%= post.title %></div></td>

View File

@ -1 +1 @@
$('#bulletin_widget').html("<%= j render 'index' %>") $('#archive_file_widget').html("<%= j render 'index' %>")

View File

@ -9,3 +9,7 @@ zh_tw:
archive: 檔案室前台 archive: 檔案室前台
widget: widget:
index: 檔案室Widget index: 檔案室Widget
mongoid:
attributes:
archive_file:
sort_number: 排序數