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