简体   繁体   中英

How can I turn underscore into spaces in printing CSV in Ruby on Rails?

I want to know if is there a way to turn CSV headers into formal naming like last_name to "Last Name".

Here is the code in my Controller:

def cross_mri_transfer
  @reports = EmployeeEvent.where(event_type: "cross_mri_transfer")
  respond_to do |format|
    format.html
    format.csv { send_data @reports.cmt_to_csv, filename: "cross-mri-transfer-event-#{Date.today}.csv" }
  end
end

And here is the code in my method in the model:

def self.cmt_to_csv
  attributes = %w{
    employee_name
    location_assignment
    job_grade_position
    from_institution
    from_cluster
    from_region
    from_area
    from_unit
    to_institution
    to_cluster
    to_region
    to_area
    to_unit
    status
    initiated_by
    start_date
    approved_by
    applied_at
  }

CSV.generate(headers: true) do |csv|
  csv << attributes
  all.each do |employee_event|
    csv << [ 
      employee_event.employee.fullname_formal,
      employee_event.location_assignment,
      employee_event.job_grade_position,
      employee_event.event_data[:old][:institution_name],
      employee_event.event_data[:old][:cluster_name],
      employee_event.event_data[:old][:region_name],
      employee_event.event_data[:old][:area_name],
      employee_event.event_data[:old][:unit_name],
      employee_event.event_data[:new][:institution_name],
      employee_event.event_data[:new][:cluster_name],
      employee_event.event_data[:new][:region_name],
      employee_event.event_data[:new][:area_name],
      employee_event.event_data[:new][:unit_name],
      employee_event.status,
      employee_event.steps.where(step_type: :approve).first.performed_actor.fullname_formal.upcase,
      employee_event.start_date,
      employee_event.steps.where(step_type: :approve).last.performed_actor.fullname_formal.upcase,
      employee_event.applied_at 
    ]
  end
end

So moving on is there some way to replace the header underscore with spaces and capitalize the first character of the headers?

This should be nice and simple - add the line

attributes.map!(&:humanize)

You can also use :titleize if you prefer how that turns out.

:humanize separates the words and capitalises the first letter of the first word; :titleize separates the words and upcases the first letter of each word.

This will transform the strings into a nice, readable format - docs here:

https://apidock.com/rails/String/humanize

https://apidock.com/rails/ActiveSupport/Inflector/titleize

Try with String#split and String#capitalize

> attributes.map{|e| e.split("_").map(&:capitalize).join(' ')}
#=> ["Employee Name", "Location Assignment", "Job Grade Position", "From Institution", "From Cluster", "From Region", "From Area", "From Unit", "To Institution", "To Cluster", "To Region", "To Area", "To Unit", "Status", "Initiated By", "Start Date", "Approved By", "Applied At"]

Note: String#humanize will split words but first word's only first letter capitalize not other word's. For eg:

> "employee_name".humanize
#=> "Employee name"

But as per your eg: you want last_name to "Last Name" .

> "last_name".split("_").map(&:capitalize).join(' ')
#=> "Last Name"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM