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 @@ -
<%= 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 %> -<%= 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;' %> +