diff --git a/app/models/column_view.rb b/app/models/column_view.rb index 2c0715b..5cf2b0f 100644 --- a/app/models/column_view.rb +++ b/app/models/column_view.rb @@ -12,6 +12,7 @@ class ColumnView < ActiveRecord::Base column = self.quantities.find(q.id) self.quantites.destroy(column) rescue ActiveRecord::RecordNotFound - self.quantities.create!(quantity: q) + # Cannot 'create' association, as ColumnView (parent) may not be saved yet + self.quantities.append(q).save! end end diff --git a/app/models/measurement.rb b/app/models/measurement.rb index d8ccaf4..4a1126f 100644 --- a/app/models/measurement.rb +++ b/app/models/measurement.rb @@ -28,21 +28,7 @@ class Measurement < ActiveRecord::Base end end - # Copy/rename ColumnView on Measurement rename - after_save do - old_column_view = self.project.column_views - .find_by(name: self.name_was, domain: :measurement) - return unless old_column_view - - if self.project.measurements.exists?(name: self.name_was) - return unless old_column_view.quantities.exist? - self.column_view.quantities.create(old_column_view.quantities) - self.column_view.save! - else - old_column_view.name = self.name - old_column_view.save! - end - end, if: :name_changed? + after_save :cleanup_column_view, if: :name_changed? # Destroy ColumnView after last Measurement destruction after_destroy do @@ -52,7 +38,8 @@ class Measurement < ActiveRecord::Base end def column_view - self.project.column_views.find_or_create_by(name: self.name, domain: :measurement) + self.project.column_views + .find_or_create_by(name: self.name, domain: ColumnView.domains[:measurement]) end def toggle_hidden! @@ -72,4 +59,19 @@ class Measurement < ActiveRecord::Base def taken_at_time=(value) self.taken_at = Time.parse(value, self.taken_at) end + + private + + # Copy/rename ColumnView on Measurement rename + def cleanup_column_view + old_column_view = self.project.column_views + .find_by(name: self.name_was, domain: ColumnView.domains[:measurement]) + return unless old_column_view + + if self.project.measurements.exists?(name: self.name_was) + self.column_view.quantities.append(old_column_view.quantities).save! + else + old_column_view.update!(name: self.name) + end + end end diff --git a/app/views/ingredients/_options.html.erb b/app/views/ingredients/_options.html.erb index 1bb441d..5c1475f 100644 --- a/app/views/ingredients/_options.html.erb +++ b/app/views/ingredients/_options.html.erb @@ -9,7 +9,7 @@ - <%= select_tag 'id', toggle_column_options %> + <%= select_tag 'quantity_id', toggle_column_options %> <%= submit_tag l(:button_add) %> diff --git a/app/views/measurements/_options.html.erb b/app/views/measurements/_options.html.erb index bd1c9b6..d6e4ff3 100644 --- a/app/views/measurements/_options.html.erb +++ b/app/views/measurements/_options.html.erb @@ -9,7 +9,7 @@ - <%= select_tag 'id', toggle_column_options %> + <%= select_tag 'quantity_id', toggle_column_options %> <%= submit_tag l(:button_add) %> diff --git a/app/views/measurements/toggle_quantity.js.erb b/app/views/measurements/toggle_column.js.erb similarity index 100% rename from app/views/measurements/toggle_quantity.js.erb rename to app/views/measurements/toggle_column.js.erb diff --git a/db/migrate/001_create_schema.rb b/db/migrate/001_create_schema.rb index 096f65c..0aad9af 100644 --- a/db/migrate/001_create_schema.rb +++ b/db/migrate/001_create_schema.rb @@ -26,9 +26,9 @@ class CreateSchema < ActiveRecord::Migration t.integer :domain end - create_table :quantities_column_views do |t| - t.references :quantity + create_table :column_views_quantities do |t| t.references :column_view + t.references :quantity end create_table :sources do |t| diff --git a/lib/body_tracking/application_controller_patch.rb b/lib/body_tracking/application_controller_patch.rb index f3d9518..4ea9d52 100644 --- a/lib/body_tracking/application_controller_patch.rb +++ b/lib/body_tracking/application_controller_patch.rb @@ -3,15 +3,15 @@ module BodyTracking ApplicationController.class_eval do private - def find_quantity(id = params[:id]) - @quantity = Quantity.find(id) + def find_quantity(id = :id) + @quantity = Quantity.find(params[id]) @project = @quantity.project rescue ActiveRecord::RecordNotFound render_404 end def find_quantity_by_quantity_id - find_quantity(params[:quantity_id]) + find_quantity(:quantity_id) end end end diff --git a/lib/body_tracking/project_patch.rb b/lib/body_tracking/project_patch.rb index 452f37d..4d39353 100644 --- a/lib/body_tracking/project_patch.rb +++ b/lib/body_tracking/project_patch.rb @@ -10,7 +10,8 @@ module BodyTracking has_many :units, dependent: :destroy def nutrients_column_view - self.column_views.find_or_create_by(name: 'Nutrients', domain: :diet) + self.column_views + .find_or_create_by(name: 'Nutrients', domain: ColumnView.domains[:diet]) end end end