1
0

Target form WIP

This commit is contained in:
cryptogopher 2020-07-02 18:59:55 +02:00
parent 0df2c6ec4f
commit 5b83860ed7
6 changed files with 24 additions and 20 deletions

View File

@ -3,4 +3,8 @@ class Goal < ActiveRecord::Base
has_many :targets, inverse_of: :goal, dependent: :destroy has_many :targets, inverse_of: :goal, dependent: :destroy
validates :name, presence: true, uniqueness: {scope: :project_id} validates :name, presence: true, uniqueness: {scope: :project_id}
def is_binding?
self == project.goals.binding
end
end end

View File

@ -13,21 +13,20 @@ class Target < ActiveRecord::Base
errors.add(:thresholds, :count_mismatch) unless thresholds.count == arity errors.add(:thresholds, :count_mismatch) unless thresholds.count == arity
errors.add(:thresholds, :quantity_mismatch) if thresholds.to_a.uniq(&:quantity) != 1 errors.add(:thresholds, :quantity_mismatch) if thresholds.to_a.uniq(&:quantity) != 1
end end
validates :condition, inclusion: {in: CONDITIONS } validates :condition, inclusion: {in: CONDITIONS}
validates :scope, inclusion: {in: [:day], if: -> { thresholds.first.domain == :diet }} validates :scope, inclusion: {in: [:ingredient, :meal, :day],
if: -> { thresholds.first.quantity.domain == :diet }}
validates :effective_from, presence: {unless: :is_binding?}, absence: {if: :is_binding?} validates :effective_from, presence: {unless: :is_binding?}, absence: {if: :is_binding?}
after_initialize do after_initialize do
if new_record? if new_record?
self.condition = CONDITIONS.first self.condition = CONDITIONS.first
self.effective_from = Date.current if is_binding?
end end
end end
delegate :is_binding?, to: :goal
def arity def arity
BigDecimal.method(condition).arity BigDecimal.method(condition).arity
end end
def is_binding?
goal == goal.project.goals.binding
end
end end

View File

@ -1,6 +1,4 @@
<div>
<%= fields_for 'target[goal_attributes]', goal do |ff| %> <%= fields_for 'target[goal_attributes]', goal do |ff| %>
<p>
<label><%= l(:field_goal) %><span class="required"> *</span></label> <label><%= l(:field_goal) %><span class="required"> *</span></label>
<%= ff.select :id, <%= ff.select :id,
options_from_collection_for_select(@project.goals, :id, :name, goal.id), options_from_collection_for_select(@project.goals, :id, :name, goal.id),
@ -11,17 +9,15 @@
dataType: 'script' dataType: 'script'
}); });
return false;" %> return false;" %>
<%= link_to l(:button_edit), '#', <%= link_to l(:button_add), '#',
onclick: "var goal_id = $('#target_goal_attributes_id').val(); onclick: "var goal_id = $('#target_goal_attributes_id').val();
$.ajax({ $.ajax({
url: '#{edit_goal_path(id: :goal_id)}'.replace('goal_id', goal_id), url: '#{edit_goal_path(id: :goal_id)}'.replace('goal_id', goal_id),
dataType: 'script' dataType: 'script'
}); });
return false;", return false;",
class: 'icon icon-edit' %> class: 'icon icon-add' %>
</p>
<% end %> <% end %>
<% if goal.description? %> <% if goal.description? %>
<p style='white-space: pre-wrap;' ><%= goal.description %></p> <p style='white-space: pre-wrap;' ><%= goal.description %></p>
<% end %> <% end %>
</div>

View File

@ -3,8 +3,9 @@
<div class="box"> <div class="box">
<div id='goal-form' class="tabular"> <div id='goal-form' class="tabular">
<% if @target.goal.persisted? %> <% if @target.goal.persisted? %>
<%= render partial: 'goals/show_form', locals: {goal: @target.goal} %> <p><%= render partial: 'goals/show_form', locals: {goal: @target.goal} %></p>
<%= f.date_field(:effective_from, required: true) if @target.is_binding? %> <%#= t '.effective_from' %>
<p><%= f.date_field :effective_from, disabled: !@target.is_binding? %></p>
<% else %> <% else %>
<%= render partial: 'goals/form', locals: {goal: @target.goal} %> <%= render partial: 'goals/form', locals: {goal: @target.goal} %>
<% end %> <% end %>
@ -17,7 +18,7 @@
<% if index == 0 %> <% if index == 0 %>
<%= ff.select :quantity_id, quantity_options, <%= ff.select :quantity_id, quantity_options,
{include_blank: true, required: true, label: :field_target} %> {include_blank: true, required: true, label: :field_target} %>
<%= f.select :condition, condition_options, required: true %> <%= f.select :condition, condition_options, required: true, label: '' %>
<% end %> <% end %>
<%= ff.hidden_field :id %> <%= ff.hidden_field :id %>
<%= ff.number_field :value, {size: 8, step: :any, label: ''} %> <%= ff.number_field :value, {size: 8, step: :any, label: ''} %>
@ -26,13 +27,13 @@
<% end %> <% end %>
<% end %> <% end %>
<% end %> <% end %>
<%= link_to t(".button_delete_target"), '#', <%= link_to t(".button_delete_target"), '#', class: 'icon icon-del',
class: 'icon icon-del',
onclick: "deleteTarget(); return false;" %> onclick: "deleteTarget(); return false;" %>
</p> </p>
<%= link_to t(".button_new_target"), '#', <p>
class: 'icon icon-add', <%= link_to t(".button_new_target"), '#', class: 'icon icon-add',
onclick: 'newTarget(); return false;' %> onclick: 'newTarget(); return false;' %>
</p>
</div> </div>
</div> </div>

View File

@ -1,6 +1,9 @@
# English strings go here for Rails i18n # English strings go here for Rails i18n
en: en:
body_trackers_menu_caption: 'Body trackers' body_trackers_menu_caption: 'Body trackers'
field_goal: 'Goal'
field_target: 'Target'
field_effective_from: 'Effective from'
field_eaten_at_date: 'Eaten at' field_eaten_at_date: 'Eaten at'
field_ingredients: 'Ingredients' field_ingredients: 'Ingredients'
field_measurement_routine: 'Routine' field_measurement_routine: 'Routine'
@ -92,6 +95,7 @@ en:
form: form:
button_new_target: 'Add target' button_new_target: 'Add target'
button_delete_target: 'Delete' button_delete_target: 'Delete'
effective_from: ', effective from:'
new_form: new_form:
heading_new_target: 'New target' heading_new_target: 'New target'
index: index:

View File

@ -31,7 +31,7 @@ module BodyTracking::ProjectPatch
has_many :goals, dependent: :destroy do has_many :goals, dependent: :destroy do
def binding def binding
find_or_create_by(name: "binding") find_or_create_by(name: "- binding -")
end end
end end
has_many :targets, through: :goals has_many :targets, through: :goals