I am to ROR and I just wonder if how can I print a CSV with some conditions on a specifid data coming from my query? I didn't use some design patterns because I am a newbie. I just want to know if is this is possible in printing CSV in ruby on rails.
Here is the sample code in the given model to print CSV.
def self.cross_mri_transfer_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
approved_by
endorsed_by
confirmed_by
start_date
applied_at
}
CSV.generate(headers: true) do |csv|
csv << attributes.map{|e| e.split("_").map(&:capitalize).join(' ')}
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,
if (employee_event.steps.where(step_type: :initiate).first.performed_actor.present?)
employee_event.steps.where(step_type: :initiate).first.performed_actor.fullname_formal.upcase,
else
none,
end
if (employee_event.steps.where(step_type: :approve).first.performed_actor.present?)
employee_event.steps.where(step_type: :approve).first.performed_actor.fullname_formal.upcase,
else
none,
end
if (employee_event.steps.where(step_type: :endorse).first.performed_actor.present?)
employee_event.steps.where(step_type: :endorse).first.performed_actor.fullname_formal.upcase,
else
none,
end
if (employee_event.steps.where(step_type: :confirm).first.performed_actor.present?)
employee_event.steps.where(step_type: :confirm).first.performed_actor.fullname_formal.upcase,
else
none,
end
employee_event.start_date,
employee_event.applied_at
]
end
end
end
I know there is something wrong in the code with syntax error. It is just a presentation or example. But is this possible in printing CSV? Sorry for the question but I just want to know because I tried goggling this for a day but didn't find out anything there :(
Appreciate if someone can help. Thanks in advance.
if else endstatements to the variables, and put variables on their places instead.