Added loading default sources
This commit is contained in:
parent
593e11ff53
commit
0e6bbd8c74
@ -6,7 +6,8 @@ class BodyTrackersController < ApplicationController
|
||||
end
|
||||
|
||||
def defaults
|
||||
available = Unit.where(project: @project).pluck(:shortname)
|
||||
# Units
|
||||
available = @project.units.pluck(:shortname)
|
||||
defaults = Unit.where(project: nil).pluck(:name, :shortname)
|
||||
defaults.delete_if { |n, s| available.include?(s) }
|
||||
@project.units.create(defaults.map { |n, s| {name: n, shortname: s} })
|
||||
@ -15,25 +16,37 @@ class BodyTrackersController < ApplicationController
|
||||
flash[:notice] = "Loaded #{new_units > 0 ? new_units : "no" } new" \
|
||||
" #{'unit'.pluralize(new_units)}"
|
||||
|
||||
# Quantities
|
||||
available = @project.quantities.map { |q| [[q.name, q.domain], q] }.to_h
|
||||
new_quantities = available.length
|
||||
defaults = Quantity.where(project: nil)
|
||||
Quantity.each_with_level(defaults) do |q, level|
|
||||
unless available.has_key?([q.name, q.domain])
|
||||
obj = @project.quantities.create({
|
||||
name: q.name,
|
||||
domain: q.domain,
|
||||
parent: q.parent ? available[[q.parent.name, q.parent.domain]] : nil,
|
||||
name: q.name,
|
||||
description: q.description,
|
||||
parent: q.parent ? available[[q.parent.name, q.parent.domain]] : nil
|
||||
displayed: q.displayed
|
||||
})
|
||||
available[[q.name, q.domain]] = obj
|
||||
end
|
||||
end
|
||||
|
||||
new_quantities = available.length - new_quantities
|
||||
flash[:notice] += " and #{new_quantities > 0 ? new_quantities : "no" } new" \
|
||||
flash[:notice] += ", #{new_quantities > 0 ? new_quantities : "no" } new" \
|
||||
" #{'quantity'.pluralize(new_quantities)}"
|
||||
|
||||
# Sources
|
||||
available = @project.sources.pluck(:name)
|
||||
defaults = Source.where(project: nil).pluck(:name, :description)
|
||||
defaults.delete_if { |n, d| available.include?(n) }
|
||||
@project.sources.create(defaults.map { |n, d| {name: n, description: d} })
|
||||
|
||||
new_sources = defaults.length
|
||||
flash[:notice] += " and #{new_sources > 0 ? new_sources : "no" } new" \
|
||||
" #{'source'.pluralize(new_sources)}"
|
||||
|
||||
redirect_to :back
|
||||
end
|
||||
end
|
||||
|
@ -163,10 +163,14 @@ class CreateUnits < ActiveRecord::Migration
|
||||
description: ""
|
||||
v21 = Quantity.create project: nil, domain: :diet, parent: v1, name: "Vitamin K",
|
||||
description: ""
|
||||
|
||||
Source.create project: nil, name: "Nutrition label",
|
||||
description: "From package nutrition label"
|
||||
end
|
||||
dir.down do
|
||||
Unit.where(project: nil).delete_all
|
||||
Quantity.where(project: nil).delete_all
|
||||
Source.where(project: nil).delete_all
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user