has_calculated_fields 1.0.3.6 → 1.0.3.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5a19bc37b89db9ad306518d885b96897f2bbcda5471a3035ae04c5ee7ba30429
4
- data.tar.gz: 6044d0bc9cff905e0c48e123595f387f601d32f859719bae47655fa45b18866e
3
+ metadata.gz: f6cf158f78752207c08624fc9f0a78e61fe23bce35345390c68199f8a60ed38e
4
+ data.tar.gz: c5f79aacc2c41741e52f9916cfd5b7aef7196c9088d13a6c70ba122d1adcb24e
5
5
  SHA512:
6
- metadata.gz: 7872e4d93d3febee83c3245108d14dda74dcab6bac04d1a450c0013dbfaca7e38b8b0d3ddd67c7a2deaf8eec7c4203977b68bc2508182ddfd25d9d443a9b73f4
7
- data.tar.gz: 90ae2e5dd0ded0c0248c90f5d02e7f8a91fbfc7c7c5041ddeadfb4483460224d2ec363e50b6877cc0494d386491cf0edb1c48ebe33a3fc7dd56fe9ae500c7ef3
6
+ metadata.gz: 7c2ccaefb0403244e27280e269078f5356024092af4c71c3d3e08b191ec583ce0717558c6cd3200c89c6d1ff505714ea560bcf1f2c7c09a634c879f210505ad4
7
+ data.tar.gz: 11e1c6edd54e3d9c8d6ff9660eaaa52d660e67ea4aecb40428639936acd70b70acd3873daed4211d3abc82bb9ddf822951913a3f871c052478c0ad1c557b6117
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.6"
6
+ s.version = "1.0.3.7"
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.author = ["Adrian Fernandez"]
9
9
  s.email = ["adrianfernandez85@gmail.com"]
@@ -206,7 +206,8 @@ module HasCalculatedFields
206
206
  end
207
207
 
208
208
  def _process_data(data)
209
- # return true unless _should_calculate_data?(data)
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) && 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])
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
@@ -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
+ 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
- it "assigns conditional when condition is matched" do
43
- obj.name = "conditional name"
52
+ context "unless_changed" do
53
+ it "assigns conditional when condition is matched" do
54
+ obj.name = "conditional name"
44
55
 
45
- expect { obj.save }.to change {
46
- obj.calculated_conditional_if
47
- }.and change { obj.calculated_conditional_unless }
48
- .to("conditional name calculated!")
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: "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.6
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