DRY code for sorting

This commit is contained in:
Christophe Vilayphiou 2012-04-29 23:39:28 +08:00
parent fb6c2a24df
commit 32faef0c70
16 changed files with 228 additions and 89 deletions

View File

@ -11,17 +11,9 @@ $(document).on('click', '.action a.remove_existing_record', function(){
$("tr #" + $(this).prev().attr('value')).hide(); $("tr #" + $(this).prev().attr('value')).hide();
}); });
$('.quick_edit_cancel').live('click', function(){ $(document).on('click', '.quick_edit_cancel', function(){
tr = $(this).attr('rel'); tr = $(this).attr('rel');
$('#' + tr).hide(); $('#' + tr).hide();
$("tr#bulletin_file_" + $(this).prev().attr('value')).hide(); $("tr#bulletin_file_" + $(this).prev().attr('value')).hide();
$("tr#bulletin_link_" + $(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);
}); });

View File

@ -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);
});

View File

@ -12,7 +12,7 @@ class OrbitBackendController< ApplicationController
@app_title = request.fullpath.split('/')[2] @app_title = request.fullpath.split('/')[2]
@module_app = ModuleApp.first(conditions: {:key => @app_title} ) @module_app = ModuleApp.first(conditions: {:key => @app_title} )
end end
private private
def force_order def force_order
@ -26,5 +26,135 @@ class OrbitBackendController< ApplicationController
render :text => '403 Forbidden' render :text => '403 Forbidden'
end end
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 end

View File

@ -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 ((controller.controller_name.eql?(controller_name) || request.fullpath.eql?(controller_name)) && controller.action_name.eql?(action_name)) ? 'active' : nil
end 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) def process_page(page, id)
parse_page_noko(page, id) parse_page_noko(page, id)
end end

View File

@ -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

View File

@ -30,7 +30,10 @@ class Panel::Announcement::BackEnd::BulletinsController < OrbitBackendController
# @bulletins = Bulletin.search(params[:search], params[:category_id]) # @bulletins = Bulletin.search(params[:search], params[:category_id])
# @bulletins = Bulletin.all.order_by([params[:sort], params[:direction]]) # @bulletins = Bulletin.all.order_by([params[:sort], params[:direction]])
@bulletins = (params[:sort] || @filter) ? get_sorted_and_filtered_bulletins : Bulletin.all.page(params[:page]).per(10)
# @bulletins = (params[:sort] || @filter) ? get_sorted_and_filtered_bulletins : Bulletin.all.page(params[:page]).per(10)
@bulletins = (params[:sort] || @filter) ? get_sorted_and_filtered("bulletin") : Bulletin.all.page(params[:page]).per(10)
@bulletin_categories = BulletinCategory.all @bulletin_categories = BulletinCategory.all
@bulletin_link = BulletinLink.new @bulletin_link = BulletinLink.new
@ -300,7 +303,7 @@ class Panel::Announcement::BackEnd::BulletinsController < OrbitBackendController
if params[:to_delete] if params[:to_delete]
bulletins = Bulletin.any_in(:_id => params[:to_delete]).delete_all bulletins = Bulletin.any_in(:_id => params[:to_delete]).delete_all
end end
redirect_to panel_announcement_back_end_bulletins_url(:filter => params[:filter], :direction => params[:direction], :sort => params[:sort]) redirect_to panel_announcement_back_end_bulletins_url(:filter => params[:filter], :direction => params[:direction], :sort => params[:sort], :sort_options => params[:sort_options])
end end

View File

@ -14,7 +14,7 @@ class Bulletin
field :deadline , :type => Date field :deadline , :type => Date
# field :url # field :url
field :create_user_id field :create_user_id
field :update_user_id field :update_user_id, :class_name => "User"
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

View File

@ -0,0 +1,11 @@
class AnnouncementTag < Tag
has_and_belongs_to_many :bulletins
def get_visible_bulletins(sort = :name)
date_now = Time.now
self.bulletins.where(:is_hidden => false).any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc(:is_top, sort)
end
end

View File

@ -30,4 +30,8 @@
<%= render 'sort_headers' %> <%= render 'sort_headers' %>
</div> </div>
</div> </div>
</div> </div>
<% content_for :page_specific_javascript do %>
<%= javascript_include_tag "sort_header" %>
<% end %>

View File

@ -292,7 +292,6 @@
<% content_for :page_specific_javascript do %> <% content_for :page_specific_javascript do %>
<%= javascript_include_tag "bulletin_form" %>
<%= javascript_include_tag "inc/jquery.imagesloaded.js" %> <%= javascript_include_tag "inc/jquery.imagesloaded.js" %>
<script> <script>
$('#add_bulletin_link a.add').live('click', function(){ $('#add_bulletin_link a.add').live('click', function(){

View File

@ -1,31 +1,8 @@
<table class="table main-list"> <%= render_sort_bar(panel_announcement_back_end_bulletins_path, true,
<thead> ['status', ['is_top', 'is_hot', 'is_hidden', 'is_pending', 'is_checked', 'is_rejected'], 'span1-2', 'bulletin.status'],
<tr class="sort-header"> ['category', 'bulletin_category', 'span1-2', 'bulletin.category'],
<th class="span1 strong"> ['title', 'title','span7', 'bulletin.title'],
<input id="check_all_bulletins" type="checkbox"> ['start_date', 'postdate', 'span1-2', 'bulletin.start_date'],
<a href='#' class="list-remove"><i class="icon-trash"></i></a> ['end_date', 'deadline', 'span1-2', 'bulletin.end_date'],
</th> ['tags', 'tags', 'span1-2', 'bulletin.tags'],
<th class="sort span1-2 <%= is_sort_active?('status') %>"> ['last_modified', 'update_user_id','span1-3', 'bulletin.last_modified']).html_safe %>
<%= link_to (t('bulletin.status') + content_tag(:b, nil, :class => is_sort?('status'))).html_safe, panel_announcement_back_end_bulletins_path({:filter => @filter}.merge(sortable('status'))), :class => 'js_history' %>
</th>
<th class="sort span1-2 <%= is_sort_active?('category') %>">
<%= link_to (t('bulletin.category') + content_tag(:b, nil, :class => is_sort?('category'))).html_safe, panel_announcement_back_end_bulletins_path({:filter => @filter}.merge(sortable('category'))), :class => 'js_history' %>
</th>
<th class="sort span7 <%= is_sort_active?('title') %>">
<%= link_to (t('bulletin.title') + content_tag(:b, nil, :class => is_sort?('title'))).html_safe, panel_announcement_back_end_bulletins_path({:filter => @filter}.merge(sortable('title'))), :class => 'js_history' %>
</th>
<th class="sort span1-2 <%= is_sort_active?('postdate') %>">
<%= link_to (t('bulletin.start_date') + content_tag(:b, nil, :class => is_sort?('postdate'))).html_safe, panel_announcement_back_end_bulletins_path({:filter => @filter}.merge(sortable('postdate'))), :class => 'js_history' %>
</th>
<th class="sort span1-2 <%= is_sort_active?('deadline') %>">
<%= link_to (t('bulletin.end_date') + content_tag(:b, nil, :class => is_sort?('deadline'))).html_safe, panel_announcement_back_end_bulletins_path({:filter => @filter}.merge(sortable('deadline'))), :class => 'js_history' %>
</th>
<th class="sort span1-2 <%= is_sort_active?('tags') %>">
<%= link_to (t('bulletin.tags') + content_tag(:b, nil, :class => is_sort?('tags'))).html_safe, panel_announcement_back_end_bulletins_path({:filter => @filter}.merge(sortable('tags'))), :class => 'js_history' %>
</th>
<th class="sort span1-3 <%= is_sort_active?('update_user_id') %>">
<%= link_to (t('bulletin.last_modified') + content_tag(:b, nil, :class => is_sort?('update_user_id'))).html_safe, panel_announcement_back_end_bulletins_path({:filter => @filter}.merge(sortable('update_user_id'))), :class => 'js_history' %>
</th>
</tr>
</thead>
</table>

View File

@ -1,4 +1,4 @@
<%= form_for :bulletins, :url => delete_panel_announcement_back_end_bulletins_path(:direction => params[:direction], :sort => params[:sort], :filter => @filter, :new_filter => nil), :html => {:id => 'delete_bulletins'}, :remote => true do %> <%= form_for :bulletins, :url => delete_panel_announcement_back_end_bulletins_path(:direction => params[:direction], :sort => params[:sort], :filter => @filter, :new_filter => nil, :sort_options => params[:sort_options]), :html => {:id => 'delete_all'}, :remote => true do %>
<%= render 'filter' %> <%= render 'filter' %>
<table id="bulettin_sort_list" class="table main-list"> <table id="bulettin_sort_list" class="table main-list">
<%= render 'bulletins' %> <%= render 'bulletins' %>
@ -6,7 +6,7 @@
<% end %> <% end %>
<div id="bulletin_pagination" class="paginationFixed"> <div id="bulletin_pagination" class="paginationFixed">
<%= paginate @bulletins, :params => {:direction => params[:direction], :sort => params[:sort], :filter => @filter, :new_filter => nil} %> <%= paginate @bulletins, :params => {:direction => params[:direction], :sort => params[:sort], :filter => @filter, :new_filter => nil, :sort_options => params[:sort_options]} %>
</div> </div>
<div id="bulletin_link_qe"> <div id="bulletin_link_qe">

View File

@ -1,7 +1,7 @@
$("#collapse-status").html("<%= j render 'filter_status' %>"); $("#collapse-status").html("<%= j render 'filter_status' %>");
$("#collapse-category").html("<%= j render 'filter_categories' %>"); $("#collapse-category").html("<%= j render 'filter_categories' %>");
$("#collapse-tags").html("<%= j render 'filter_tags' %>"); $("#collapse-tags").html("<%= j render 'filter_tags' %>");
$("#delete_bulletins").attr("action", "<%= delete_panel_announcement_back_end_bulletins_path(:direction => params[:direction], :sort => params[:sort], :filter => @filter) %>"); $("#delete_all").attr("action", "<%= delete_panel_announcement_back_end_bulletins_path(:direction => params[:direction], :sort => params[:sort], :filter => @filter, :new_filter => nil, :sort_options => params[:sort_options]) %>");
$("#sort_headers").html("<%= j render 'sort_headers' %>"); $("#sort_headers").html("<%= j render 'sort_headers' %>");
$("#tbody_bulletins").html("<%= j render :partial => 'bulletin', :collection => @bulletins %>"); $("#tbody_bulletins").html("<%= j render :partial => 'bulletin', :collection => @bulletins %>");
$("#bulletin_pagination").html("<%= j paginate @bulletins, :params => {:direction => params[:direction], :sort => params[:sort], :filter => @filter, :new_filter => nil} %>"); $("#bulletin_pagination").html("<%= j paginate @bulletins, :params => {:direction => params[:direction], :sort => params[:sort], :filter => @filter, :new_filter => nil} %>");

View File

@ -4,4 +4,8 @@
<%= render 'sort_headers' %> <%= render 'sort_headers' %>
</div> </div>
</div> </div>
</div> </div>
<% content_for :page_specific_javascript do %>
<%= javascript_include_tag "sort_header" %>
<% end %>

View File

@ -1,18 +1,5 @@
<table class="table main-list"> <%= render_sort_bar(panel_web_resource_back_end_web_links_path, false,
<thead> ['status', ['is_top', 'is_hot', 'is_hidden', 'is_pending', 'is_checked', 'is_rejected'], 'span1', 'bulletin.status'],
<tr class="sort-header"> ['category', 'bulletin_category', 'span2', 'bulletin.category'],
<th class="sort span1 <%= is_sort_active?('status') %>"> ['name', 'name','span3', 'bulletin.title'],
<%= link_to (t('web_link.status') + content_tag(:b, nil, :class => is_sort?('status'))).html_safe, panel_web_resource_back_end_web_links_path({:filter => @filter}.merge(sortable('status'))), :class => 'js_history' %> ['tags', 'tags', 'span2', 'bulletin.tags']).html_safe %>
</th>
<th class="sort span1 <%= is_sort_active?('category') %>">
<%= link_to (t('web_link.category') + content_tag(:b, nil, :class => is_sort?('category'))).html_safe, panel_web_resource_back_end_web_links_path({:filter => @filter}.merge(sortable('category'))), :class => 'js_history' %>
</th>
<th class="sort span3 <%= is_sort_active?('name') %>">
<%= link_to (t('web_link.name') + content_tag(:b, nil, :class => is_sort?('name'))).html_safe, panel_web_resource_back_end_web_links_path({:filter => @filter}.merge(sortable('name'))), :class => 'js_history' %>
</th>
<th class="sort span2 <%= is_sort_active?('tags') %>">
<%= link_to (t('web_link.tags') + content_tag(:b, nil, :class => is_sort?('tags'))).html_safe, panel_web_resource_back_end_web_links_path({:filter => @filter}.merge(sortable('tags'))), :class => 'js_history' %>
</th>
</tr>
</thead>
</table>

View File

@ -18,4 +18,4 @@
</div> </div>
<div class="form-actions form-fixed pagination-right"> <div class="form-actions form-fixed pagination-right">
<%= link_to content_tag(:i, nil, :class => 'icon-plus icon-white') + t('announcement.add_new'), new_panel_web_resource_back_end_web_link_path, :class => 'btn btn-primary' %> <%= link_to content_tag(:i, nil, :class => 'icon-plus icon-white') + t('announcement.add_new'), new_panel_web_resource_back_end_web_link_path, :class => 'btn btn-primary' %>
</div> </div>