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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 80f022c4e87a567bdd3578d27a6ed797f317c44700407e42a54872556063b26c
4
- data.tar.gz: b471fa50ce90385f9ef2dc4d048634827dd880ee9f3501db3fd94fc686a6efe7
3
+ metadata.gz: fe42e86264da503a5735ff33ad72337d8ce94bb5282a1c4971983a8b9a32e030
4
+ data.tar.gz: 036c3d42cb5514e44d63a0af95176c35562f06118d0ecab261df73e322ab7637
5
5
  SHA512:
6
- metadata.gz: a592df5ed4a8a590b01209c6f4980e17020eb33cca40e84a9206c8263ab39ca15a8783a6ec93bdc632aa6f9d87c29469d7fde594c77a130daa3c3b10f8c7baf4
7
- data.tar.gz: dd46d667edf614df0c509189cee7e8a822fbcb9239cfdd4d5e762620885b07ea0957da0dae54890030465a132cf185059e6b1df9650920db481dd21c05c836ac
6
+ metadata.gz: ba646c37722a97d9b92eb384022d68d6b3112a766abfa19d561bdbef0a6c02531041ec6288ca59019b3c4244150ff522e95ab1c4a8f4277d45c27ff6728bbb74
7
+ data.tar.gz: 554db6d1dab5b198f89b1231fca1ef7566ba15d250ee581c9c739662d741a5ebc259d84b6c0b1abcc5f7a26246c0eb4ae7a8b7c22ddb12c0a9cedcfc1e98eca6
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- has_calculated_fields (1.0.3.4)
4
+ has_calculated_fields (1.0.3.7)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -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.5"
6
+ s.version = "1.0.3.8"
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.author = ["Adrian Fernandez"]
9
9
  s.email = ["adrianfernandez85@gmail.com"]
@@ -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
- f _should_calculate_data?(data)
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) && 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])
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
@@ -2,7 +2,6 @@
2
2
 
3
3
  require "active_record"
4
4
  require "active_support/inflector"
5
- require "active_model_serializers"
6
5
 
7
6
  $LOAD_PATH.unshift(File.dirname(__FILE__))
8
7
 
@@ -29,23 +29,42 @@ describe HasCalculatedFields do
29
29
  .to("new name calculated!")
30
30
  end
31
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!")
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
- it "assigns conditional when condition is matched" do
43
- obj.name = "conditional name"
51
+ context "unless_changed" do
52
+ it "assigns conditional when condition is matched" do
53
+ obj.name = "conditional name"
44
54
 
45
- expect { obj.save }.to change {
46
- obj.calculated_conditional_if
47
- }.and change { obj.calculated_conditional_unless }
48
- .to("conditional name calculated!")
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: "sqlite3", database: ":memory:")
23
-
24
- ActiveRecord::Schema.define(version: 3) do
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.5
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