Browse files

add exporting csv feature

  • Loading branch information...
1 parent 742e753 commit 144486a34a5ab9f5c84b9aac4ae7b9f1fa312bec @marutosi marutosi committed Jan 4, 2013
Showing with 37 additions and 0 deletions.
  1. +9 −0 app/controllers/ratios_controller.rb
  2. +24 −0 app/helpers/ratios_helper.rb
  3. +4 −0 app/views/ratios/index.html.erb
View
9 app/controllers/ratios_controller.rb
@@ -2,6 +2,9 @@ class RatiosController < ApplicationController
unloadable
include IndicatorsLogic
+ helper :ratios
+ include RatiosHelper
+
def index
project = Project.find(params[:id])
data = IndicatorsLogic::retrive_data(project)
@@ -17,5 +20,11 @@ def index
:indicators => IndicatorsLogic::calc_indicators(my_version, data[0], data[1])
}
end
+ respond_to do |format|
+ format.html { render :action => 'index' }
+ format.csv {
+ send_data(evm_csv(@evms), :type => 'text/csv; header=present', :filename => 'evm.csv')
+ }
+ end
end
end
View
24 app/helpers/ratios_helper.rb
@@ -1,2 +1,26 @@
module RatiosHelper
+ def evm_csv(evms)
+ decimal_separator = l(:general_csv_decimal_separator)
+ export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
+ evms.each do |evm|
+ fields = ["", "cpi", "spi"]
+ csv << fields.collect {|c| Redmine::CodesetUtil.from_utf8(
+ c,
+ l(:general_csv_encoding) ) }
+ fields = []
+ fields << evm[:name]
+ fields << evm[:indicators][1].to_s.gsub('.', decimal_separator)
+ fields << evm[:indicators][2].to_s.gsub('.', decimal_separator)
+ csv << fields.collect {|c| Redmine::CodesetUtil.from_utf8(
+ c,
+ l(:general_csv_encoding) ) }
+ evm[:indicators][0].each do |ent|
+ csv << ent.collect {|c| Redmine::CodesetUtil.from_utf8(
+ c.to_s.gsub('.', decimal_separator),
+ l(:general_csv_encoding) ) }
+ end
+ end
+ end
+ export
+ end
end
View
4 app/views/ratios/index.html.erb
@@ -10,3 +10,7 @@
<%= render "ratios/head_js" %>
<%= render :partial=>"ratios/bottom_js", :locals => {:evm => evm} %>
<% end %>
+
+<% other_formats_links do |f| %>
+ <%= f.link_to 'CSV', :url => params %>
+<% end %>

0 comments on commit 144486a

Please sign in to comment.