From 09e27eb754fa4114ccb5651deb00432d80159c02 Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Sun, 21 Feb 2021 19:41:24 +0100 Subject: [PATCH] List binding goal targets --- app/controllers/targets_controller.rb | 6 +++--- app/models/goal.rb | 2 +- app/models/quantity_value.rb | 5 ++++- app/views/body_trackers/index.html.erb | 2 +- app/views/foods/index.html.erb | 2 +- app/views/foods/nutrients.html.erb | 2 +- app/views/goals/_index.html.erb | 2 +- app/views/goals/index.html.erb | 2 +- app/views/meals/index.html.erb | 2 +- app/views/measurements/index.html.erb | 2 +- app/views/measurements/readouts.html.erb | 3 +-- app/views/quantities/index.html.erb | 2 +- app/views/sources/index.html.erb | 2 +- app/views/targets/index.html.erb | 2 +- app/views/units/index.html.erb | 2 +- config/locales/en.yml | 5 ++++- config/routes.rb | 1 + lib/body_tracking/project_patch.rb | 2 +- 18 files changed, 26 insertions(+), 20 deletions(-) diff --git a/app/controllers/targets_controller.rb b/app/controllers/targets_controller.rb index b6155e6..ef41258 100644 --- a/app/controllers/targets_controller.rb +++ b/app/controllers/targets_controller.rb @@ -5,13 +5,13 @@ class TargetsController < ApplicationController include Concerns::Finders - before_action :find_binding_goal_by_project_id, only: [:index, :new, :edit] + before_action :find_binding_goal_by_project_id, only: [:new, :edit] before_action :find_project_by_project_id, only: [:create, :subthresholds] before_action :find_quantity_by_quantity_id, only: [:toggle_exposure] #, if: ->{ params[:project_id].present? } #before_action :find_goal, only: [:index, :new], # unless: -> { @goal } - before_action :find_goal, only: [:toggle_exposure] + before_action :find_goal, only: [:index, :toggle_exposure] before_action :authorize #before_action :set_view_params @@ -104,7 +104,7 @@ class TargetsController < ApplicationController @quantities = @goal.quantities.includes(:formula) @targets_by_date = Hash.new { |h,k| h[k] = {} } - @project.targets.includes(:item, thresholds: [:quantity]).reject(&:new_record?) + @goal.targets.includes(:item, thresholds: [:quantity]).reject(&:new_record?) .each { |t| @targets_by_date[t.effective_from][t.thresholds.first.quantity] = t } end diff --git a/app/models/goal.rb b/app/models/goal.rb index 2dfc7d0..a211ae2 100644 --- a/app/models/goal.rb +++ b/app/models/goal.rb @@ -14,7 +14,7 @@ class Goal < ActiveRecord::Base after_initialize do if new_record? self.is_binding = false if self.is_binding.nil? - self.targets.new if self.targets.empty? + self.targets.new if !self.is_binding && self.targets.empty? end end diff --git a/app/models/quantity_value.rb b/app/models/quantity_value.rb index 8925f2b..c95097b 100644 --- a/app/models/quantity_value.rb +++ b/app/models/quantity_value.rb @@ -5,7 +5,10 @@ class QuantityValue < ActiveRecord::Base # to allow for accessing registry item without knowing QuantityValue (subitem) # type, e.g. qv.registry.completed_at belongs_to :registry, polymorphic: true - belongs_to :quantity, ->(qv) { where(domain: qv.class::DOMAIN) }, required: true + belongs_to :quantity, required: true + validate do + errors.add(:quantity, :domain_mismatch) unless self.class::DOMAIN == quantity.domain + end belongs_to :unit, required: true # Uniqueness is checked exclusively on the other end of association level. diff --git a/app/views/body_trackers/index.html.erb b/app/views/body_trackers/index.html.erb index c4eb811..5150dc9 100644 --- a/app/views/body_trackers/index.html.erb +++ b/app/views/body_trackers/index.html.erb @@ -1 +1 @@ -<%= title t(:body_trackers_title), t(".heading") %> +<%= title t(".heading") %> diff --git a/app/views/foods/index.html.erb b/app/views/foods/index.html.erb index 9d6fe0e..da85ccb 100644 --- a/app/views/foods/index.html.erb +++ b/app/views/foods/index.html.erb @@ -9,7 +9,7 @@
-<%= title t(:body_trackers_title), t(".heading") %> +<%= title t(".heading") %>
<%= render partial: 'foods/index' %>
diff --git a/app/views/foods/nutrients.html.erb b/app/views/foods/nutrients.html.erb index 9acabf9..a676955 100644 --- a/app/views/foods/nutrients.html.erb +++ b/app/views/foods/nutrients.html.erb @@ -9,7 +9,7 @@
-<%= title t(:body_trackers_title), t(".heading") %> +<%= title t(".heading") %>
<%= render partial: 'foods/nutrients' %>
diff --git a/app/views/goals/_index.html.erb b/app/views/goals/_index.html.erb index b090919..dd86405 100644 --- a/app/views/goals/_index.html.erb +++ b/app/views/goals/_index.html.erb @@ -12,7 +12,7 @@
- <%= checked_image g.is_binding %><%= link_to g.name, g %> + <%= checked_image g.is_binding %><%= link_to g.name, targets_goal_path(g) %>
<%# TODO: display # of active targets/targeted quantities %> diff --git a/app/views/goals/index.html.erb b/app/views/goals/index.html.erb index f0dc038..248b60e 100644 --- a/app/views/goals/index.html.erb +++ b/app/views/goals/index.html.erb @@ -7,7 +7,7 @@
-<%= title t(:body_trackers_title), t(".heading") %> +<%= title t(".heading") %>
<%= render partial: 'goals/index' %>
diff --git a/app/views/meals/index.html.erb b/app/views/meals/index.html.erb index 247ad18..c72d99a 100644 --- a/app/views/meals/index.html.erb +++ b/app/views/meals/index.html.erb @@ -5,7 +5,7 @@
-<%= title t(:body_trackers_title), t(".heading") %> +<%= title t(".heading") %>
<%= render partial: 'meals/index' %>
diff --git a/app/views/measurements/index.html.erb b/app/views/measurements/index.html.erb index 2eb7178..d184827 100644 --- a/app/views/measurements/index.html.erb +++ b/app/views/measurements/index.html.erb @@ -5,7 +5,7 @@
-<%= title t(:body_trackers_title), t(".heading") %> +<%= title t(".heading") %>
<%= render partial: 'measurements/index' %>
diff --git a/app/views/measurements/readouts.html.erb b/app/views/measurements/readouts.html.erb index 4ba732e..0c1ba5f 100644 --- a/app/views/measurements/readouts.html.erb +++ b/app/views/measurements/readouts.html.erb @@ -5,8 +5,7 @@
-<%= title t(:body_trackers_title), - [t("measurements.index.heading"), project_measurements_path(@project)], +<%= title [t("measurements.index.heading"), project_measurements_path(@project)], @routine.name %>
<%= render partial: 'measurements/readouts' %> diff --git a/app/views/quantities/index.html.erb b/app/views/quantities/index.html.erb index 38f3d0f..f2bfb20 100644 --- a/app/views/quantities/index.html.erb +++ b/app/views/quantities/index.html.erb @@ -8,7 +8,7 @@
-<%= title t(:body_trackers_title), t(".heading") %> +<%= title t(".heading") %>
<%= render partial: 'quantities/index' %>
diff --git a/app/views/sources/index.html.erb b/app/views/sources/index.html.erb index aeb2e4a..86eb926 100644 --- a/app/views/sources/index.html.erb +++ b/app/views/sources/index.html.erb @@ -19,7 +19,7 @@
-<%= title t(:body_trackers_title), t(".heading") %> +<%= title t(".heading") %> <% if @sources.any? { |s| s.persisted? } %> diff --git a/app/views/targets/index.html.erb b/app/views/targets/index.html.erb index 27fa1a3..4fa2921 100644 --- a/app/views/targets/index.html.erb +++ b/app/views/targets/index.html.erb @@ -5,7 +5,7 @@
-<%= title t(:body_trackers_title), t(".heading") %> +<%= title [t("goals.index.heading"), project_goals_path(@project)], @goal.name %>
<%= render partial: 'targets/index' %>
diff --git a/app/views/units/index.html.erb b/app/views/units/index.html.erb index 7b1be68..36a7191 100644 --- a/app/views/units/index.html.erb +++ b/app/views/units/index.html.erb @@ -19,7 +19,7 @@
-<%= title t(:body_trackers_title), t(".heading") %> +<%= title t(".heading") %> <% if @units.any? { |u| u.persisted? } %>
diff --git a/config/locales/en.yml b/config/locales/en.yml index b336c54..2ce1cbb 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1,7 +1,6 @@ # English strings go here for Rails i18n en: body_trackers_menu_caption: 'Body trackers' - body_trackers_title: 'Body trackers' field_goal: 'Goal' field_target: 'Target' field_effective_from: 'Effective from' @@ -50,6 +49,10 @@ en: attributes: nutrients: duplicated_quantity: 'you can define each nutrient quantity only once per food' + threshold: + attributes: + quantity: + domain_mismatch: 'domain has to match model domain' quantity: attributes: parent: diff --git a/config/routes.rb b/config/routes.rb index 41b6cc4..14a4af5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -9,6 +9,7 @@ resources :projects, shallow: true do end resources :goals do member do + get 'targets', controller: :targets, action: :index, as: :targets post 'toggle_exposure', controller: :targets end end diff --git a/lib/body_tracking/project_patch.rb b/lib/body_tracking/project_patch.rb index cf1989a..3a51e74 100644 --- a/lib/body_tracking/project_patch.rb +++ b/lib/body_tracking/project_patch.rb @@ -31,7 +31,7 @@ module BodyTracking::ProjectPatch has_many :goals, dependent: :destroy do def binding - find_or_create_by(is_binding: true) do |goal| + find_or_create_by!(is_binding: true) do |goal| goal.name = I18n.t('goals.binding.name') goal.description = I18n.t('goals.binding.description') end