From 5b7ac1a22b607df5db56e97e8bd336e1d7d63b08 Mon Sep 17 00:00:00 2001 From: iCross Date: Thu, 6 Jun 2013 22:19:44 +0800 Subject: [PATCH] archive module: add sort feature to achive files --- app/models/mongoid/sortable.rb | 2 +- .../app/models/archive_file_multiple.rb | 15 +++++---- .../back_end/archive_files/_form.html.erb | 5 ++- .../archive_files/_form_file.html.erb | 33 +++++++++++-------- 4 files changed, 34 insertions(+), 21 deletions(-) diff --git a/app/models/mongoid/sortable.rb b/app/models/mongoid/sortable.rb index 1e7302b8..16a69def 100644 --- a/app/models/mongoid/sortable.rb +++ b/app/models/mongoid/sortable.rb @@ -21,7 +21,7 @@ module Mongoid private def set_sort_number - if self.sort_number.nil? + if self.new? && 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 diff --git a/vendor/built_in_modules/archive/app/models/archive_file_multiple.rb b/vendor/built_in_modules/archive/app/models/archive_file_multiple.rb index 9dc83363..d45d34a8 100644 --- a/vendor/built_in_modules/archive/app/models/archive_file_multiple.rb +++ b/vendor/built_in_modules/archive/app/models/archive_file_multiple.rb @@ -2,23 +2,26 @@ class ArchiveFileMultiple include Mongoid::Document include Mongoid::Timestamps + include Mongoid::Sortable mount_uploader :file, AssetUploader - + field :file_title, localize: true # field :description field :choose_lang, :type => Array, :default => nil - + field :should_destroy, :type => Boolean + default_scope asc(:sort_number) + def choose_lang_display(lang) self.choose_lang.include?(lang) end - + belongs_to :archive_file - + # has_many :archive_file_multiple_langs, :autosave => true, :dependent => :destroy - + # accepts_nested_attributes_for :archive_file_multiple_langs, :allow_destroy => true - + end diff --git a/vendor/built_in_modules/archive/app/views/panel/archive/back_end/archive_files/_form.html.erb b/vendor/built_in_modules/archive/app/views/panel/archive/back_end/archive_files/_form.html.erb index 01bd76e0..318ba6b5 100644 --- a/vendor/built_in_modules/archive/app/views/panel/archive/back_end/archive_files/_form.html.erb +++ b/vendor/built_in_modules/archive/app/views/panel/archive/back_end/archive_files/_form.html.erb @@ -82,6 +82,7 @@ + @@ -137,7 +138,9 @@ var new_id = $(this).prev().attr('value'); var old_id = new RegExp("new_archive_file_multiples", "g"); $(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(){ diff --git a/vendor/built_in_modules/archive/app/views/panel/archive/back_end/archive_files/_form_file.html.erb b/vendor/built_in_modules/archive/app/views/panel/archive/back_end/archive_files/_form_file.html.erb index f2c11f06..52f77666 100644 --- a/vendor/built_in_modules/archive/app/views/panel/archive/back_end/archive_files/_form_file.html.erb +++ b/vendor/built_in_modules/archive/app/views/panel/archive/back_end/archive_files/_form_file.html.erb @@ -1,6 +1,13 @@ <% # encoding: utf-8 %> " class='list_item'> + - + - + - +
排序數 File File Name <%= t('呈現語系')%>
+
+
+ <%= f.text_field :sort_number %> +
+
+
@@ -8,13 +15,13 @@ <%= form_file.file.file ? ( link_to t(:view), form_file.file.url, {:class => 'btn', :target => '_blank', :title => t(:view)} ) : '' %>
-
- + +
- + <% @site_valid_locales.each_with_index do |locale, i| %> - +
"> <%#= 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| %> @@ -24,14 +31,14 @@ <%= f.text_field locale, :class=>'post-file_title', :value => (form_file.file_title_translations[locale] rescue nil) %>
- <% end %> - + <% end %> + <% end %> - + - +
<% @site_valid_locales.each do |locale| %> @@ -40,18 +47,18 @@ <% end %> <%= hidden_field_tag 'archive_file[archive_file_multiples_attributes][0][choose_lang][]', '' %> <% if form_file.new_record? %> - <% else %> + <% else %> <%= f.hidden_field :id %> <%= f.hidden_field :should_destroy, :value => nil, :class => 'should_destroy' %> <% end %> - +