DRY code for sorting
This commit is contained in:
1 parent
fb6c2a24df
commit
32faef0c70
16 files changed
+228
-89
No files matched your search
@@ -11,17 +11,9 @@ $(document).on('click', '.action a.remove_existing_record', function(){
|
||||
$("tr #" + $(this).prev().attr('value')).hide();
|
||||
});
|
||||
|
||||
$('.quick_edit_cancel').live('click', function(){
|
||||
$(document).on('click', '.quick_edit_cancel', function(){
|
||||
tr = $(this).attr('rel');
|
||||
$('#' + tr).hide();
|
||||
$("tr#bulletin_file_" + $(this).prev().attr('value')).hide();
|
||||
$("tr#bulletin_link_" + $(this).prev().attr('value')).hide();
|
||||
});
|
||||
|
||||
$(document).on('click', '.list-remove', function(){
|
||||
$('#delete_bulletins').submit();
|
||||
});
|
||||
|
||||
$(document).on('click', '#check_all_bulletins', function(){
|
||||
$('.checkbox_in_list').attr("checked", this.checked);
|
||||
});
|
||||
@@ -0,0 +1,7 @@
|
||||
$(document).on('click', '.list-remove', function(){
|
||||
$('#delete_all').submit();
|
||||
});
|
||||
|
||||
$(document).on('click', '#check_all', function(){
|
||||
$('.checkbox_in_list').attr("checked", this.checked);
|
||||
});
|
||||
@@ -12,7 +12,7 @@ class OrbitBackendController< ApplicationController
|
||||
@app_title = request.fullpath.split('/')[2]
|
||||
@module_app = ModuleApp.first(conditions: {:key => @app_title} )
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
def force_order
|
||||
@@ -26,5 +26,135 @@ class OrbitBackendController< ApplicationController
|
||||
render :text => '403 Forbidden'
|
||||
end
|
||||
end
|
||||
|
||||
def get_sorted_and_filtered(object_class)
|
||||
object_class = object_class.classify.constantize
|
||||
objects = object_class.all
|
||||
|
||||
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)
|
||||
case object_class.fields[option].type.to_s
|
||||
when 'BigDecimal', 'Boolean', 'Date', 'DateTime', 'Float', 'Integer', 'String', 'Symbol', 'Time'
|
||||
(objects = objects.order_by(option, params[:direction])) rescue nil
|
||||
when 'Object'
|
||||
objects = get_objects_from_referenced_objects(object_class.fields[option].options[:class_name].constantize, objects, option)
|
||||
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
|
||||
end
|
||||
end
|
||||
end
|
||||
if @filter
|
||||
@filter.each do |key, value|
|
||||
case key
|
||||
when 'status'
|
||||
a = Array.new
|
||||
objects.each do |bulletin|
|
||||
value.each do |v|
|
||||
case v
|
||||
when 'pending'
|
||||
a << bulletin if bulletin.is_checked.nil?
|
||||
when 'rejected'
|
||||
a << bulletin if bulletin.is_checked.eql?(false)
|
||||
else
|
||||
a << bulletin if bulletin[v]
|
||||
end
|
||||
end
|
||||
end
|
||||
objects = a.uniq
|
||||
when 'categories'
|
||||
a = Array.new
|
||||
objects.each do |bulletin|
|
||||
a << bulletin if value.include?(bulletin.bulletin_category.id.to_s)
|
||||
end
|
||||
objects = a.uniq
|
||||
when 'tags'
|
||||
a = Array.new
|
||||
objects.each do |bulletin|
|
||||
bulletin.tags.each do |tag|
|
||||
a << bulletin if value.include?(tag.id.to_s)
|
||||
end
|
||||
end
|
||||
objects = a.uniq
|
||||
end if value.size > 0
|
||||
end
|
||||
end
|
||||
Kaminari.paginate_array(objects).page(params[:page]).per(10)
|
||||
end
|
||||
|
||||
def get_string_value_from_object(object)
|
||||
s = object[I18n.locale] rescue nil
|
||||
s = object.i18n_variable unless s rescue nil
|
||||
s = object.name unless s rescue nil
|
||||
s = object.title unless s rescue nil
|
||||
if s
|
||||
case s.class.to_s
|
||||
when "String"
|
||||
s.downcase
|
||||
when "I18nVariable"
|
||||
s[I18n.locale].downcase
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
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.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).count == 0) }
|
||||
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
|
||||
|
||||
end
|
||||
@@ -87,26 +87,6 @@ module ApplicationHelper
|
||||
((controller.controller_name.eql?(controller_name) || request.fullpath.eql?(controller_name)) && controller.action_name.eql?(action_name)) ? 'active' : nil
|
||||
end
|
||||
|
||||
def sortable(column)
|
||||
direction = (column == params[:sort] && params[:direction] == "asc") ? "desc" : "asc"
|
||||
{:sort => column, :direction => direction}
|
||||
end
|
||||
|
||||
def is_sort_active?(name)
|
||||
res = ''
|
||||
res << ' select' if params[:sort].eql?(name)
|
||||
res << ' active' if params[:sort].eql?(name) && params[:direction].eql?('asc')
|
||||
res
|
||||
end
|
||||
|
||||
def is_sort?(name)
|
||||
' web-symbol' if params[:sort].eql?(name)
|
||||
end
|
||||
|
||||
def is_filter_active?(type, id)
|
||||
' active' if (@filter[type].include?(id.to_s) rescue nil)
|
||||
end
|
||||
|
||||
def process_page(page, id)
|
||||
parse_page_noko(page, id)
|
||||
end
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
module OrbitBackendHelper
|
||||
|
||||
def sortable(column)
|
||||
direction = (column == params[:sort] && params[:direction] == "asc") ? "desc" : "asc"
|
||||
{:sort => column, :direction => direction}
|
||||
end
|
||||
|
||||
def is_sort_active?(name)
|
||||
res = ''
|
||||
res << ' select' if params[:sort].eql?(name)
|
||||
res << ' active' if params[:sort].eql?(name) && params[:direction].eql?('asc')
|
||||
res
|
||||
end
|
||||
|
||||
def is_sort?(name)
|
||||
' web-symbol' if params[:sort].eql?(name)
|
||||
end
|
||||
|
||||
def is_filter_active?(type, id)
|
||||
' active' if (@filter[type].include?(id.to_s) rescue nil)
|
||||
end
|
||||
|
||||
def render_sort_bar(url, delete_all, *titles)
|
||||
content_tag :table, :class => "table main-list" do
|
||||
content_tag :thead do
|
||||
content_tag :tr, :class => "sort-header" do
|
||||
concat (content_tag :th, :class => "span1 strong" do
|
||||
check_box_tag :check_all
|
||||
link_to content_tag(:i, nil, :class => "icon-trash"), '#', :class => "list-remove"
|
||||
end) if delete_all
|
||||
titles.each do |title|
|
||||
concat render_title(url, title[0], title[1], title[2], title[3])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def render_title(url, title, fields, span, translation)
|
||||
content_tag :th, :class => "sort #{span} #{is_sort_active?(title)}" do
|
||||
link_to (t(translation) + content_tag(:b, nil, :class => is_sort?(title))).html_safe, url_for({:filter => @filter}.merge(sortable(title).merge(:sort_options => fields))), :class => 'js_history'
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
Reference in new issue
Block a user