Added MeasurementRoutine as a nested Measurement model
Updated ItemsWithQuantities to work with MeasurementRoutine Replaced ColumnViews HABTM with polymorphic HMT Added Measurement notes Added destroy restrictions on Quantity Replaced BodyTrackingPluginController with Finders concern Removed 'body_trackers' prefix from paths Unified styling for textarea
This commit is contained in:
@@ -1,12 +1,18 @@
|
||||
module BodyTracking
|
||||
module ItemsWithQuantities
|
||||
QUANTITY_DOMAINS = {
|
||||
Measurement => :measurement,
|
||||
Ingredient => :diet
|
||||
}
|
||||
VALUE_COLUMNS = {
|
||||
Measurement => :value,
|
||||
Ingredient => :amount
|
||||
RELATIONS = {
|
||||
'Ingredient' => {
|
||||
domain: :diet,
|
||||
foreign_key: :ingredient_id,
|
||||
subitem_class: Nutrient,
|
||||
value_field: :amount
|
||||
},
|
||||
'Measurement' => {
|
||||
domain: :measurement,
|
||||
foreign_key: :measurement_id,
|
||||
subitem_class: Readout,
|
||||
value_field: :value
|
||||
}
|
||||
}
|
||||
|
||||
def filter(filters, requested_q = nil)
|
||||
@@ -20,13 +26,18 @@ module BodyTracking
|
||||
items = items.where(hidden: filters[:visibility] == "1" ? false : true)
|
||||
end
|
||||
|
||||
project = proxy_association.owner
|
||||
domain = QUANTITY_DOMAINS[proxy_association.klass]
|
||||
formula_q = if filters[:formula].present?
|
||||
project.quantities.new(name: 'Filter formula',
|
||||
formula_attributes: filters[:formula],
|
||||
domain: domain)
|
||||
end
|
||||
formula_q =
|
||||
if filters[:formula].present?
|
||||
owner = proxy_association.owner
|
||||
project = owner.is_a?(Project) ? owner : owner.project
|
||||
domain = RELATIONS[proxy_association.klass.name][:domain]
|
||||
formula_q_attrs = {
|
||||
name: 'Filter formula',
|
||||
formula_attributes: filters[:formula],
|
||||
domain: domain
|
||||
}
|
||||
project.quantities.new(formula_q_attrs)
|
||||
end
|
||||
apply_formula = formula_q.present? && formula_q.valid?
|
||||
|
||||
result =
|
||||
@@ -45,15 +56,13 @@ module BodyTracking
|
||||
unchecked_q = requested_q.map { |q| [q, nil] }
|
||||
unchecked_q << [filter_q, nil] if filter_q
|
||||
|
||||
item_class = proxy_association.klass
|
||||
subitem_type = item_class.nested_attributes_options.keys.first.to_s
|
||||
subitem_reflection = item_class.reflections[subitem_type]
|
||||
subitem_class = subitem_reflection.klass
|
||||
subitems_scope = subitem_class.where(subitem_reflection.options[:inverse_of] => items)
|
||||
relations = RELATIONS[proxy_association.klass.name]
|
||||
subitems = Hash.new { |h,k| h[k] = {} }
|
||||
subitems_scope.includes(:quantity, :unit).order('quantities.lft').each do |s|
|
||||
item_id = s.send(subitem_reflection.foreign_key)
|
||||
subitem_value = s.send(VALUE_COLUMNS[item_class])
|
||||
relations[:subitem_class].where(relations[:foreign_key] => items)
|
||||
.includes(:quantity, :unit).order('quantities.lft').each do |s|
|
||||
|
||||
item_id = s.send(relations[:foreign_key])
|
||||
subitem_value = s.send(relations[:value_field])
|
||||
subitems[s.quantity][item_id] = [subitem_value, s.unit]
|
||||
end
|
||||
|
||||
|
||||
@@ -1,21 +1,18 @@
|
||||
module BodyTracking
|
||||
module ProjectPatch
|
||||
Project.class_eval do
|
||||
has_many :measurements, -> { order "taken_at DESC" }, dependent: :destroy,
|
||||
extend: ItemsWithQuantities
|
||||
has_many :ingredients, -> { order "name" }, dependent: :destroy,
|
||||
extend: ItemsWithQuantities
|
||||
module BodyTracking::ProjectPatch
|
||||
Project.class_eval do
|
||||
has_many :measurement_routines, dependent: :destroy
|
||||
has_many :measurements, -> { order "taken_at DESC" }, dependent: :destroy,
|
||||
extend: BodyTracking::ItemsWithQuantities, through: :measurement_routines
|
||||
has_many :ingredients, -> { order "name" }, dependent: :destroy,
|
||||
extend: BodyTracking::ItemsWithQuantities
|
||||
|
||||
has_many :sources, dependent: :destroy
|
||||
has_many :column_views, dependent: :destroy
|
||||
has_many :quantities, -> { order "lft" }, dependent: :destroy
|
||||
has_many :units, dependent: :destroy
|
||||
has_many :sources, dependent: :destroy
|
||||
has_many :quantities, -> { order "lft" }, dependent: :destroy
|
||||
has_many :units, dependent: :destroy
|
||||
|
||||
def nutrients_column_view
|
||||
self.column_views
|
||||
.find_or_create_by(name: 'Nutrients', domain: ColumnView.domains[:diet])
|
||||
end
|
||||
end
|
||||
has_many :nutrient_columns, as: :column_view, dependent: :destroy, class_name: 'Column',
|
||||
extend: BodyTracking::TogglableColumns
|
||||
has_many :nutrient_quantities, -> { order "lft" }, through: :nutrient_columns,
|
||||
source: 'quantity'
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
7
lib/body_tracking/togglable_columns.rb
Normal file
7
lib/body_tracking/togglable_columns.rb
Normal file
@@ -0,0 +1,7 @@
|
||||
module BodyTracking::TogglableColumns
|
||||
# TODO: enforce 'domain' identity between quantites and receiving collection?
|
||||
def toggle!(q)
|
||||
column = find_by(quantity: q)
|
||||
column ? destroy(column) : create(quantity: q)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user