更改 FastCSV 的输出

发布于 2024-10-02 06:09:19 字数 1163 浏览 2 评论 0原文

我目前有一个控制器,它将处理使用 FasterCSV gem 将表导出到 CSV 文件的调用。问题是数据库中存储的信息有时不清楚,所以我想更改特定列的输出。

例如,我的 project.status 列有数字而不是状态,即数据库中的 1 对应于“活动”,2 表示“非活动”,0 表示“尚未决定”。当我导出表时,它显示 0,1,2,而不是“活动”、“非活动”或“尚未决定”。知道如何实现吗?

我尝试了一个简单的循环,该循环将检查最终生成的 CSV 文件并将每个 0,1,2 更改为其相应的输出,但问题是具有 0,1,2 的其他所有列也会更改。我不知道如何隔离该列。 提前致谢

 def csv
    qt = params[:selection]
    @lists = Project.find(:all, :order=> (params[:sort] + ' ' + params[:direction]), :conditions =>  ["name LIKE ? OR description LIKE ?", "%#{qt}%", "%#{qt}%"])

    csv_string = FasterCSV.generate(:encoding => 'u') do |csv|
      csv << ["Status","Name","Summary","Description","Creator","Comment","Contact Information","Created Date","Updated Date"]
      @lists.each do |project|
              csv << [project.status, project.name, project.summary, project.description, project.creator, project.statusreason, project.contactinfo, project.created_at, project.updated_at]
      end
    end

    filename = Time.now.strftime("%Y%m%d") + ".csv"
    send_data(csv_string,
      :type => 'text/csv; charset=UTF-8; header=present',
      :filename => filename)
  end

I currently have a controller that will handle a call to export a table into a CSV file using the FasterCSV gem. The problem is the information stored in the database isn't clear sometimes and so I want to change the output for a particular column.

My project.status column for instance has numbers instead of statuses ie 1 in the database corresponds to Active, 2 for Inactive and 0 for Not Yet decided. When I export the table it shows 0,1,2 instead of Active, Inactive or Not Yet decided. Any idea how to implement this?

I tried a simple loop that would check the final generated CSV file and change each 0,1,2 to its corresponding output, but the problem is every other column that had a 0,1,2 would change as well. I'm not sure how to isolate the column.
Thanks in advance

 def csv
    qt = params[:selection]
    @lists = Project.find(:all, :order=> (params[:sort] + ' ' + params[:direction]), :conditions =>  ["name LIKE ? OR description LIKE ?", "%#{qt}%", "%#{qt}%"])

    csv_string = FasterCSV.generate(:encoding => 'u') do |csv|
      csv << ["Status","Name","Summary","Description","Creator","Comment","Contact Information","Created Date","Updated Date"]
      @lists.each do |project|
              csv << [project.status, project.name, project.summary, project.description, project.creator, project.statusreason, project.contactinfo, project.created_at, project.updated_at]
      end
    end

    filename = Time.now.strftime("%Y%m%d") + ".csv"
    send_data(csv_string,
      :type => 'text/csv; charset=UTF-8; header=present',
      :filename => filename)
  end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

作妖 2024-10-09 06:09:19

这实际上相当容易。在您的控制器代码中:

  #app/controllers/projects_controller.rb#csv
  @lists.each do |project|
          csv << [project.descriptive_status, project.name, project.summary, project.description, project.creator, project.statusreason, project.contactinfo, project.created_at, project.updated_at]
  end

然后在您的模型代码中。不过,您可能已经有了一种将数据库状态解码为更具描述性的方法:

#app/models/project.rb

ACTIVE_STATUS = 0
INACTIVE_STATUS = 1
NOT_YET_DECIDED_STATUS = 2

def descriptive_status
  case status
    when ACTIVE_STATUS
      "Active"
    when INACTIVE_STATUS
      "Inactive"
    when NOT_YET_DECIDED_STATUS
      "Not Yet Decided"
  end
end

您可能可以通过多种方法来重构它。至少在控制器中,最好使该查找器成为更具描述性的命名范围。模型中的常量可以引入 SettingsLogic 配置或其他类似的 gem。

This is actually fairly easy. In your controller code:

  #app/controllers/projects_controller.rb#csv
  @lists.each do |project|
          csv << [project.descriptive_status, project.name, project.summary, project.description, project.creator, project.statusreason, project.contactinfo, project.created_at, project.updated_at]
  end

Then in your model code. You probably already have a method that decodes the DB status to a more descriptive one though:

#app/models/project.rb

ACTIVE_STATUS = 0
INACTIVE_STATUS = 1
NOT_YET_DECIDED_STATUS = 2

def descriptive_status
  case status
    when ACTIVE_STATUS
      "Active"
    when INACTIVE_STATUS
      "Inactive"
    when NOT_YET_DECIDED_STATUS
      "Not Yet Decided"
  end
end

There are probably a number of ways you can then refactor this. In the controller at least, it would probably be best to make that finder a more descriptive named scope. The constants in the model could be brought into SettingsLogic configuration or another similar gem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文