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 :file_size, :placeholder => "单位:M", :class => "wb85" %> +请输入正数