diff --git a/app/models/goal.rb b/app/models/goal.rb index 5af6f54..4e8730b 100644 --- a/app/models/goal.rb +++ b/app/models/goal.rb @@ -7,9 +7,12 @@ class Goal < ActiveRecord::Base has_many :quantities, -> { order "lft" }, through: :target_exposures validates :target_exposures, presence: true + validates :is_binding, uniqueness: {scope: :project_id}, if: :is_binding? validates :name, presence: true, uniqueness: {scope: :project_id} - def is_binding? - self == project.goals.binding + after_initialize do + if new_record? + self.is_binding = false if self.is_binding.nil? + end end end diff --git a/app/views/targets/_form.html.erb b/app/views/targets/_form.html.erb index dc6b237..645706e 100644 --- a/app/views/targets/_form.html.erb +++ b/app/views/targets/_form.html.erb @@ -3,7 +3,7 @@ <% @targets.group_by(&:goal).each do |goal, targets| %>
- <% if goal.persisted? %> + <% if goal.persisted? || goal.is_binding? %>

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

<%#= t '.effective_from' %>

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

diff --git a/app/views/targets/_index.html.erb b/app/views/targets/_index.html.erb index 7edd1fb..5c667de 100644 --- a/app/views/targets/_index.html.erb +++ b/app/views/targets/_index.html.erb @@ -9,33 +9,35 @@ <%= error_messages_for *formulas %> - + <% total_width = 3 + @quantities.length %> <% header = quantities_table_header(@quantities) %> <% header.each_with_index do |row, i| %> <% if i == 0 %> - + <% end %> <% row.each do |q, span| %> - + <% end %> <% if i == 0 %> - + <% end %> <% end %> + + <% @targets_by_date.each do |date, targets| %> <% row_class = "date #{cycle('odd', 'even')}" %> diff --git a/assets/stylesheets/body_tracking.css b/assets/stylesheets/body_tracking.css index b02e4c4..c372512 100644 --- a/assets/stylesheets/body_tracking.css +++ b/assets/stylesheets/body_tracking.css @@ -5,14 +5,22 @@ table.list tr.food.hidden {opacity: 0.4} table.list .date, table.list .name, table.list .quantity {text-align: left;} -/* TODO: merge with .closable */ -table.list .quantityhead { +/* TODO: merge with .closable and/or remove .closable */ +/* TODO: replace .quantityhead(empty) with th.quantity/.empty */ +table.list .quantityhead, table.list th.quantity { text-align: center; position: relative; padding: 2px 0; border-bottom: none; } table.list .quantityheadempty {border-top: none; border-bottom: none;} +table.list th.quantity { + text-align: center; + position: relative; + padding: 2px 0; + border-bottom: none; +} +table.list th.empty {border-top: none;} table.list .action, table.list .value {text-align: right; padding-right: 2px;} diff --git a/db/migrate/001_create_schema.rb b/db/migrate/001_create_schema.rb index 807f14d..fd786e5 100644 --- a/db/migrate/001_create_schema.rb +++ b/db/migrate/001_create_schema.rb @@ -74,6 +74,7 @@ class CreateSchema < t.integer :group t.references :source t.string :source_ident + # TODO: rename to is_hidden t.boolean :hidden t.decimal :ready_amount, precision: 12, scale: 6 t.timestamps null: false @@ -96,6 +97,7 @@ class CreateSchema < create_table :goals do |t| t.references :project + t.boolean :is_binding t.string :name t.text :description t.timestamps null: false diff --git a/lib/body_tracking/project_patch.rb b/lib/body_tracking/project_patch.rb index cc9f59c..d5f82b6 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_initialize_by(is_binding: true) { |g| g.name = "- binding -" } end end has_many :targets, through: :goals diff --git a/test/application_system_test_case.rb b/test/application_system_test_case.rb index 1899e4a..dd14066 100644 --- a/test/application_system_test_case.rb +++ b/test/application_system_test_case.rb @@ -21,7 +21,7 @@ class BodyTrackingSystemTestCase < ApplicationSystemTestCase end fixtures :projects - plugin_fixtures :quantities, :units, :exposures, :targets, :quantity_values + plugin_fixtures :quantities, :units, :goals, :exposures, :targets, :quantity_values include AbstractController::Translation diff --git a/test/fixtures/exposures.yml b/test/fixtures/exposures.yml index ea132e3..628050d 100644 --- a/test/fixtures/exposures.yml +++ b/test/fixtures/exposures.yml @@ -1,4 +1,4 @@ exposures_01: view_type: Goal - view_id: <%= Project.find(1).goals.binding.id %> + view: goals_binding quantity: quantities_energy diff --git a/test/fixtures/goals.yml b/test/fixtures/goals.yml new file mode 100644 index 0000000..5e57cd2 --- /dev/null +++ b/test/fixtures/goals.yml @@ -0,0 +1,4 @@ +goals_binding: + project_id: 1 + is_binding: true + name: '- binding -' diff --git a/test/fixtures/quantities.yml b/test/fixtures/quantities.yml index be4b47b..c081970 100644 --- a/test/fixtures/quantities.yml +++ b/test/fixtures/quantities.yml @@ -1,5 +1,5 @@ quantities_energy: - project: projects_001 + project_id: 1 domain: diet parent: null lft: 1 diff --git a/test/fixtures/targets.yml b/test/fixtures/targets.yml index 9354f70..f52a938 100644 --- a/test/fixtures/targets.yml +++ b/test/fixtures/targets.yml @@ -1,4 +1,4 @@ targets_01: - goal_id: <%= Project.find(1).goals.binding.id %> + goal: goals_binding condition: '==' effective_from: <%= 1.week.ago.to_s(:db) %> diff --git a/test/fixtures/units.yml b/test/fixtures/units.yml index 00f561a..0928441 100644 --- a/test/fixtures/units.yml +++ b/test/fixtures/units.yml @@ -1,14 +1,14 @@ units_gram: - project: projects_001 + project_id: 1 name: 'gram' shortname: 'g' units_percent: - project: projects_001 + project_id: 1 name: 'percent' shortname: '%' units_kcal: - project: projects_001 + project_id: 1 name: 'kilocalorie' shortname: 'kcal' diff --git a/test/system/targets_test.rb b/test/system/targets_test.rb index b0c2739..6915925 100644 --- a/test/system/targets_test.rb +++ b/test/system/targets_test.rb @@ -18,11 +18,14 @@ class TargetsTest < BodyTrackingSystemTestCase assert_not_equal 0, @project1.targets.count visit project_targets_path(@project1) assert_current_path project_targets_path(@project1) - assert_selector 'table#targets tr', count: @project1.targets.count + assert_selector 'table#targets tbody tr', count: @project1.targets.count end def test_index_without_targets #assert_equal 0, @project1.targets.count #assert_selector 'div#targets', visible: :yes, exact_text: l(:label_no_data) end + + def test_create_saves_binding_goal_if_nonexistent + end end
<%= l(:field_effective_from) %><%= l(:field_effective_from) %> 0 %> <%= "rowspan=#{-span}" if span && span < 0 %> style="width: <%= (span && span > 0 ? span : 1) * 100/total_width %>%;" title="<%= q.description %>"> <%= q.name if span %> - <%= l(:field_action) %><%= l(:field_action) %>