Files
nairobi/app/helpers/budget_investments_helper.rb
Javi Martín db1ccb18c7 Use safe_join instead of html_safe
The name `html_safe` is very confusing, and many developers (including
me a few years ago) think what that method does is convert the HTML
contents to safe content. It's actually quite the opposite: it marks the
string as safe, so the HTML inside it isn't stripped out by Rails.

In some cases we were marking strings as safe because we wanted to add
some HTML. However, it meant the whole string was considered safe, and
not just the contents which were under our control.

In particular, some translations added by admins to the database or
through crowding were marked as safe, when it wasn't necessarily the
case.

Although AFAIK crowdin checks for potential cross-site scripting
attacks, it's a good practice to sanitize parts of a string potentially
out of our control before marking the string as HTML safe.
2019-10-08 18:46:20 +02:00

53 lines
1.3 KiB
Ruby

module BudgetInvestmentsHelper
def budget_investments_advanced_filters(params)
params.map { |af| t("admin.budget_investments.index.filters.#{af}") }.join(", ")
end
def link_to_investments_sorted_by(column)
direction = set_direction(params[:direction])
icon = set_sorting_icon(direction, column)
translation = t("admin.budget_investments.index.list.#{column}")
link_to(
safe_join([translation, content_tag(:span, "", class: "icon-sortable #{icon}")]),
admin_budget_budget_investments_path(sort_by: column, direction: direction)
)
end
def set_sorting_icon(direction, sort_by)
if sort_by.to_s == params[:sort_by]
if direction == "desc"
"desc"
else
"asc"
end
else
""
end
end
def set_direction(current_direction)
current_direction == "desc" ? "asc" : "desc"
end
def investments_minimal_view_path
budget_investments_path(id: @heading.group.to_param,
heading_id: @heading.to_param,
filter: @current_filter,
view: investments_secondary_view)
end
def investments_default_view?
@view == "default"
end
def investments_current_view
@view
end
def investments_secondary_view
investments_current_view == "default" ? "minimal" : "default"
end
end