1
0

Added test_defaults_seed_and_load_into_empty_project

Added :defaults scopes to models
Added :sources and :formulas fixtures
Loading defaults from seeds.rb using rake task instead of migration
This commit is contained in:
cryptogopher
2020-08-29 01:26:36 +02:00
parent 8f2a455561
commit 1b7f2f0abd
11 changed files with 245 additions and 10 deletions

View File

@@ -13,7 +13,7 @@ class BodyTrackersController < ApplicationController
# Units
available_units = @project.units.pluck(:shortname, :id).to_h
defaults = Unit.where(project: nil).map do |u|
defaults = Unit.defaults.map do |u|
u.attributes.except('id', 'project_id', 'created_at', 'updated_at')
end
defaults.delete_if { |u| available_units.has_key?(u['shortname']) }
@@ -26,7 +26,7 @@ class BodyTrackersController < ApplicationController
# Quantities
available_quantities = Quantity.each_with_path(@project.quantities).map(&:rotate).to_h
quantities_count = available_quantities.length
defaults = Quantity.where(project: nil)
defaults = Quantity.defaults
Quantity.each_with_path(defaults) do |q, path|
unless available_quantities.has_key?(path)
attrs = q.attributes.except('id', 'project_id', 'parent_id', 'lft', 'rgt',
@@ -56,7 +56,7 @@ class BodyTrackersController < ApplicationController
# Sources
available_sources = @project.sources.pluck(:name, :id).to_h
defaults = Source.where(project: nil).map do |s|
defaults = Source.defaults.map do |s|
s.attributes.except('id', 'project_id', 'created_at', 'updated_at')
end
defaults.delete_if { |s| available_sources.has_key?(s['name']) }

View File

@@ -6,6 +6,8 @@ class Formula < ActiveRecord::Base
belongs_to :quantity, inverse_of: :formula, required: true
belongs_to :unit
scope :defaults, -> { includes(:quantity).where(quantities: {project: nil}) }
validates :code, presence: true
validate do
messages = parse.each { |message, params| errors.add(:code, message, params) }
@@ -92,7 +94,7 @@ class Formula < ActiveRecord::Base
# e.g. during import of defaults (so impossible to use recursive sql instead)
q_names = identifiers.map { |i| i.split('::').last }
q_paths = {}
(quantity.project.try(&:quantities) || Quantity.where(project: nil))
(quantity.project.try(&:quantities) || Quantity.defaults)
.select { |q| q_names.include?(q.name) }.each do |q|
# NOTE: after upgrade to Ruby 2.7 replace with Enumerator#produce

View File

@@ -13,6 +13,8 @@ class Quantity < ActiveRecord::Base
has_many :values, class_name: 'QuantityValue', dependent: :restrict_with_error
has_many :exposures, dependent: :destroy
scope :defaults, -> { where(project: nil) }
has_one :formula, inverse_of: :quantity, dependent: :destroy, validate: true
accepts_nested_attributes_for :formula, allow_destroy: true,
reject_if: proc { |attrs| attrs['id'].blank? && attrs['code'].blank? }

View File

@@ -1,6 +1,8 @@
class Source < ActiveRecord::Base
belongs_to :project, required: false
scope :defaults, -> { where(project: nil) }
validates :name, presence: true, uniqueness: {scope: :project_id}
# Has to go before any 'dependent:' association

View File

@@ -1,6 +1,8 @@
class Unit < ActiveRecord::Base
belongs_to :project, required: false
scope :defaults, -> { where(project: nil) }
validates :shortname, presence: true, uniqueness: {scope: :project_id}
# Has to go before any 'dependent:' association