has_calculated_fields 1.0.3.4 → 1.0.3.7
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 +10 -4
- 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: f6cf158f78752207c08624fc9f0a78e61fe23bce35345390c68199f8a60ed38e
|
4
|
+
data.tar.gz: c5f79aacc2c41741e52f9916cfd5b7aef7196c9088d13a6c70ba122d1adcb24e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7c2ccaefb0403244e27280e269078f5356024092af4c71c3d3e08b191ec583ce0717558c6cd3200c89c6d1ff505714ea560bcf1f2c7c09a634c879f210505ad4
|
7
|
+
data.tar.gz: 11e1c6edd54e3d9c8d6ff9660eaaa52d660e67ea4aecb40428639936acd70b70acd3873daed4211d3abc82bb9ddf822951913a3f871c052478c0ad1c557b6117
|
data/Gemfile.lock
CHANGED
@@ -206,7 +206,8 @@ module HasCalculatedFields
|
|
206
206
|
end
|
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]
|
@@ -255,12 +256,17 @@ module HasCalculatedFields
|
|
255
256
|
end
|
256
257
|
end
|
257
258
|
|
258
|
-
def _should_calculate_data?(data)
|
259
|
+
def _should_calculate_data?(data = {})
|
260
|
+
if_changed = data.fetch(:if_changed, nil)
|
261
|
+
unless_changed = data.fetch(:unless_changed, nil)
|
262
|
+
|
263
|
+
changed_keys = changes.keys.map(&:to_sym) || []
|
264
|
+
|
259
265
|
return true if data.blank?
|
260
266
|
return true if !data.has_key?(:if_changed) && !data.has_key?(:unless_changed)
|
261
267
|
|
262
|
-
return true if data.has_key?(:if_changed) &&
|
263
|
-
return true if data.has_key?(:unless_changed) && !
|
268
|
+
return true if data.has_key?(:if_changed) && changed_keys.include?(if_changed)
|
269
|
+
return true if data.has_key?(:unless_changed) && !changed_keys.include?(unless_changed)
|
264
270
|
|
265
271
|
false
|
266
272
|
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
|
+
obj.random_attribute = "4"
|
45
|
+
|
46
|
+
expect { obj.save }.not_to change {
|
47
|
+
obj.calculated_conditional_if
|
48
|
+
}
|
49
|
+
end
|
40
50
|
end
|
41
51
|
|
42
|
-
|
43
|
-
|
52
|
+
context "unless_changed" do
|
53
|
+
it "assigns conditional when condition is matched" do
|
54
|
+
obj.name = "conditional name"
|
44
55
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
56
|
+
expect { obj.save }.to change {
|
57
|
+
obj.calculated_conditional_unless
|
58
|
+
}
|
59
|
+
end
|
60
|
+
|
61
|
+
it "does not assign conditional when condition is matched" do
|
62
|
+
obj.name = "conditional name"
|
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.7
|
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
|