has_calculated_fields 1.0.3.5 → 1.0.3.8
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 +1 -1
- data/has_calculated_fields.gemspec +1 -1
- data/lib/has_calculated_fields/has_calculated_fields.rb +11 -3
- data/lib/has_calculated_fields.rb +0 -1
- data/spec/has_calculated_fields_spec.rb +33 -14
- data/spec/schema.rb +12 -0
- data/spec/spec_helper.rb +3 -15
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fe42e86264da503a5735ff33ad72337d8ce94bb5282a1c4971983a8b9a32e030
|
4
|
+
data.tar.gz: 036c3d42cb5514e44d63a0af95176c35562f06118d0ecab261df73e322ab7637
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ba646c37722a97d9b92eb384022d68d6b3112a766abfa19d561bdbef0a6c02531041ec6288ca59019b3c4244150ff522e95ab1c4a8f4277d45c27ff6728bbb74
|
7
|
+
data.tar.gz: 554db6d1dab5b198f89b1231fca1ef7566ba15d250ee581c9c739662d741a5ebc259d84b6c0b1abcc5f7a26246c0eb4ae7a8b7c22ddb12c0a9cedcfc1e98eca6
|
data/Gemfile.lock
CHANGED
@@ -207,6 +207,7 @@ module HasCalculatedFields
|
|
207
207
|
|
208
208
|
def _process_data(data)
|
209
209
|
return true unless _should_calculate_data?(data)
|
210
|
+
|
210
211
|
attr_equal = "#{data[:calculated_field]}="
|
211
212
|
|
212
213
|
value = case data[:type]
|
@@ -217,6 +218,8 @@ module HasCalculatedFields
|
|
217
218
|
end
|
218
219
|
|
219
220
|
send(attr_equal, value)
|
221
|
+
|
222
|
+
true
|
220
223
|
end
|
221
224
|
|
222
225
|
def _process_date(data)
|
@@ -255,12 +258,17 @@ module HasCalculatedFields
|
|
255
258
|
end
|
256
259
|
end
|
257
260
|
|
258
|
-
|
261
|
+
def _should_calculate_data?(data = {})
|
262
|
+
if_changed = data.fetch(:if_changed, nil)
|
263
|
+
unless_changed = data.fetch(:unless_changed, nil)
|
264
|
+
|
265
|
+
changed_keys = changes.keys.map(&:to_sym) || []
|
266
|
+
|
259
267
|
return true if data.blank?
|
260
268
|
return true if !data.has_key?(:if_changed) && !data.has_key?(:unless_changed)
|
261
269
|
|
262
|
-
return true if data.has_key?(:if_changed) &&
|
263
|
-
return true if data.has_key?(:unless_changed) && !
|
270
|
+
return true if data.has_key?(:if_changed) && changed_keys.include?(if_changed)
|
271
|
+
return true if data.has_key?(:unless_changed) && !changed_keys.include?(unless_changed)
|
264
272
|
|
265
273
|
false
|
266
274
|
end
|
@@ -29,23 +29,42 @@ describe HasCalculatedFields do
|
|
29
29
|
.to("new name calculated!")
|
30
30
|
end
|
31
31
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
obj.
|
38
|
-
|
39
|
-
|
32
|
+
context "if_changed" do
|
33
|
+
it "assigns conditional when condition is matched" do
|
34
|
+
obj.name = "conditional name"
|
35
|
+
obj.random_attribute = "4"
|
36
|
+
|
37
|
+
expect { obj.save }.to change {
|
38
|
+
obj.calculated_conditional_if
|
39
|
+
}
|
40
|
+
end
|
41
|
+
|
42
|
+
it "does not assign conditional when condition is matched" do
|
43
|
+
obj.name = "conditional name"
|
44
|
+
|
45
|
+
expect { obj.save }.not_to change {
|
46
|
+
obj.calculated_conditional_if
|
47
|
+
}
|
48
|
+
end
|
40
49
|
end
|
41
50
|
|
42
|
-
|
43
|
-
|
51
|
+
context "unless_changed" do
|
52
|
+
it "assigns conditional when condition is matched" do
|
53
|
+
obj.name = "conditional name"
|
44
54
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
55
|
+
expect { obj.save }.to change {
|
56
|
+
obj.calculated_conditional_unless
|
57
|
+
}
|
58
|
+
end
|
59
|
+
|
60
|
+
it "does not assign conditional when condition is matched" do
|
61
|
+
obj.name = "conditional name"
|
62
|
+
obj.random_attribute = "4"
|
63
|
+
|
64
|
+
expect { obj.save }.not_to change {
|
65
|
+
obj.calculated_conditional_unless
|
66
|
+
}
|
67
|
+
end
|
49
68
|
end
|
50
69
|
end
|
51
70
|
end
|
data/spec/schema.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
ActiveRecord::Schema.define(version: 2022_05_07_120518) do
|
2
|
+
create_table :sample_models do |t|
|
3
|
+
t.string :name
|
4
|
+
t.string :random_attribute
|
5
|
+
t.datetime :created_at
|
6
|
+
|
7
|
+
t.string :calculated_name
|
8
|
+
t.string :calculated_created_at
|
9
|
+
t.string :calculated_conditional_if
|
10
|
+
t.string :calculated_conditional_unless
|
11
|
+
end
|
12
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -4,7 +4,6 @@ $LOAD_PATH << File.join(File.dirname(__FILE__), "..", "lib")
|
|
4
4
|
require "sqlite3"
|
5
5
|
require "has_calculated_fields"
|
6
6
|
require 'database_cleaner'
|
7
|
-
require "active_model_serializers"
|
8
7
|
|
9
8
|
RSpec.configure do |config|
|
10
9
|
config.before(:suite) do
|
@@ -19,20 +18,9 @@ RSpec.configure do |config|
|
|
19
18
|
end
|
20
19
|
end
|
21
20
|
|
22
|
-
ActiveRecord::Base.establish_connection(adapter:
|
23
|
-
|
24
|
-
|
25
|
-
create_table :sample_models do |t|
|
26
|
-
t.string :name
|
27
|
-
t.string :random_attribute
|
28
|
-
t.datetime :created_at
|
29
|
-
|
30
|
-
t.string :calculated_name
|
31
|
-
t.string :calculated_created_at
|
32
|
-
t.string :calculated_conditional_if
|
33
|
-
t.string :calculated_conditional_unless
|
34
|
-
end
|
35
|
-
end
|
21
|
+
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
|
22
|
+
ActiveRecord::Schema.verbose = true
|
23
|
+
load "spec/schema.rb"
|
36
24
|
|
37
25
|
class SampleModel < ActiveRecord::Base
|
38
26
|
has_calculated_fields on_before_save: [
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adrian Fernandez
|
@@ -155,6 +155,7 @@ files:
|
|
155
155
|
- lib/has_calculated_fields/has_calculated_fields.rb
|
156
156
|
- spec/.DS_Store
|
157
157
|
- spec/has_calculated_fields_spec.rb
|
158
|
+
- spec/schema.rb
|
158
159
|
- spec/spec_helper.rb
|
159
160
|
homepage: http://github.com/adrian-fernandez/has_calculated_fields
|
160
161
|
licenses:
|
@@ -181,4 +182,5 @@ specification_version: 4
|
|
181
182
|
summary: Rails gem to allow models to save auto calculated fields
|
182
183
|
test_files:
|
183
184
|
- spec/has_calculated_fields_spec.rb
|
185
|
+
- spec/schema.rb
|
184
186
|
- spec/spec_helper.rb
|