1
0

Displaying readouts is working

This commit is contained in:
cryptogopher 2019-12-14 01:25:20 +01:00
parent 96fe7db39d
commit 925def1674
10 changed files with 98 additions and 48 deletions

View File

@ -3,6 +3,7 @@ class MeasurementsController < ApplicationController
before_action :init_session_filters
before_action :find_project_by_project_id, only: [:index, :new, :create]
before_action :find_quantity, only: [:toggle_quantity]
before_action :find_measurement, only: [:edit, :update, :destroy, :retake, :readouts]
before_action :authorize
@ -60,6 +61,11 @@ class MeasurementsController < ApplicationController
prepare_readouts
end
def toggle_quantity
@quantity.toggle_primary!
prepare_readouts
end
private
def init_session_filters
@ -91,29 +97,15 @@ class MeasurementsController < ApplicationController
end
def prepare_measurements
@measurements = @project.measurements.includes(:source, :readouts)
@measurements, @formula_q = @project.measurements
.includes(:source, :readouts)
.filter(session[:m_filters])
end
def prepare_readouts
@quantities = @project.quantities.measurement.where(primary: true)
measurements, requested_r, extra_r, @formula_q = @project.measurements.includes(:source)
.filter(@project, session[:i_filters], @quantities)
@nutrients = {}
@extra_nutrients = {}
ingredients.each_with_index do |i, index|
@nutrients[i] = []
requested_n[index].each do |q_name, value|
amount, unitname = value
@nutrients[i] << [q_name, amount.nil? ? '-' : "#{amount} [#{unitname || '-'}]"]
end
@extra_nutrients[i] = []
extra_n[index].each do |q_name, value|
amount, unitname = value
@extra_nutrients[i] << [q_name, amount.nil? ? '-' : "#{amount} [#{unitname || '-'}]"]
end
end
@measurements, @requested_r, @extra_r, @formula_q = @project.measurements
.includes(:source)
.filter(session[:m_filters], @quantities)
end
end

View File

@ -7,6 +7,20 @@ module MeasurementsHelper
m.taken_at.getlocal.strftime("%R")
end
def format_value(value)
amount, unitname = value
amount.nil? ? '-' : "#{amount} [#{unitname || '-'}]"
end
def quantity_toggle_options
disabled = []
options = nested_set_options(@project.quantities.measurement) do |q|
disabled << q.id if q.primary
raw("#{'&ensp;' * q.level}#{q.name}")
end
options_for_select(options, disabled: disabled)
end
def quantity_options
nested_set_options(@project.quantities.measurement) do |q|
raw("#{'&ensp;' * q.level}#{q.name}")

View File

@ -0,0 +1,35 @@
<fieldset id="options" class="collapsible">
<legend onclick="toggleFieldset(this);"><%= l(:label_options) %></legend>
<div>
<%= form_tag toggle_quantity_project_measurements_path(@project),
id: 'quantity-toggle-form', name: 'quantity-toggle-form',
method: :post, remote: true do %>
<table>
<tr>
<td style="width:100%"></td>
<td>
<%= select_tag 'id', quantity_toggle_options %>
</td>
<td>
<%= submit_tag l(:button_add) %>
</td>
</tr>
</table>
<% end %>
</div>
</fieldset>
<table class="list" style="border:none; width:100%">
<tr>
<% total_width = 3 + @quantities.length %>
<td style="visibility: hidden; border: none; width:<%= 3 * 100/total_width%>%"></td>
<% @quantities.each do |q| %>
<td class="action" style="width:<%= 100/total_width %>%">
<%= link_to l(:button_hide),
toggle_quantity_project_measurements_path(@project, id: q.id),
{class: "icon icon-close", method: :post, remote: true} %>
</td>
<% end %>
</tr>
</table>

View File

@ -1,25 +1,25 @@
<% if @readouts.any? %>
<% if @measurements.any? %>
<%= render partial: 'measurements/options' %>
<table class="readouts list odd-even">
<thead>
<tr>
<% total_width = 3 + @quantities.length %>
<th style="width:<%= 3 * 100/total_width%>%"><%= l(:field_name) %></th>
<th style="width:<%= 3 * 100/total_width%>%"><%= l(:field_taken_at_date) %></th>
<% @quantities.each do |q| %>
<th style="width:<%= 100/total_width %>%"><%= q.name %></th>
<% end %>
</tr>
</thead>
<tbody>
<% @readouts.each do |m, values| %>
<% @measurements.each_with_index do |m, index| %>
<% row_class = "measurement #{cycle('odd', 'even')}" %>
<tr id="measurement-<%= m.id %>" class="primary <%= row_class %>">
<td class="date" style="cursor: pointer;" onclick="$(this).closest('tr').toggle(); $(this).closest('tr').nextUntil('tr.primary', 'tr').toggle(); return false;">
<span class="icon icon-bullet-closed"><%= format_datetime(m) %></span>
</td>
<% values.each do |*, value| %>
<td class="primary value"><%= value %></td>
<% @requested_r[index].each do |*, value| %>
<td class="primary value"><%= format_value(value) %></td>
<% end %>
</tr>
@ -27,19 +27,19 @@
<td class="date" style="cursor: pointer;" onclick="$(this).closest('tr').prev('tr.primary').toggle(); $(this).closest('tr').prev('tr.primary').nextUntil('tr.primary', 'tr').toggle(); return false;">
<span class="icon icon-bullet-closed"><%= format_datetime(m) %></span>
</td>
<% values.each do |q_name, *| %>
<% @requested_r[index].each do |q_name, *| %>
<td class="primary quantity"><%= q_name %></td>
<% end %>
</tr>
<tr class="<%= row_class %>" style="display:none">
<td class="space"></td>
<% values.each do |*, value| %>
<td class="primary value"><%= value %></td>
<% @requested_r[index].each do |*, value| %>
<td class="primary value"><%= format_value(value) %></td>
<% end %>
</tr>
<% extras = @extra_readouts[i] %>
<% extras.each_slice(@quantities.length).with_index do |values, index| %>
<% next unless @quantities.length > 0 %>
<% @extra_r[index].each_slice(@quantities.length).with_index do |values| %>
<tr class="extra <%= row_class %>" style="display:none">
<td class="space"></td>
<% values.each do |q_name, *| %>
@ -52,7 +52,7 @@
<tr class="extra <%= row_class %>" style="display:none">
<td class="space"></td>
<% values.each do |*, value| %>
<td class="extra value"><%= value %></td>
<td class="extra value"><%= format_value(value) %></td>
<% end %>
<% if @quantities.length > values.length %>
<td class="space" colspan="<%= @quantities.length-values.length %>"></td>

View File

@ -14,6 +14,6 @@
>
<%= @measurements.first.name %>
</h2>
<div id='measurements'>
<%= render partial: 'measurements/index' %>
<div id='readouts'>
<%= render partial: 'measurements/readouts' %>
</div>

View File

@ -0,0 +1,3 @@
$('div[id^=flash_]').remove();
$('#content').prepend('<%= escape_javascript(render_flash_messages) %>');
$('#readouts').html('<%= escape_javascript(render partial: 'measurements/readouts') %>');

View File

@ -1,8 +1,12 @@
table.list tr.quantity.primary td.name {font-weight: bold;}
table.nutrients.list td.name,
table.nutrients.list td.quantity {text-align: left; border-bottom: none;}
table.nutrients.list td.space {border-top: none; border-bottom: none;}
table.nutrients.list td.value {border-top: none;}
table.nutrients.list td.quantity,
table.readouts.list td.date,
table.readouts.list td.quantity {text-align: left; border-bottom: none;}
table.nutrients.list td.space,
table.readouts.list td.space {border-top: none; border-bottom: none;}
table.nutrients.list td.value,
table.readouts.list td.value {border-top: none;}
table.list tr.ingredient.hidden {opacity: 0.4}
table.list td.action,
table.list td.date,

View File

@ -10,6 +10,9 @@ resources :projects, shallow: true do
get 'retake'
get 'readouts'
end
collection do
post 'toggle_quantity'
end
end
resources :ingredients, only: [:index, :create, :destroy] do
post 'toggle', on: :member

View File

@ -24,7 +24,7 @@ Redmine::Plugin.register :body_tracking do
}, read: true
permission :manage_common, {
body_trackers: [:defaults],
measurements: [:new, :create, :edit, :update, :destroy, :retake],
measurements: [:new, :create, :edit, :update, :destroy, :retake, :toggle_quantity],
ingredients: [:create, :destroy, :toggle, :import, :toggle_nutrient_column],
sources: [:create, :destroy],
quantities: [:create, :edit, :update, :destroy, :toggle, :move],

View File

@ -1,6 +1,6 @@
module BodyTracking
module ItemsWithQuantities
def filter(filters, requested_q = Quantity.none)
def filter(filters, requested_q = nil)
items = all.where(filters[:scope])
if filters[:name].present?
@ -21,9 +21,9 @@ module BodyTracking
apply_formula = formula_q.present? && formula_q.valid?
result =
if !requested_q.empty? || apply_formula
if requested_q || apply_formula
computed = items.compute_quantities(requested_q, apply_formula && formula_q)
requested_q.present? ? computed : [computed[0]]
requested_q ? computed : [computed[0]]
else
[items]
end
@ -32,21 +32,20 @@ module BodyTracking
def compute_quantities(requested_q, filter_q = nil)
items = all
requested_q ||= Quantity.none
unchecked_q = requested_q.map { |q| [q, nil] }
unchecked_q << [filter_q, nil] if filter_q
subitems = Hash.new { |h,k| h[k] = {} }
subitems_scope = case proxy_association.klass
when Measurement
item_primary_key = :measurement_id
Readout.where(measurement: items)
when Ingredient
item_primary_key = :ingredient_id
Nutrient.where(ingredient: items)
end
item_class = proxy_association.klass
subitem_type = item_class.nested_attributes_options.keys.first.to_s
subitem_reflection = item_class.reflections[subitem_type]
subitem_class = subitem_reflection.klass
subitems_scope = subitem_class.where(subitem_reflection.options[:inverse_of] => items)
item_foreign_key = subitem_reflection.foreign_key
subitems_scope.includes(:quantity, :unit)
.order('quantities.lft')
.pluck(item_primary_key, 'quantities.name', :amount, 'units.shortname')
.pluck(item_foreign_key, 'quantities.name', :value, 'units.shortname')
.each { |item_id, q_name, a, u_id| subitems[q_name][item_id] = [a, u_id] }
extra_q = subitems.keys - requested_q.pluck(:name)