built_in_data 1.2.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/MIT-LICENSE +1 -1
- data/README.md +3 -0
- data/Rakefile +2 -0
- data/lib/built_in_data/version.rb +3 -1
- data/lib/built_in_data.rb +22 -20
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1dcb3fbcceaeff5cb4d43a1bc0c08edb3b375324ad4797c1ffcf24923913f0cd
|
4
|
+
data.tar.gz: 2b2195a4b426b877bb80cfb36c51e4244e05540ed71e8cb853c9eb0175373cde
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8252552d62e0f142c16f51465205ff986e11f5f30d23c4af1db7d45359e36b90a32814d66d277fdb29cb982fadaee65e6a2419352a97c19c3c5b1efde5a59e8c
|
7
|
+
data.tar.gz: 973e5ec125c4e846c63ec464de0e3a3824491f5704d478d2dedab136ba269a4b8e0a0e00d387ceaf548c812fb75dacc631e239f44d2e431a0163e7c4db0d6638
|
data/MIT-LICENSE
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Copyright 2012-
|
1
|
+
Copyright 2012-2023 Brightways Learning https://www.brightwayslearning.org
|
2
2
|
|
3
3
|
Permission is hereby granted, free of charge, to any person obtaining
|
4
4
|
a copy of this software and associated documentation files (the
|
data/README.md
CHANGED
data/Rakefile
CHANGED
data/lib/built_in_data.rb
CHANGED
@@ -1,32 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "built_in_data/version"
|
2
4
|
|
3
5
|
module BuiltInData
|
4
6
|
extend ActiveSupport::Concern
|
5
7
|
|
6
8
|
included do
|
7
|
-
#
|
9
|
+
# All built in data objects should have a built_in_key, model objects without a key will not be modified or removed
|
8
10
|
validates_uniqueness_of :built_in_key, allow_nil: true, case_sensitive: false
|
9
11
|
|
10
|
-
scope :built_in, -> { where
|
12
|
+
scope :built_in, -> { where.not(built_in_key: nil) }
|
11
13
|
end
|
12
14
|
|
13
15
|
module ClassMethods
|
16
|
+
# Inserts new, updates existing, and destorys removed built_in objects
|
14
17
|
def load_built_in_data!(hash = nil)
|
15
18
|
objects_hash = prepare_objects_hash(hash)
|
16
|
-
|
17
|
-
|
18
|
-
objects_hash.each do |key, attributes|
|
19
|
-
updated_objects << create_or_update!(key, attributes)
|
20
|
-
end
|
19
|
+
destroy_removed_built_in_objects!(objects_hash.keys)
|
21
20
|
|
22
|
-
|
23
|
-
|
24
|
-
object.destroy unless objects_hash.has_key?(object.built_in_key)
|
25
|
-
end
|
21
|
+
objects_hash.map do |key, attributes|
|
22
|
+
create_or_update!(key, attributes)
|
26
23
|
end
|
27
24
|
end
|
28
25
|
|
29
|
-
#
|
26
|
+
# Cached database id for fixture files
|
30
27
|
def built_in_object_id(key)
|
31
28
|
built_in_object_ids[key]
|
32
29
|
end
|
@@ -39,20 +36,25 @@ module BuiltInData
|
|
39
36
|
private
|
40
37
|
|
41
38
|
def prepare_objects_hash(hash)
|
42
|
-
|
39
|
+
hash.nil? ? load_yaml_data : hash.with_indifferent_access
|
43
40
|
end
|
44
41
|
|
45
42
|
def load_yaml_data
|
46
43
|
# allow a standard key to be used for defaults in YAML files
|
47
44
|
YAML.safe_load(
|
48
45
|
read_and_erb_process_yaml_file,
|
49
|
-
permitted_classes:
|
50
|
-
aliases:
|
51
|
-
).except(
|
46
|
+
permitted_classes: [Date],
|
47
|
+
aliases: true
|
48
|
+
).except("DEFAULTS")
|
52
49
|
end
|
53
50
|
|
54
51
|
def read_and_erb_process_yaml_file
|
55
|
-
ERB.new(
|
52
|
+
ERB.new(Rails.root.join("db", "built_in_data", "#{table_name}.yml").read).result
|
53
|
+
end
|
54
|
+
|
55
|
+
# Destroys built_in objects with a built_in_key not present in built_in_keys array
|
56
|
+
def destroy_removed_built_in_objects!(built_in_keys)
|
57
|
+
built_in.where.not(built_in_key: built_in_keys).destroy_all
|
56
58
|
end
|
57
59
|
|
58
60
|
def create_or_update!(key, attributes)
|
@@ -62,15 +64,15 @@ module BuiltInData
|
|
62
64
|
end
|
63
65
|
end
|
64
66
|
|
65
|
-
#
|
67
|
+
# Memoized hash of built in object ids
|
66
68
|
def built_in_object_ids
|
67
69
|
@built_in_object_ids ||= Hash.new do |hash, key|
|
68
|
-
hash[key] = where(built_in_key: key).
|
70
|
+
hash[key] = where(built_in_key: key).pick(:id)
|
69
71
|
end
|
70
72
|
end
|
71
73
|
end
|
72
74
|
|
73
75
|
def built_in?
|
74
|
-
|
76
|
+
built_in_key.present?
|
75
77
|
end
|
76
78
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: built_in_data
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aaron Baldwin
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2023-
|
12
|
+
date: 2023-02-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -60,7 +60,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
62
|
requirements: []
|
63
|
-
rubygems_version: 3.4.
|
63
|
+
rubygems_version: 3.4.5
|
64
64
|
signing_key:
|
65
65
|
specification_version: 4
|
66
66
|
summary: Data management for Rails models.
|