easy_reference_data 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/ChangeLog +6 -0
- data/README.md +8 -0
- data/easy_reference_data.gemspec +1 -1
- data/lib/easy/reference_data/refresh.rb +26 -0
- data/lib/easy/reference_data/version.rb +1 -1
- data/lib/tasks/easy_reference_data.rake +4 -10
- data/spec/easy/reference_data/refresh_spec.rb +53 -4
- metadata +5 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: bd25797a8180f4486dd3746f13cf052fff6d3b91a7b61bea365ca89120ddb1cd
|
4
|
+
data.tar.gz: 85f70d58dca410136ea7e12b4234b9f40c0a45997ef64bc3dcbf28a13b8216b9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bc4c9f6abe89f1170d0b566909f3383353376b575aac5e170e0cc5aeef79d2c78fa368df1efbcc1d6a1c479b0d05ea0c721d915fe7ba447fc0fba782809e13c1
|
7
|
+
data.tar.gz: f4af0162d855cdf8c0d2d8af89264fe4cae4dfaa8b5e3e851a148228bf33f570c49dd63f2c45c893997f96c2bf01e7b03989bc5dffd705498cbd761f2848f7b4
|
data/ChangeLog
CHANGED
data/README.md
CHANGED
@@ -22,10 +22,18 @@ Place references in 'db/reference/'
|
|
22
22
|
|
23
23
|
References will be loaded in ascending order, so if an order is desired, prepend 000, 001, 002... etc to the filename.
|
24
24
|
|
25
|
+
If an unhandled error occurs during the loading of a specific file, then all the data loaded prior to the error will remain.
|
26
|
+
|
25
27
|
Run with:
|
26
28
|
|
27
29
|
rake easy:reference_data:refresh
|
28
30
|
|
31
|
+
To have all the reference data files loaded in one transaction, run with:
|
32
|
+
|
33
|
+
rake easy:reference_data:refresh[wrap_in_transaction]
|
34
|
+
|
35
|
+
The `wrap_in_transaction` parameter will wrap the entire load process in a single transaction. This means any unhandled errors will result in all the data loaded prior to the error being rolled back.
|
36
|
+
|
29
37
|
## Deployment
|
30
38
|
|
31
39
|
Add this line to your application's deploy.rb file:
|
data/easy_reference_data.gemspec
CHANGED
@@ -20,6 +20,6 @@ Gem::Specification.new do |gem|
|
|
20
20
|
gem.add_runtime_dependency 'rails', '>= 3.0.0'
|
21
21
|
|
22
22
|
gem.add_development_dependency 'rspec', '~> 3.6.0'
|
23
|
-
gem.add_development_dependency 'sqlite3', '~> 1.3'
|
23
|
+
gem.add_development_dependency 'sqlite3', '~> 1.3.0'
|
24
24
|
gem.add_development_dependency 'database_cleaner', '~> 1.6'
|
25
25
|
end
|
@@ -2,6 +2,7 @@ require 'active_support'
|
|
2
2
|
|
3
3
|
module Easy
|
4
4
|
module ReferenceData
|
5
|
+
|
5
6
|
def self.refresh(clazz, unique_attribute_symbol, unique_attribute_value, attributes)
|
6
7
|
self.update_or_create(clazz, attributes.merge(unique_attribute_symbol => unique_attribute_value), keys: [unique_attribute_symbol])
|
7
8
|
end
|
@@ -27,5 +28,30 @@ module Easy
|
|
27
28
|
record
|
28
29
|
end
|
29
30
|
|
31
|
+
def self.load_files(wrap_in_transaction: false)
|
32
|
+
if wrap_in_transaction
|
33
|
+
ActiveRecord::Base.transaction do
|
34
|
+
load_the_files
|
35
|
+
end
|
36
|
+
else
|
37
|
+
load_the_files
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
private_class_method
|
42
|
+
|
43
|
+
def self.files
|
44
|
+
files = Dir[File.join(Rails.root, 'db', 'reference', '*.rb')].sort
|
45
|
+
files += Dir[File.join(Rails.root, 'db', 'reference', Rails.env, '*.rb')].sort
|
46
|
+
files
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.load_the_files
|
50
|
+
files.each do |file|
|
51
|
+
puts "Populating reference #{file}"
|
52
|
+
load file
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
30
56
|
end
|
31
57
|
end
|
@@ -1,14 +1,8 @@
|
|
1
1
|
namespace :easy do
|
2
2
|
namespace :reference_data do
|
3
|
-
desc "Refreshes reference data values for the current environment."
|
4
|
-
task :refresh => :environment do
|
5
|
-
|
6
|
-
files += Dir[File.join(Rails.root, 'db', 'reference', Rails.env, '*.rb')].sort
|
7
|
-
|
8
|
-
files.each do |file|
|
9
|
-
puts "Populating reference #{file}"
|
10
|
-
load file
|
11
|
-
end
|
3
|
+
desc "Refreshes reference data values for the current environment. Pass the wrap_in_transaction flag to refresh in a transaction."
|
4
|
+
task :refresh, [:wrap_in_transaction] => :environment do |_task, args|
|
5
|
+
Easy::ReferenceData.load_files(wrap_in_transaction: args[:wrap_in_transaction] == "wrap_in_transaction")
|
12
6
|
end
|
13
7
|
end
|
14
|
-
end
|
8
|
+
end
|
@@ -13,7 +13,7 @@ RSpec.describe Easy::ReferenceData do
|
|
13
13
|
expect{ Easy::ReferenceData.update_or_create(User, {system_code: 1}, keys: [:system_code])}.not_to change{ User.count }
|
14
14
|
end
|
15
15
|
|
16
|
-
context "with additional
|
16
|
+
context "with additional attributes" do
|
17
17
|
it "updates the existing record" do
|
18
18
|
user = User.create(system_code: 1, name: "Jo")
|
19
19
|
|
@@ -43,8 +43,8 @@ RSpec.describe Easy::ReferenceData do
|
|
43
43
|
|
44
44
|
describe ".refresh" do
|
45
45
|
|
46
|
-
context "with a unique
|
47
|
-
context "and no
|
46
|
+
context "with a unique attribute" do
|
47
|
+
context "and no existing record" do
|
48
48
|
|
49
49
|
it "creates a new record" do
|
50
50
|
expect{
|
@@ -54,7 +54,7 @@ RSpec.describe Easy::ReferenceData do
|
|
54
54
|
|
55
55
|
end
|
56
56
|
|
57
|
-
context "and an
|
57
|
+
context "and an existing record" do
|
58
58
|
it "updates the existing record" do
|
59
59
|
user = User.create(system_code: 1, name: "Jo")
|
60
60
|
|
@@ -70,4 +70,53 @@ RSpec.describe Easy::ReferenceData do
|
|
70
70
|
end
|
71
71
|
|
72
72
|
end
|
73
|
+
|
74
|
+
describe ".load_files" do
|
75
|
+
before do
|
76
|
+
allow(subject).to receive(:files).and_return([@file1 = double(to_str: "easy/reference_data/refresh.rb")])
|
77
|
+
end
|
78
|
+
|
79
|
+
context "when the wrap_in_transaction argument is true" do
|
80
|
+
let(:call) {subject.load_files(wrap_in_transaction: true)}
|
81
|
+
|
82
|
+
it "starts a transaction" do
|
83
|
+
expect(ActiveRecord::Base).to receive(:transaction).and_call_original
|
84
|
+
call
|
85
|
+
end
|
86
|
+
|
87
|
+
it "loads all the files" do
|
88
|
+
allow(Kernel).to receive(:load).with(@file1)
|
89
|
+
call
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
context "when the wrap_in_transaction argument is false" do
|
95
|
+
let(:call) {subject.load_files(wrap_in_transaction: false)}
|
96
|
+
|
97
|
+
it "does not start a transaction" do
|
98
|
+
expect(ActiveRecord::Base).not_to receive(:transaction)
|
99
|
+
call
|
100
|
+
end
|
101
|
+
|
102
|
+
it "loads all the files" do
|
103
|
+
allow(Kernel).to receive(:load).with(@file1)
|
104
|
+
call
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
context "if the wrap_in_transaction argument is not passed in" do
|
109
|
+
let(:call) {subject.load_files}
|
110
|
+
|
111
|
+
it "does not start a transaction" do
|
112
|
+
expect(ActiveRecord::Base).not_to receive(:transaction)
|
113
|
+
call
|
114
|
+
end
|
115
|
+
|
116
|
+
it "loads all the files" do
|
117
|
+
allow(Kernel).to receive(:load).with(@file1)
|
118
|
+
call
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
73
122
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: easy_reference_data
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nigel Ramsay
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-02-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -44,14 +44,14 @@ dependencies:
|
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
47
|
+
version: 1.3.0
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
54
|
+
version: 1.3.0
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: database_cleaner
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -110,8 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
110
110
|
- !ruby/object:Gem::Version
|
111
111
|
version: '0'
|
112
112
|
requirements: []
|
113
|
-
|
114
|
-
rubygems_version: 2.6.11
|
113
|
+
rubygems_version: 3.0.1
|
115
114
|
signing_key:
|
116
115
|
specification_version: 4
|
117
116
|
summary: Loads files from db/reference/*.rb
|