I have a nasty (IMO) piece of code for a datatable configuration, how do i best remove the duplication here?
I've tried the obvious things I know (collapsing into one statement and trying inline conditionals) but it isn't working and I can't help thinking there must be a more elegant Ruby way here.
if show_status == "true"
if show_requested_by == "true"
records.map do |record|
{
warning: as_warning(record),
updated_at: as_time_ago(record.updated_at),
status: as_status(record.status),
user: record.user.nil? ? "" : record.user.formal_name,
manager: record.manager.nil? ? "" : record.manager.formal_name,
first_date: as_date(record.first_date),
last_date: as_date(record.last_date),
days: as_number(record.total_days),
hours: as_number(record.total_hours),
units: as_number(record.total_units),
value: as_number(record.total_value),
comment: as_comment(record.comment),
actions: as_actions(record)
}
end
else
records.map do |record|
{
warning: as_warning(record),
updated_at: as_time_ago(record.updated_at),
status: as_status(record.status),
manager: record.manager.nil? ? "" : record.manager.formal_name,
first_date: as_date(record.first_date),
last_date: as_date(record.last_date),
days: as_number(record.total_days),
hours: as_number(record.total_hours),
units: as_number(record.total_units),
value: as_number(record.total_value),
comment: as_comment(record.comment),
actions: as_actions(record)
}
end
end
else
if show_requested_by == "true"
records.map do |record|
{
warning: as_warning(record),
updated_at: as_time_ago(record.updated_at),
user: record.user.nil? ? "" : record.user.formal_name,
manager: record.manager.nil? ? "" : record.manager.formal_name,
first_date: as_date(record.first_date),
last_date: as_date(record.last_date),
days: as_number(record.total_days),
hours: as_number(record.total_hours),
units: as_number(record.total_units),
value: as_number(record.total_value),
comment: as_comment(record.comment),
actions: as_actions(record)
}
end
else
records.map do |record|
{
warning: as_warning(record),
updated_at: as_time_ago(record.updated_at),
manager: record.manager.nil? ? "" : record.manager.formal_name,
first_date: as_date(record.first_date),
last_date: as_date(record.last_date),
days: as_number(record.total_days),
hours: as_number(record.total_hours),
units: as_number(record.total_units),
value: as_number(record.total_value),
comment: as_comment(record.comment),
actions: as_actions(record)
}
end
end
end