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)
|
column = self.quantities.find(q.id)
|
||||||
self.quantites.destroy(column)
|
self.quantites.destroy(column)
|
||||||
rescue ActiveRecord::RecordNotFound
|
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
|
||||||
end
|
end
|
||||||
|
@ -28,21 +28,7 @@ class Measurement < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Copy/rename ColumnView on Measurement rename
|
after_save :cleanup_column_view, if: :name_changed?
|
||||||
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?
|
|
||||||
|
|
||||||
# Destroy ColumnView after last Measurement destruction
|
# Destroy ColumnView after last Measurement destruction
|
||||||
after_destroy do
|
after_destroy do
|
||||||
@ -52,7 +38,8 @@ class Measurement < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def column_view
|
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
|
end
|
||||||
|
|
||||||
def toggle_hidden!
|
def toggle_hidden!
|
||||||
@ -72,4 +59,19 @@ class Measurement < ActiveRecord::Base
|
|||||||
def taken_at_time=(value)
|
def taken_at_time=(value)
|
||||||
self.taken_at = Time.parse(value, self.taken_at)
|
self.taken_at = Time.parse(value, self.taken_at)
|
||||||
end
|
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
|
end
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
<tr>
|
<tr>
|
||||||
<td style="width:100%"></td>
|
<td style="width:100%"></td>
|
||||||
<td>
|
<td>
|
||||||
<%= select_tag 'id', toggle_column_options %>
|
<%= select_tag 'quantity_id', toggle_column_options %>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<%= submit_tag l(:button_add) %>
|
<%= submit_tag l(:button_add) %>
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
<tr>
|
<tr>
|
||||||
<td style="width:100%"></td>
|
<td style="width:100%"></td>
|
||||||
<td>
|
<td>
|
||||||
<%= select_tag 'id', toggle_column_options %>
|
<%= select_tag 'quantity_id', toggle_column_options %>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<%= submit_tag l(:button_add) %>
|
<%= submit_tag l(:button_add) %>
|
||||||
|
@ -26,9 +26,9 @@ class CreateSchema < ActiveRecord::Migration
|
|||||||
t.integer :domain
|
t.integer :domain
|
||||||
end
|
end
|
||||||
|
|
||||||
create_table :quantities_column_views do |t|
|
create_table :column_views_quantities do |t|
|
||||||
t.references :quantity
|
|
||||||
t.references :column_view
|
t.references :column_view
|
||||||
|
t.references :quantity
|
||||||
end
|
end
|
||||||
|
|
||||||
create_table :sources do |t|
|
create_table :sources do |t|
|
||||||
|
@ -3,15 +3,15 @@ module BodyTracking
|
|||||||
ApplicationController.class_eval do
|
ApplicationController.class_eval do
|
||||||
private
|
private
|
||||||
|
|
||||||
def find_quantity(id = params[:id])
|
def find_quantity(id = :id)
|
||||||
@quantity = Quantity.find(id)
|
@quantity = Quantity.find(params[id])
|
||||||
@project = @quantity.project
|
@project = @quantity.project
|
||||||
rescue ActiveRecord::RecordNotFound
|
rescue ActiveRecord::RecordNotFound
|
||||||
render_404
|
render_404
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_quantity_by_quantity_id
|
def find_quantity_by_quantity_id
|
||||||
find_quantity(params[:quantity_id])
|
find_quantity(:quantity_id)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -10,7 +10,8 @@ module BodyTracking
|
|||||||
has_many :units, dependent: :destroy
|
has_many :units, dependent: :destroy
|
||||||
|
|
||||||
def nutrients_column_view
|
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
|
end
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user