has_calculated_fields 1.0.3.1 → 1.0.3.5
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/Gemfile.lock +32 -35
- data/has_calculated_fields.gemspec +1 -2
- data/lib/has_calculated_fields/has_calculated_fields.rb +11 -0
- data/spec/.DS_Store +0 -0
- data/spec/has_calculated_fields_spec.rb +20 -10
- data/spec/spec_helper.rb +20 -4
- metadata +5 -23
- data/spec/factories/other_event.rb +0 -6
- data/spec/support/factory_bot.rb +0 -9
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 80f022c4e87a567bdd3578d27a6ed797f317c44700407e42a54872556063b26c
|
|
4
|
+
data.tar.gz: b471fa50ce90385f9ef2dc4d048634827dd880ee9f3501db3fd94fc686a6efe7
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a592df5ed4a8a590b01209c6f4980e17020eb33cca40e84a9206c8263ab39ca15a8783a6ec93bdc632aa6f9d87c29469d7fde594c77a130daa3c3b10f8c7baf4
|
|
7
|
+
data.tar.gz: dd46d667edf614df0c509189cee7e8a822fbcb9239cfdd4d5e762620885b07ea0957da0dae54890030465a132cf185059e6b1df9650920db481dd21c05c836ac
|
data/Gemfile.lock
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
has_calculated_fields (1.0.
|
|
4
|
+
has_calculated_fields (1.0.3.4)
|
|
5
5
|
|
|
6
6
|
GEM
|
|
7
7
|
remote: https://rubygems.org/
|
|
8
8
|
specs:
|
|
9
|
-
active_model_serializers (0.9.
|
|
9
|
+
active_model_serializers (0.9.8)
|
|
10
10
|
activemodel (>= 3.2)
|
|
11
11
|
concurrent-ruby (~> 1.0)
|
|
12
12
|
activemodel (4.2.11.1)
|
|
@@ -21,44 +21,42 @@ GEM
|
|
|
21
21
|
minitest (~> 5.1)
|
|
22
22
|
thread_safe (~> 0.3, >= 0.3.4)
|
|
23
23
|
tzinfo (~> 1.1)
|
|
24
|
-
appraisal (2.
|
|
24
|
+
appraisal (2.4.1)
|
|
25
25
|
bundler
|
|
26
26
|
rake
|
|
27
27
|
thor (>= 0.14.0)
|
|
28
28
|
arel (6.0.4)
|
|
29
|
-
ast (2.4.
|
|
30
|
-
builder (3.2.
|
|
31
|
-
byebug (11.
|
|
32
|
-
concurrent-ruby (1.1.
|
|
33
|
-
database_cleaner (1.
|
|
34
|
-
diff-lcs (1.
|
|
29
|
+
ast (2.4.2)
|
|
30
|
+
builder (3.2.4)
|
|
31
|
+
byebug (11.1.3)
|
|
32
|
+
concurrent-ruby (1.1.10)
|
|
33
|
+
database_cleaner (1.99.0)
|
|
34
|
+
diff-lcs (1.5.0)
|
|
35
35
|
docile (1.1.5)
|
|
36
|
-
factory_bot (4.11.1)
|
|
37
|
-
activesupport (>= 3.0.0)
|
|
38
36
|
i18n (0.9.5)
|
|
39
37
|
concurrent-ruby (~> 1.0)
|
|
40
|
-
json (2.
|
|
41
|
-
minitest (5.
|
|
42
|
-
parallel (1.
|
|
43
|
-
parser (2.
|
|
44
|
-
ast (~> 2.4.
|
|
45
|
-
powerpack (0.1.
|
|
38
|
+
json (2.6.1)
|
|
39
|
+
minitest (5.15.0)
|
|
40
|
+
parallel (1.22.1)
|
|
41
|
+
parser (2.7.2.0)
|
|
42
|
+
ast (~> 2.4.1)
|
|
43
|
+
powerpack (0.1.3)
|
|
46
44
|
rainbow (2.2.2)
|
|
47
45
|
rake
|
|
48
|
-
rake (
|
|
49
|
-
rspec (3.
|
|
50
|
-
rspec-core (~> 3.
|
|
51
|
-
rspec-expectations (~> 3.
|
|
52
|
-
rspec-mocks (~> 3.
|
|
53
|
-
rspec-core (3.
|
|
54
|
-
rspec-support (~> 3.
|
|
55
|
-
rspec-expectations (3.
|
|
46
|
+
rake (13.0.6)
|
|
47
|
+
rspec (3.11.0)
|
|
48
|
+
rspec-core (~> 3.11.0)
|
|
49
|
+
rspec-expectations (~> 3.11.0)
|
|
50
|
+
rspec-mocks (~> 3.11.0)
|
|
51
|
+
rspec-core (3.11.0)
|
|
52
|
+
rspec-support (~> 3.11.0)
|
|
53
|
+
rspec-expectations (3.11.0)
|
|
56
54
|
diff-lcs (>= 1.2.0, < 2.0)
|
|
57
|
-
rspec-support (~> 3.
|
|
58
|
-
rspec-mocks (3.
|
|
55
|
+
rspec-support (~> 3.11.0)
|
|
56
|
+
rspec-mocks (3.11.1)
|
|
59
57
|
diff-lcs (>= 1.2.0, < 2.0)
|
|
60
|
-
rspec-support (~> 3.
|
|
61
|
-
rspec-support (3.
|
|
58
|
+
rspec-support (~> 3.11.0)
|
|
59
|
+
rspec-support (3.11.0)
|
|
62
60
|
rubocop (0.49.1)
|
|
63
61
|
parallel (~> 1.10)
|
|
64
62
|
parser (>= 2.3.3.1, < 3.0)
|
|
@@ -66,18 +64,18 @@ GEM
|
|
|
66
64
|
rainbow (>= 1.99.1, < 3.0)
|
|
67
65
|
ruby-progressbar (~> 1.7)
|
|
68
66
|
unicode-display_width (~> 1.0, >= 1.0.1)
|
|
69
|
-
ruby-progressbar (1.
|
|
67
|
+
ruby-progressbar (1.11.0)
|
|
70
68
|
simplecov (0.15.1)
|
|
71
69
|
docile (~> 1.1.0)
|
|
72
70
|
json (>= 1.8, < 3)
|
|
73
71
|
simplecov-html (~> 0.10.0)
|
|
74
72
|
simplecov-html (0.10.2)
|
|
75
73
|
sqlite3 (1.3.13)
|
|
76
|
-
thor (
|
|
74
|
+
thor (1.2.1)
|
|
77
75
|
thread_safe (0.3.6)
|
|
78
|
-
tzinfo (1.2.
|
|
76
|
+
tzinfo (1.2.9)
|
|
79
77
|
thread_safe (~> 0.1)
|
|
80
|
-
unicode-display_width (1.
|
|
78
|
+
unicode-display_width (1.8.0)
|
|
81
79
|
|
|
82
80
|
PLATFORMS
|
|
83
81
|
ruby
|
|
@@ -88,7 +86,6 @@ DEPENDENCIES
|
|
|
88
86
|
appraisal (~> 2.2)
|
|
89
87
|
byebug
|
|
90
88
|
database_cleaner
|
|
91
|
-
factory_bot (~> 4.8)
|
|
92
89
|
has_calculated_fields!
|
|
93
90
|
rspec (~> 3.6)
|
|
94
91
|
rubocop (~> 0.49.1)
|
|
@@ -96,4 +93,4 @@ DEPENDENCIES
|
|
|
96
93
|
sqlite3 (~> 1.3.6)
|
|
97
94
|
|
|
98
95
|
BUNDLED WITH
|
|
99
|
-
1.
|
|
96
|
+
1.17.2
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
|
|
4
4
|
Gem::Specification.new do |s|
|
|
5
5
|
s.name = "has_calculated_fields"
|
|
6
|
-
s.version = "1.0.3.
|
|
6
|
+
s.version = "1.0.3.5"
|
|
7
7
|
s.platform = Gem::Platform::RUBY
|
|
8
8
|
s.author = ["Adrian Fernandez"]
|
|
9
9
|
s.email = ["adrianfernandez85@gmail.com"]
|
|
@@ -24,7 +24,6 @@ Gem::Specification.new do |s|
|
|
|
24
24
|
s.add_development_dependency "simplecov", "~> 0.15.0"
|
|
25
25
|
s.add_development_dependency "appraisal", "~> 2.2"
|
|
26
26
|
s.add_development_dependency "byebug"
|
|
27
|
-
s.add_development_dependency "factory_bot", "~> 4.8"
|
|
28
27
|
s.add_development_dependency "database_cleaner"
|
|
29
28
|
s.add_development_dependency "active_model_serializers", "~> 0.9.3"
|
|
30
29
|
end
|
|
@@ -206,6 +206,7 @@ module HasCalculatedFields
|
|
|
206
206
|
end
|
|
207
207
|
|
|
208
208
|
def _process_data(data)
|
|
209
|
+
return true unless _should_calculate_data?(data)
|
|
209
210
|
attr_equal = "#{data[:calculated_field]}="
|
|
210
211
|
|
|
211
212
|
value = case data[:type]
|
|
@@ -253,6 +254,16 @@ module HasCalculatedFields
|
|
|
253
254
|
value
|
|
254
255
|
end
|
|
255
256
|
end
|
|
257
|
+
|
|
258
|
+
f _should_calculate_data?(data)
|
|
259
|
+
return true if data.blank?
|
|
260
|
+
return true if !data.has_key?(:if_changed) && !data.has_key?(:unless_changed)
|
|
261
|
+
|
|
262
|
+
return true if data.has_key?(:if_changed) && changes.keys.map(&:to_sym).include?(data[:if_changed])
|
|
263
|
+
return true if data.has_key?(:unless_changed) && !changes.keys.map(&:to_sym).include?(data[:unless_changed])
|
|
264
|
+
|
|
265
|
+
false
|
|
266
|
+
end
|
|
256
267
|
end
|
|
257
268
|
end
|
|
258
269
|
|
data/spec/.DS_Store
CHANGED
|
Binary file
|
|
@@ -5,7 +5,7 @@ describe HasCalculatedFields do
|
|
|
5
5
|
describe "runs before_save callbacks" do
|
|
6
6
|
let(:name) { "name" }
|
|
7
7
|
let(:created_at) { Time.find_zone("Madrid").parse("2019-05-03 2pm") }
|
|
8
|
-
let(:obj) { create(
|
|
8
|
+
let(:obj) { SampleModel.create(created_at: created_at, name: name) }
|
|
9
9
|
|
|
10
10
|
context "using time" do
|
|
11
11
|
context "when attribute has value" do
|
|
@@ -18,15 +18,6 @@ describe HasCalculatedFields do
|
|
|
18
18
|
.to("Sat, 04 May 2019 16:00:00 +0200")
|
|
19
19
|
end
|
|
20
20
|
end
|
|
21
|
-
|
|
22
|
-
context "when attribute hasn't value" do
|
|
23
|
-
let(:created_at) { nil }
|
|
24
|
-
it "assigns formatted time attribute" do
|
|
25
|
-
expect { obj.save }.to change { obj.calculated_created_at }
|
|
26
|
-
.from(I18n.l(Time.current))
|
|
27
|
-
.to(I18n.l(Time.current.in_time_zone("UTC")))
|
|
28
|
-
end
|
|
29
|
-
end
|
|
30
21
|
end
|
|
31
22
|
|
|
32
23
|
context "using method" do
|
|
@@ -37,6 +28,25 @@ describe HasCalculatedFields do
|
|
|
37
28
|
.from("name calculated!")
|
|
38
29
|
.to("new name calculated!")
|
|
39
30
|
end
|
|
31
|
+
|
|
32
|
+
it "assigns conditional when condition is matched" do
|
|
33
|
+
obj.name = "conditional name"
|
|
34
|
+
obj.random_attribute = "4"
|
|
35
|
+
|
|
36
|
+
expect { obj.save }.to change {
|
|
37
|
+
obj.calculated_conditional_unless
|
|
38
|
+
}.and change { obj.calculated_conditional_if }
|
|
39
|
+
.to("conditional name calculated!")
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
it "assigns conditional when condition is matched" do
|
|
43
|
+
obj.name = "conditional name"
|
|
44
|
+
|
|
45
|
+
expect { obj.save }.to change {
|
|
46
|
+
obj.calculated_conditional_if
|
|
47
|
+
}.and change { obj.calculated_conditional_unless }
|
|
48
|
+
.to("conditional name calculated!")
|
|
49
|
+
end
|
|
40
50
|
end
|
|
41
51
|
end
|
|
42
52
|
end
|
data/spec/spec_helper.rb
CHANGED
|
@@ -3,12 +3,9 @@
|
|
|
3
3
|
$LOAD_PATH << File.join(File.dirname(__FILE__), "..", "lib")
|
|
4
4
|
require "sqlite3"
|
|
5
5
|
require "has_calculated_fields"
|
|
6
|
-
require "factory_bot"
|
|
7
6
|
require 'database_cleaner'
|
|
8
7
|
require "active_model_serializers"
|
|
9
8
|
|
|
10
|
-
Dir["./spec/support/**/*.rb"].sort.each { |f| require f }
|
|
11
|
-
|
|
12
9
|
RSpec.configure do |config|
|
|
13
10
|
config.before(:suite) do
|
|
14
11
|
DatabaseCleaner.strategy = :transaction
|
|
@@ -24,13 +21,16 @@ end
|
|
|
24
21
|
|
|
25
22
|
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
|
|
26
23
|
|
|
27
|
-
ActiveRecord::Schema.define(version:
|
|
24
|
+
ActiveRecord::Schema.define(version: 3) do
|
|
28
25
|
create_table :sample_models do |t|
|
|
29
26
|
t.string :name
|
|
27
|
+
t.string :random_attribute
|
|
30
28
|
t.datetime :created_at
|
|
31
29
|
|
|
32
30
|
t.string :calculated_name
|
|
33
31
|
t.string :calculated_created_at
|
|
32
|
+
t.string :calculated_conditional_if
|
|
33
|
+
t.string :calculated_conditional_unless
|
|
34
34
|
end
|
|
35
35
|
end
|
|
36
36
|
|
|
@@ -48,6 +48,22 @@ class SampleModel < ActiveRecord::Base
|
|
|
48
48
|
method: Proc.new do |obj|
|
|
49
49
|
obj.name + " calculated!"
|
|
50
50
|
end
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
calculated_field: :calculated_conditional_if,
|
|
54
|
+
type: :method,
|
|
55
|
+
if_changed: :random_attribute,
|
|
56
|
+
method: Proc.new do |obj|
|
|
57
|
+
obj.name + " calculated!"
|
|
58
|
+
end
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
calculated_field: :calculated_conditional_unless,
|
|
62
|
+
type: :method,
|
|
63
|
+
unless_changed: :random_attribute,
|
|
64
|
+
method: Proc.new do |obj|
|
|
65
|
+
obj.name + " calculated!"
|
|
66
|
+
end
|
|
51
67
|
}
|
|
52
68
|
]
|
|
53
69
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: has_calculated_fields
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0.3.
|
|
4
|
+
version: 1.0.3.5
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Adrian Fernandez
|
|
8
|
-
autorequire:
|
|
8
|
+
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2022-05-07 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rspec
|
|
@@ -108,20 +108,6 @@ dependencies:
|
|
|
108
108
|
- - ">="
|
|
109
109
|
- !ruby/object:Gem::Version
|
|
110
110
|
version: '0'
|
|
111
|
-
- !ruby/object:Gem::Dependency
|
|
112
|
-
name: factory_bot
|
|
113
|
-
requirement: !ruby/object:Gem::Requirement
|
|
114
|
-
requirements:
|
|
115
|
-
- - "~>"
|
|
116
|
-
- !ruby/object:Gem::Version
|
|
117
|
-
version: '4.8'
|
|
118
|
-
type: :development
|
|
119
|
-
prerelease: false
|
|
120
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
121
|
-
requirements:
|
|
122
|
-
- - "~>"
|
|
123
|
-
- !ruby/object:Gem::Version
|
|
124
|
-
version: '4.8'
|
|
125
111
|
- !ruby/object:Gem::Dependency
|
|
126
112
|
name: database_cleaner
|
|
127
113
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -168,15 +154,13 @@ files:
|
|
|
168
154
|
- lib/has_calculated_fields/.DS_Store
|
|
169
155
|
- lib/has_calculated_fields/has_calculated_fields.rb
|
|
170
156
|
- spec/.DS_Store
|
|
171
|
-
- spec/factories/other_event.rb
|
|
172
157
|
- spec/has_calculated_fields_spec.rb
|
|
173
158
|
- spec/spec_helper.rb
|
|
174
|
-
- spec/support/factory_bot.rb
|
|
175
159
|
homepage: http://github.com/adrian-fernandez/has_calculated_fields
|
|
176
160
|
licenses:
|
|
177
161
|
- MIT
|
|
178
162
|
metadata: {}
|
|
179
|
-
post_install_message:
|
|
163
|
+
post_install_message:
|
|
180
164
|
rdoc_options: []
|
|
181
165
|
require_paths:
|
|
182
166
|
- lib
|
|
@@ -192,11 +176,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
192
176
|
version: '0'
|
|
193
177
|
requirements: []
|
|
194
178
|
rubygems_version: 3.0.3
|
|
195
|
-
signing_key:
|
|
179
|
+
signing_key:
|
|
196
180
|
specification_version: 4
|
|
197
181
|
summary: Rails gem to allow models to save auto calculated fields
|
|
198
182
|
test_files:
|
|
199
|
-
- spec/factories/other_event.rb
|
|
200
183
|
- spec/has_calculated_fields_spec.rb
|
|
201
184
|
- spec/spec_helper.rb
|
|
202
|
-
- spec/support/factory_bot.rb
|