ColumnView fixes
This commit is contained in:
parent
0c0ca1d286
commit
530dba0612
@ -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
|
||||
|
@ -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
|
||||
|
@ -9,7 +9,7 @@
|
||||
<tr>
|
||||
<td style="width:100%"></td>
|
||||
<td>
|
||||
<%= select_tag 'id', toggle_column_options %>
|
||||
<%= select_tag 'quantity_id', toggle_column_options %>
|
||||
</td>
|
||||
<td>
|
||||
<%= submit_tag l(:button_add) %>
|
||||
|
@ -9,7 +9,7 @@
|
||||
<tr>
|
||||
<td style="width:100%"></td>
|
||||
<td>
|
||||
<%= select_tag 'id', toggle_column_options %>
|
||||
<%= select_tag 'quantity_id', toggle_column_options %>
|
||||
</td>
|
||||
<td>
|
||||
<%= submit_tag l(:button_add) %>
|
||||
|
@ -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|
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user