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
validates :name, presence: true, uniqueness: {scope: :project_id}
def is_binding?
self == project.goals.binding
end
end

View File

@ -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

View File

@ -1,6 +1,4 @@
<div>
<%= fields_for 'target[goal_attributes]', goal do |ff| %>
<p>
<label><%= l(:field_goal) %><span class="required"> *</span></label>
<%= 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' %>
</p>
class: 'icon icon-add' %>
<% end %>
<% if goal.description? %>
<p style='white-space: pre-wrap;' ><%= goal.description %></p>
<% end %>
</div>

View File

@ -3,8 +3,9 @@
<div class="box">
<div id='goal-form' class="tabular">
<% if @target.goal.persisted? %>
<%= render partial: 'goals/show_form', locals: {goal: @target.goal} %>
<%= f.date_field(:effective_from, required: true) if @target.is_binding? %>
<p><%= render partial: 'goals/show_form', locals: {goal: @target.goal} %></p>
<%#= t '.effective_from' %>
<p><%= f.date_field :effective_from, disabled: !@target.is_binding? %></p>
<% 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;" %>
</p>
<%= link_to t(".button_new_target"), '#',
class: 'icon icon-add',
<p>
<%= link_to t(".button_new_target"), '#', class: 'icon icon-add',
onclick: 'newTarget(); return false;' %>
</p>
</div>
</div>

View File

@ -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:

View File

@ -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