diff --git a/app/controllers/statistics_controller.rb b/app/controllers/statistics_controller.rb index beebb331c..914169a13 100644 --- a/app/controllers/statistics_controller.rb +++ b/app/controllers/statistics_controller.rb @@ -9,20 +9,17 @@ class StatisticsController < ApplicationController before_filter :require_manager, :only =>[:edit, :update, :destroy] def index - type = (params[:type] == "reorder_popu" ? "size" : "created_at") + type = (params[:type] == "reorder_popu" ? "file_size" : "created_at") order = (params[:order] == "asc" ? "asc" : "desc") if params[:sub_category_id].present? @statistics = Statistic.where(:sub_category_id => params[:sub_category_id]) sub_category_id = '(' + params[:sub_category_id].to_a.join(",") + ')' - @statistics = Statistic.find_by_sql("SELECT statistics.*, (select sum(filesize) from attachments where attachments.container_type='Statistic' and - attachments.container_id = statistics.id group by attachments.container_id) as size from statistics where sub_category_id in #{sub_category_id} order by #{type} #{order}") + @statistics = Statistic.find_by_sql("SELECT statistics.* from statistics where sub_category_id in #{sub_category_id} order by #{type} #{order}") else if params[:main_category_id].present? - @statistics = Statistic.find_by_sql("SELECT statistics.*, (select sum(filesize) from attachments where attachments.container_type='Statistic' and - attachments.container_id = statistics.id group by attachments.container_id) as size from statistics where main_category_id = #{params[:main_category_id]} order by #{type} #{order}") + @statistics = Statistic.find_by_sql("SELECT statistics.* from statistics where main_category_id = #{params[:main_category_id]} order by #{type} #{order}") else - @statistics = Statistic.find_by_sql("SELECT statistics.*, (select sum(filesize) from attachments where attachments.container_type='Statistic' and - attachments.container_id = statistics.id group by attachments.container_id) as size from statistics order by #{type} #{order}") + @statistics = Statistic.find_by_sql("SELECT statistics.* from statistics order by #{type} #{order}") end end @statistics_count = @statistics.count @@ -78,6 +75,9 @@ class StatisticsController < ApplicationController @statistic.save_attachments_containers(params[:attachments], User.current, true) respond_to do |format| if @statistic.save + if @statistic.file_size.nil? && @statistic.attachments.count > 0 + @statistic.update_attribute('file_size', format("%0.3f", @statistic.attachments.map{|c| c.filesize}.sum.to_f/(1024*1024))) + end format.html { redirect_to @statistic, notice: '创建成功' } format.json { render json: @statistic, status: :created, location: @statistic } else @@ -102,6 +102,10 @@ class StatisticsController < ApplicationController end end end + + if @statistic.file_size.nil? && @statistic.attachments.count > 0 + @statistic.update_attribute('file_size', format("%0.3f", @statistic.attachments.map{|c| c.filesize}.sum.to_f/(1024*1024))) + end format.html { redirect_to @statistic, notice: '更新成功' } format.json { head :no_content } diff --git a/app/models/statistic.rb b/app/models/statistic.rb index 2302bd37c..15fafa270 100644 --- a/app/models/statistic.rb +++ b/app/models/statistic.rb @@ -1,5 +1,5 @@ class Statistic < ActiveRecord::Base - attr_accessible :description, :name, :status, :user_id, :main_category_id, :sub_category_id + attr_accessible :description, :name, :status, :user_id, :main_category_id, :sub_category_id, :file_size acts_as_attachable validates_presence_of :main_category_id diff --git a/app/views/statistics/_form.html.erb b/app/views/statistics/_form.html.erb index 40bac0afa..6e18228c0 100644 --- a/app/views/statistics/_form.html.erb +++ b/app/views/statistics/_form.html.erb @@ -31,9 +31,9 @@
- - <%= f.text_field :name, :class => "wb85" %> -

请输入数据名称

+ + <%= f.text_field :file_size, :placeholder => "单位:M", :class => "wb85" %> +

请输入正数

@@ -112,10 +112,14 @@ function submit_cate(){ var name=$("#statistic_name").val(); + var file_size=$("#statistic_file_size").val().trim(); var cate=$("#statistic_main_category_id option:selected").text(); if(name == ""){ $("#input_name_notice").removeClass("none"); return; + } else if(file_size != "" && !/^(?:[1-9][0-9]*\.[0-9]+|0\.(?!0+$)[0-9]+|[1-9]+\d*)$/.test(file_size)){ + $("#input_file_size_notice").show(); + return; } $("#new_statistic").submit(); $("#edit_statistic_<%= @statistic.id %>").submit(); diff --git a/app/views/statistics/_statistics_list.html.erb b/app/views/statistics/_statistics_list.html.erb index 34985a166..718d2096d 100644 --- a/app/views/statistics/_statistics_list.html.erb +++ b/app/views/statistics/_statistics_list.html.erb @@ -25,8 +25,8 @@ <% if statistic.sub_category.present? %> <%= statistic.sub_category.try(:name) %> <% end %> - <% if statistic.size.present? %> - <%= number_to_human_size statistic.size %> Bytes + <% if statistic.file_size.present? %> + <%= statistic.file_size %> MB <% end %> <%= format_time statistic.created_at %>
diff --git a/db/migrate/20180504075834_add_file_size_to_statistics.rb b/db/migrate/20180504075834_add_file_size_to_statistics.rb new file mode 100644 index 000000000..77bf20dd0 --- /dev/null +++ b/db/migrate/20180504075834_add_file_size_to_statistics.rb @@ -0,0 +1,11 @@ +class AddFileSizeToStatistics < ActiveRecord::Migration + def change + add_column :statistics, :file_size, :float + + Statistic.all.each do |statistic| + if statistic.attachments.count > 0 + statistic.update_attribute('file_size', format("%0.3f",statistic.attachments.map{|c| c.filesize}.sum.to_f/(1024*1024))) + end + end + end +end diff --git a/db/schema.rb b/db/schema.rb index df52a9a2b..e4a3dccca 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20180207065530) do +ActiveRecord::Schema.define(:version => 20180504075834) do create_table "activities", :force => true do |t| t.integer "act_id", :null => false @@ -1133,6 +1133,9 @@ ActiveRecord::Schema.define(:version => 20180207065530) do t.integer "score", :default => -1 end + add_index "exercise_answers", ["exercise_choice_id"], :name => "ec" + add_index "exercise_answers", ["exercise_question_id", "user_id"], :name => "ea" + create_table "exercise_bank_choices", :force => true do |t| t.integer "exercise_bank_question_id" t.text "choice_text" @@ -1221,6 +1224,8 @@ ActiveRecord::Schema.define(:version => 20180207065530) do t.integer "subjective_score", :default => -1 end + add_index "exercise_users", ["exercise_id"], :name => "index_exercise_users_on_exercise_id" + create_table "exercises", :force => true do |t| t.text "exercise_name" t.text "exercise_description"