archive module: add sort feature to achive files

This commit is contained in:
iCross 2013-06-06 22:19:44 +08:00
parent c63ac751ea
commit 5b7ac1a22b
4 changed files with 34 additions and 21 deletions

View File

@ -21,7 +21,7 @@ module Mongoid
private private
def set_sort_number def set_sort_number
if self.sort_number.nil? if self.new? && self.sort_number.nil?
last_record = self.class.desc(:sort_number).first last_record = self.class.desc(:sort_number).first
if last_record if last_record
self.sort_number = (last_record.sort_number + 10) / 10 * 10 self.sort_number = (last_record.sort_number + 10) / 10 * 10

View File

@ -2,23 +2,26 @@ class ArchiveFileMultiple
include Mongoid::Document include Mongoid::Document
include Mongoid::Timestamps include Mongoid::Timestamps
include Mongoid::Sortable
mount_uploader :file, AssetUploader mount_uploader :file, AssetUploader
field :file_title, localize: true field :file_title, localize: true
# field :description # field :description
field :choose_lang, :type => Array, :default => nil field :choose_lang, :type => Array, :default => nil
field :should_destroy, :type => Boolean field :should_destroy, :type => Boolean
default_scope asc(:sort_number)
def choose_lang_display(lang) def choose_lang_display(lang)
self.choose_lang.include?(lang) self.choose_lang.include?(lang)
end end
belongs_to :archive_file belongs_to :archive_file
# has_many :archive_file_multiple_langs, :autosave => true, :dependent => :destroy # has_many :archive_file_multiple_langs, :autosave => true, :dependent => :destroy
# accepts_nested_attributes_for :archive_file_multiple_langs, :allow_destroy => true # accepts_nested_attributes_for :archive_file_multiple_langs, :allow_destroy => true
end end

View File

@ -82,6 +82,7 @@
<table class="table table-condensed"> <table class="table table-condensed">
<thead> <thead>
<tr> <tr>
<th>排序數</th>
<th>File</th> <th>File</th>
<th>File Name</th> <th>File Name</th>
<th class="span2"><%= t('呈現語系')%></th> <th class="span2"><%= t('呈現語系')%></th>
@ -137,7 +138,9 @@
var new_id = $(this).prev().attr('value'); var new_id = $(this).prev().attr('value');
var old_id = new RegExp("new_archive_file_multiples", "g"); var old_id = new RegExp("new_archive_file_multiples", "g");
$(this).prev().attr('value', parseInt(new_id) + 1); $(this).prev().attr('value', parseInt(new_id) + 1);
$(this).parents('table').append(("<%= escape_javascript(add_attribute 'form_file', f, :archive_file_multiples) %>").replace(old_id, new_id)); var new_form_file = $("<%= escape_javascript(add_attribute 'form_file', f, :archive_file_multiples) %>".replace(old_id, new_id));
new_form_file.find('input#archive_file_archive_file_multiples_attributes_'+new_id+'_sort_number').val(new_id * 10)
$(this).parents('table').append(new_form_file);
}); });
$('.archive_file_multiples_block a.delete').live('click', function(){ $('.archive_file_multiples_block a.delete').live('click', function(){

View File

@ -1,6 +1,13 @@
<% # encoding: utf-8 %> <% # encoding: utf-8 %>
<tr id="<%= "archive_file_multiple_#{form_file.id}" if !form_file.new_record? %>" class='list_item'> <tr id="<%= "archive_file_multiple_#{form_file.id}" if !form_file.new_record? %>" class='list_item'>
<td>
<div class="control-group">
<div class="controls">
<%= f.text_field :sort_number %>
</div>
</div>
</td>
<td> <td>
<div class="control-group"> <div class="control-group">
<div class="controls"> <div class="controls">
@ -8,13 +15,13 @@
<%= form_file.file.file ? ( link_to t(:view), form_file.file.url, {:class => 'btn', :target => '_blank', :title => t(:view)} ) : '' %> <%= form_file.file.file ? ( link_to t(:view), form_file.file.url, {:class => 'btn', :target => '_blank', :title => t(:view)} ) : '' %>
</div> </div>
</div> </div>
</td> </td>
<td> <td>
<div class="tab-content"> <div class="tab-content">
<% @site_valid_locales.each_with_index do |locale, i| %> <% @site_valid_locales.each_with_index do |locale, i| %>
<div class="<%= locale %> fade tab-pane <%= ( i == 0 ) ? "in active" : '' %>"> <div class="<%= locale %> fade tab-pane <%= ( i == 0 ) ? "in active" : '' %>">
<%#= f.fields_for :i18n_variable, (form_file.new_record? ? form_file.build_i18n_variable : form_file.i18n_variable ) do |f| %> <%#= f.fields_for :i18n_variable, (form_file.new_record? ? form_file.build_i18n_variable : form_file.i18n_variable ) do |f| %>
<%= f.fields_for :file_title_translations do |f| %> <%= f.fields_for :file_title_translations do |f| %>
@ -24,14 +31,14 @@
<%= f.text_field locale, :class=>'post-file_title', :value => (form_file.file_title_translations[locale] rescue nil) %> <%= f.text_field locale, :class=>'post-file_title', :value => (form_file.file_title_translations[locale] rescue nil) %>
</div> </div>
</div> </div>
<% end %> <% end %>
</div> </div>
<% end %> <% end %>
</div> </div>
</td> </td>
<td> <td>
<% @site_valid_locales.each do |locale| %> <% @site_valid_locales.each do |locale| %>
@ -40,18 +47,18 @@
<% end %> <% end %>
<%= hidden_field_tag 'archive_file[archive_file_multiples_attributes][0][choose_lang][]', '' %> <%= hidden_field_tag 'archive_file[archive_file_multiples_attributes][0][choose_lang][]', '' %>
</td> </td>
<td> <td>
<span class="action"> <span class="action">
<% if form_file.new_record? %> <% if form_file.new_record? %>
<a class="delete"><i class="icon-remove"></i></a> <a class="delete"><i class="icon-remove"></i></a>
<% else %> <% else %>
<%= f.hidden_field :id %> <%= f.hidden_field :id %>
<a class="remove_existing_record"><i class="icon-remove"></i></a> <a class="remove_existing_record"><i class="icon-remove"></i></a>
<%= f.hidden_field :should_destroy, :value => nil, :class => 'should_destroy' %> <%= f.hidden_field :should_destroy, :value => nil, :class => 'should_destroy' %>
<% end %> <% end %>
</span> </span>
</td> </td>
</tr> </tr>