DRY code for sorting

This commit is contained in:
Christophe Vilayphiou committed 2012-04-30 06:30:52 +08:00
1 parent fb6c2a24df
commit 32faef0c70
16 files changed
+228 -89

No files matched your search

@@ -30,7 +30,10 @@ class Panel::Announcement::BackEnd::BulletinsController < OrbitBackendController
# @bulletins = Bulletin.search(params[:search], params[:category_id])
# @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_link = BulletinLink.new
@@ -300,7 +303,7 @@ class Panel::Announcement::BackEnd::BulletinsController < OrbitBackendController
if params[:to_delete]
bulletins = Bulletin.any_in(:_id => params[:to_delete]).delete_all
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
@@ -14,7 +14,7 @@ class Bulletin
field :deadline , :type => Date
# field :url
field :create_user_id
field :update_user_id
field :update_user_id, :class_name => "User"
field :is_top, :type => Boolean, :default => false
field :is_hot, :type => Boolean, :default => false
@@ -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
@@ -30,4 +30,8 @@
<%= render 'sort_headers' %>
</div>
</div>
</div>
</div>
<% content_for :page_specific_javascript do %>
<%= javascript_include_tag "sort_header" %>
<% end %>
@@ -292,7 +292,6 @@
<% content_for :page_specific_javascript do %>
<%= javascript_include_tag "bulletin_form" %>
<%= javascript_include_tag "inc/jquery.imagesloaded.js" %>
<script>
$('#add_bulletin_link a.add').live('click', function(){
@@ -1,31 +1,8 @@
<table class="table main-list">
<thead>
<tr class="sort-header">
<th class="span1 strong">
<input id="check_all_bulletins" type="checkbox">
<a href='#' class="list-remove"><i class="icon-trash"></i></a>
</th>
<th class="sort span1-2 <%= is_sort_active?('status') %>">
<%= 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>
<%= render_sort_bar(panel_announcement_back_end_bulletins_path, true,
['status', ['is_top', 'is_hot', 'is_hidden', 'is_pending', 'is_checked', 'is_rejected'], 'span1-2', 'bulletin.status'],
['category', 'bulletin_category', 'span1-2', 'bulletin.category'],
['title', 'title','span7', 'bulletin.title'],
['start_date', 'postdate', 'span1-2', 'bulletin.start_date'],
['end_date', 'deadline', 'span1-2', 'bulletin.end_date'],
['tags', 'tags', 'span1-2', 'bulletin.tags'],
['last_modified', 'update_user_id','span1-3', 'bulletin.last_modified']).html_safe %>
@@ -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' %>
<table id="bulettin_sort_list" class="table main-list">
<%= render 'bulletins' %>
@@ -6,7 +6,7 @@
<% end %>
<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 id="bulletin_link_qe">
@@ -1,7 +1,7 @@
$("#collapse-status").html("<%= j render 'filter_status' %>");
$("#collapse-category").html("<%= j render 'filter_categories' %>");
$("#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' %>");
$("#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} %>");
@@ -4,4 +4,8 @@
<%= render 'sort_headers' %>
</div>
</div>
</div>
</div>
<% content_for :page_specific_javascript do %>
<%= javascript_include_tag "sort_header" %>
<% end %>
@@ -1,18 +1,5 @@
<table class="table main-list">
<thead>
<tr class="sort-header">
<th class="sort span1 <%= is_sort_active?('status') %>">
<%= 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' %>
</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>
<%= render_sort_bar(panel_web_resource_back_end_web_links_path, false,
['status', ['is_top', 'is_hot', 'is_hidden', 'is_pending', 'is_checked', 'is_rejected'], 'span1', 'bulletin.status'],
['category', 'bulletin_category', 'span2', 'bulletin.category'],
['name', 'name','span3', 'bulletin.title'],
['tags', 'tags', 'span2', 'bulletin.tags']).html_safe %>
@@ -18,4 +18,4 @@
</div>
<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' %>
</div>
</div>