1
0
This repository has been archived on 2023-12-07. You can view files and clone it, but cannot push or open issues or pull requests.
body_tracking/app/helpers/body_trackers_helper.rb
2020-05-24 18:57:10 +02:00

75 lines
2.1 KiB
Ruby

module BodyTrackersHelper
def format_value(value, precision=2, mfu_unit=nil)
amount, unit = value
case
when amount.nil?
'-'
when amount.nan?
'?'
else
a = amount.round(precision)
a_desc = a.nonzero? ? "%.#{precision}f" % a : '-'
u_desc = unit && " [#{unit.shortname}]" || ' [-]' if unit != mfu_unit && a.nonzero?
"#{a_desc}#{u_desc}"
end
end
def format_time(t)
t.strftime("%R") if t
end
def toggle_exposure_options(enabled, domain)
enabled = enabled.map { |q| [q.name, q.id] }
enabled_ids = enabled.map(&:last)
options = [[t('body_trackers.helpers.exposures_available'), 0]]
options += nested_set_options(@project.quantities.send(domain)) do |q|
raw("#{' ' * q.level}#{q.name}")
end
options.collect! { |name, id| [name, enabled_ids.include?(id) ? 0 : id] }
options = [[t('body_trackers.helpers.exposures_enabled'), 0]] + enabled + options
options_for_select(options, disabled: 0)
end
def unit_options
@project.units.map do |u|
[u.shortname, u.id]
end
end
def table_header_spec(quantities)
# spec: table of rows (tr), where each row is a hash of cells (td) (hash keeps items
# ordered the way they were added). Hash values determine cell property:
# * int > 0 - quantity name-labelled cell with 'int' size colspan
# * int < 0 - quantity name-labelled cell with 'int' size rowspan
# * nil - non-labelled cell without col-/rowspan
spec = []
default_row = Hash.new(0)
# Determine colspans first...
quantities.each do |q|
ancestors = q.self_and_ancestors.each_with_index do |a, i|
spec[i] ||= default_row.dup
spec[i][a] += 1
end
spec[ancestors.length...spec.length].each { |row| row[ancestors.last] = nil }
default_row[ancestors.last] = nil
end
# ...then rowspans
single_columns = []
spec.each_with_index do |row, i|
single_columns.each { |q| row.delete(q) }
row.each do |q, span|
if span == 1
row[q] = -(spec.length - i)
single_columns << q
end
end
end
spec
end
end