diff --git a/app/models/goal.rb b/app/models/goal.rb index 051b921..1ad3f58 100644 --- a/app/models/goal.rb +++ b/app/models/goal.rb @@ -3,4 +3,8 @@ class Goal < ActiveRecord::Base has_many :targets, inverse_of: :goal, dependent: :destroy validates :name, presence: true, uniqueness: {scope: :project_id} + + def is_binding? + self == project.goals.binding + end end diff --git a/app/models/target.rb b/app/models/target.rb index ac9a008..7569e1a 100644 --- a/app/models/target.rb +++ b/app/models/target.rb @@ -13,21 +13,20 @@ class Target < ActiveRecord::Base errors.add(:thresholds, :count_mismatch) unless thresholds.count == arity errors.add(:thresholds, :quantity_mismatch) if thresholds.to_a.uniq(&:quantity) != 1 end - validates :condition, inclusion: {in: CONDITIONS } - validates :scope, inclusion: {in: [:day], if: -> { thresholds.first.domain == :diet }} + validates :condition, inclusion: {in: CONDITIONS} + validates :scope, inclusion: {in: [:ingredient, :meal, :day], + if: -> { thresholds.first.quantity.domain == :diet }} validates :effective_from, presence: {unless: :is_binding?}, absence: {if: :is_binding?} after_initialize do if new_record? self.condition = CONDITIONS.first + self.effective_from = Date.current if is_binding? end end + delegate :is_binding?, to: :goal def arity BigDecimal.method(condition).arity end - - def is_binding? - goal == goal.project.goals.binding - end end diff --git a/app/views/goals/_show_form.html.erb b/app/views/goals/_show_form.html.erb index 8d3fc67..121eb9d 100644 --- a/app/views/goals/_show_form.html.erb +++ b/app/views/goals/_show_form.html.erb @@ -1,6 +1,4 @@ -
<%= fields_for 'target[goal_attributes]', goal do |ff| %> -

<%= ff.select :id, options_from_collection_for_select(@project.goals, :id, :name, goal.id), @@ -11,17 +9,15 @@ dataType: 'script' }); return false;" %> - <%= link_to l(:button_edit), '#', + <%= link_to l(:button_add), '#', onclick: "var goal_id = $('#target_goal_attributes_id').val(); $.ajax({ url: '#{edit_goal_path(id: :goal_id)}'.replace('goal_id', goal_id), dataType: 'script' }); return false;", - class: 'icon icon-edit' %> -

+ class: 'icon icon-add' %> <% end %> <% if goal.description? %>

<%= goal.description %>

<% end %> -
diff --git a/app/views/targets/_form.html.erb b/app/views/targets/_form.html.erb index 250a6b1..2fe4acd 100644 --- a/app/views/targets/_form.html.erb +++ b/app/views/targets/_form.html.erb @@ -3,8 +3,9 @@
<% if @target.goal.persisted? %> - <%= render partial: 'goals/show_form', locals: {goal: @target.goal} %> - <%= f.date_field(:effective_from, required: true) if @target.is_binding? %> +

<%= render partial: 'goals/show_form', locals: {goal: @target.goal} %>

+ <%#= t '.effective_from' %> +

<%= f.date_field :effective_from, disabled: !@target.is_binding? %>

<% else %> <%= render partial: 'goals/form', locals: {goal: @target.goal} %> <% end %> @@ -17,7 +18,7 @@ <% if index == 0 %> <%= ff.select :quantity_id, quantity_options, {include_blank: true, required: true, label: :field_target} %> - <%= f.select :condition, condition_options, required: true %> + <%= f.select :condition, condition_options, required: true, label: '' %> <% end %> <%= ff.hidden_field :id %> <%= ff.number_field :value, {size: 8, step: :any, label: ''} %> @@ -26,13 +27,13 @@ <% end %> <% end %> <% end %> - <%= link_to t(".button_delete_target"), '#', - class: 'icon icon-del', + <%= link_to t(".button_delete_target"), '#', class: 'icon icon-del', onclick: "deleteTarget(); return false;" %>

- <%= link_to t(".button_new_target"), '#', - class: 'icon icon-add', +

+ <%= link_to t(".button_new_target"), '#', class: 'icon icon-add', onclick: 'newTarget(); return false;' %> +

diff --git a/config/locales/en.yml b/config/locales/en.yml index 65358d9..d7e4cbc 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1,6 +1,9 @@ # English strings go here for Rails i18n en: body_trackers_menu_caption: 'Body trackers' + field_goal: 'Goal' + field_target: 'Target' + field_effective_from: 'Effective from' field_eaten_at_date: 'Eaten at' field_ingredients: 'Ingredients' field_measurement_routine: 'Routine' @@ -92,6 +95,7 @@ en: form: button_new_target: 'Add target' button_delete_target: 'Delete' + effective_from: ', effective from:' new_form: heading_new_target: 'New target' index: diff --git a/lib/body_tracking/project_patch.rb b/lib/body_tracking/project_patch.rb index 4b20c8b..cc9f59c 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(name: "binding") + find_or_create_by(name: "- binding -") end end has_many :targets, through: :goals