0

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.

5
  • Move your if else end statements to the variables, and put variables on their places instead. Commented Nov 28, 2017 at 10:34
  • or move them to private methods and call the methods in there place. Also, What is none? Is it a variable?. Commented Nov 28, 2017 at 10:45
  • 2
    @xeon131 My guess is that this method is defined within a Rails model, so that's: apidock.com/rails/ActiveRecord/QueryMethods/none Commented Nov 28, 2017 at 10:52
  • [refactoring suggestions] Please move this to a separate class and call that class to generate the csv instead of dumping this code in a model Commented Nov 28, 2017 at 10:58
  • Gottta find a way to refactor this code its too messy. Sorry I am new to ROR and I really want to improve and learn some design pattern techniques. Thanks for the tip guy. Commented Dec 1, 2017 at 1:39

1 Answer 1

4

You have a syntax error in the array definition. For example:

[
  # ...
  employee_event.status,

  if (...)
    employee_event.steps.where(step_type: :initiate).etc.etc,
  else
    none,
  end

  # ...
]

Should instead be written as:

[
  # ...
  employee_event.status,

  if (...)
    employee_event.steps.where(step_type: :initiate).etc.etc
  else
    none
  end, # <--- !!!!!

  # ...
]

You can think this code as saying "the array element is the return value of the if statement".

Each item in the array must be separated by a comma, and you are only adding one item to the array for each if statement.

There are, as others have suggested, many other ways that the code could be improved; but this should resolve your immediate problem.

Sign up to request clarification or add additional context in comments.

1 Comment

Thankyou so much! Is there someway where can I put the code instead of the model. Cause I am really new to ROR and I want to find a way to clean this code in the model.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.