has_calculated_fields 1.0.3 → 1.0.3.4

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: 2a23a9eeb023025474707803883654b56283734136f43a154e808f8791249045
4
- data.tar.gz: a9628e8beed6c91953e0eb3573061cb33e5f5fb8f246eedca78851cedf8948d1
3
+ metadata.gz: 0afedc16cbc98af23216b3d921975247b1852fb83ada078232ae8878605ecd40
4
+ data.tar.gz: 0c527a880f8dc6164fdfc2328475b6de0327c0bbb172b21940e5ec0f1344f8ec
5
5
  SHA512:
6
- metadata.gz: aaa4fc3f368d22f30e8b98e1d93744413e2be1d794ac27382500f7c00da2a05c384f235364527058a2332ff4a0908cc987c0b2dbb12d7e98eaeec209623cb51e
7
- data.tar.gz: 9b5d7beaeb2718b7b5a2dcbdc58c9ce86501ed1bd91ab140b1b59d01b0682950c5d491a6950e60b03f52ea48ac69b0dc2ac8f87475a6952015d44bf26e9f304d
6
+ metadata.gz: '081c4f56ed1bd82af9a4fa15a40320d91c57546970681889152f7589832311fb7a6ef582b12587d0143c29cac4f78de4baee31d1a59e6c570408cf996a2c8bfa'
7
+ data.tar.gz: 1954e0f1faf6b0e7bfe0d7c716d9e8e550745015f8217c0b383498b0d7a1811637053984008b632dd25ed69ecef3c5977fe03ad3219715d1d22c5bc7290bd8cf
data/Gemfile.lock CHANGED
@@ -1,12 +1,12 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- has_calculated_fields (1.0.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.7)
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.2.0)
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.0)
30
- builder (3.2.3)
31
- byebug (11.0.1)
32
- concurrent-ruby (1.1.5)
33
- database_cleaner (1.7.0)
34
- diff-lcs (1.3)
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.2.0)
41
- minitest (5.11.3)
42
- parallel (1.17.0)
43
- parser (2.6.3.0)
44
- ast (~> 2.4.0)
45
- powerpack (0.1.2)
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 (12.3.3)
49
- rspec (3.8.0)
50
- rspec-core (~> 3.8.0)
51
- rspec-expectations (~> 3.8.0)
52
- rspec-mocks (~> 3.8.0)
53
- rspec-core (3.8.2)
54
- rspec-support (~> 3.8.0)
55
- rspec-expectations (3.8.4)
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.8.0)
58
- rspec-mocks (3.8.1)
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.8.0)
61
- rspec-support (3.8.2)
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.10.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 (0.20.3)
74
+ thor (1.2.1)
77
75
  thread_safe (0.3.6)
78
- tzinfo (1.2.5)
76
+ tzinfo (1.2.9)
79
77
  thread_safe (~> 0.1)
80
- unicode-display_width (1.6.0)
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.16.6
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.4"
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
@@ -41,141 +41,172 @@ module HasCalculatedFields
41
41
  end
42
42
 
43
43
  def _has_calculated_fields__on_before_save
44
- return unless respond_to?(:has_calculated_fields_options)
45
- return unless has_calculated_fields_options.has_key?(:on_before_save)
44
+ return true unless respond_to?(:has_calculated_fields_options)
45
+ return true unless has_calculated_fields_options.has_key?(:on_before_save)
46
46
 
47
47
  has_calculated_fields_options[:on_before_save].each do |data|
48
48
  _process_data(data)
49
49
  end
50
+
51
+ true
50
52
  end
51
53
 
52
54
  def _has_calculated_fields__on_after_save
53
- return unless respond_to?(:has_calculated_fields_options)
54
- return unless has_calculated_fields_options.has_key?(:on_after_save)
55
+ return true unless respond_to?(:has_calculated_fields_options)
56
+ return true unless has_calculated_fields_options.has_key?(:on_after_save)
55
57
 
56
58
  has_calculated_fields_options[:on_after_save].each do |data|
57
59
  _process_data(data)
58
60
  end
61
+
62
+ true
59
63
  end
60
64
 
61
65
  def _has_calculated_fields__on_before_validation
62
- return unless respond_to?(:has_calculated_fields_options)
63
- return unless has_calculated_fields_options.has_key?(:on_before_validation)
66
+ return true unless respond_to?(:has_calculated_fields_options)
67
+ return true unless has_calculated_fields_options.has_key?(:on_before_validation)
64
68
 
65
69
  has_calculated_fields_options[:on_before_validation].each do |data|
66
70
  _process_data(data)
67
71
  end
72
+
73
+ true
68
74
  end
69
75
 
70
76
  def _has_calculated_fields__on_after_validation
71
- return unless respond_to?(:has_calculated_fields_options)
72
- return unless has_calculated_fields_options.has_key?(:on_after_validation)
77
+ return true unless respond_to?(:has_calculated_fields_options)
78
+ return true unless has_calculated_fields_options.has_key?(:on_after_validation)
73
79
 
74
80
  has_calculated_fields_options[:on_after_validation].each do |data|
75
81
  _process_data(data)
76
82
  end
83
+
84
+ true
77
85
  end
78
86
 
79
87
  def _has_calculated_fields__on_before_update
80
- return unless respond_to?(:has_calculated_fields_options)
81
- return unless has_calculated_fields_options.has_key?(:on_before_update)
88
+ return true unless respond_to?(:has_calculated_fields_options)
89
+ return true unless has_calculated_fields_options.has_key?(:on_before_update)
82
90
 
83
91
  has_calculated_fields_options[:on_before_update].each do |data|
84
92
  _process_data(data)
85
93
  end
94
+
95
+ true
86
96
  end
87
97
 
88
98
  def _has_calculated_fields__on_before_create
89
- return unless respond_to?(:has_calculated_fields_options)
90
- return unless has_calculated_fields_options.has_key?(:on_before_create)
99
+ return true unless respond_to?(:has_calculated_fields_options)
100
+ return true unless has_calculated_fields_options.has_key?(:on_before_create)
91
101
 
92
102
  has_calculated_fields_options[:on_before_create].each do |data|
93
103
  _process_data(data)
94
104
  end
105
+
106
+ true
95
107
  end
96
108
 
97
109
  def _has_calculated_fields__on_before_destroy
98
- return unless respond_to?(:has_calculated_fields_options)
99
- return unless has_calculated_fields_options.has_key?(:on_before_destroy)
110
+ return true unless respond_to?(:has_calculated_fields_options)
111
+ return true unless has_calculated_fields_options.has_key?(:on_before_destroy)
100
112
 
101
113
  has_calculated_fields_options[:on_before_destroy].each do |data|
102
114
  _process_data(data)
103
115
  end
116
+
117
+ true
104
118
  end
105
119
 
106
120
  def _has_calculated_fields__on_around_save
107
- return unless respond_to?(:has_calculated_fields_options)
108
- return unless has_calculated_fields_options.has_key?(:on_around_save)
121
+ return true unless respond_to?(:has_calculated_fields_options)
122
+ return true unless has_calculated_fields_options.has_key?(:on_around_save)
109
123
 
110
124
  has_calculated_fields_options[:on_around_save].each do |data|
111
125
  _process_data(data)
112
126
  end
127
+
128
+ true
113
129
  end
114
130
 
115
131
  def _has_calculated_fields__on_around_update
116
- return unless respond_to?(:has_calculated_fields_options)
117
- return unless has_calculated_fields_options.has_key?(:on_around_update)
132
+ return true unless respond_to?(:has_calculated_fields_options)
133
+ return true unless has_calculated_fields_options.has_key?(:on_around_update)
118
134
 
119
135
  has_calculated_fields_options[:on_around_update].each do |data|
120
136
  _process_data(data)
121
137
  end
138
+
139
+ true
122
140
  end
123
141
 
124
142
  def _has_calculated_fields__on_around_create
125
- return unless respond_to?(:has_calculated_fields_options)
126
- return unless has_calculated_fields_options.has_key?(:on_around_create)
143
+ return true unless respond_to?(:has_calculated_fields_options)
144
+ return true unless has_calculated_fields_options.has_key?(:on_around_create)
127
145
 
128
146
  has_calculated_fields_options[:on_around_create].each do |data|
129
147
  _process_data(data)
130
148
  end
149
+
150
+ true
131
151
  end
132
152
 
133
153
  def _has_calculated_fields__on_around_destroy
134
- return unless respond_to?(:has_calculated_fields_options)
135
- return unless has_calculated_fields_options.has_key?(:on_around_destroy)
154
+ return true unless respond_to?(:has_calculated_fields_options)
155
+ return true unless has_calculated_fields_options.has_key?(:on_around_destroy)
136
156
 
137
157
  has_calculated_fields_options[:on_around_destroy].each do |data|
138
158
  _process_data(data)
139
159
  end
160
+
161
+ true
140
162
  end
141
163
 
142
164
  def _has_calculated_fields__on_after_create
143
- return unless respond_to?(:has_calculated_fields_options)
144
- return unless has_calculated_fields_options.has_key?(:on_after_create)
165
+ return true unless respond_to?(:has_calculated_fields_options)
166
+ return true unless has_calculated_fields_options.has_key?(:on_after_create)
145
167
 
146
168
  has_calculated_fields_options[:on_after_create].each do |data|
147
169
  _process_data(data)
148
170
  end
171
+
172
+ true
149
173
  end
150
174
 
151
175
  def _has_calculated_fields__on_after_update
152
- return unless respond_to?(:has_calculated_fields_options)
153
- return unless has_calculated_fields_options.has_key?(:on_after_update)
176
+ return true unless respond_to?(:has_calculated_fields_options)
177
+ return true unless has_calculated_fields_options.has_key?(:on_after_update)
154
178
 
155
179
  has_calculated_fields_options[:on_after_update].each do |data|
156
180
  _process_data(data)
157
181
  end
182
+
183
+ true
158
184
  end
159
185
 
160
186
  def _has_calculated_fields__on_after_destroy
161
- return unless respond_to?(:has_calculated_fields_options)
162
- return unless has_calculated_fields_options.has_key?(:on_after_destroy)
187
+ return true unless respond_to?(:has_calculated_fields_options)
188
+ return true unless has_calculated_fields_options.has_key?(:on_after_destroy)
163
189
 
164
190
  has_calculated_fields_options[:on_after_destroy].each do |data|
165
191
  _process_data(data)
166
192
  end
193
+
194
+ true
167
195
  end
168
196
 
169
197
  def _has_calculated_fields__on_after_commit
170
- return unless respond_to?(:has_calculated_fields_options)
171
- return unless has_calculated_fields_options.has_key?(:on_after_commit)
198
+ return true unless respond_to?(:has_calculated_fields_options)
199
+ return true unless has_calculated_fields_options.has_key?(:on_after_commit)
172
200
 
173
201
  has_calculated_fields_options[:on_after_commit].each do |data|
174
202
  _process_data(data)
175
203
  end
204
+
205
+ true
176
206
  end
177
207
 
178
208
  def _process_data(data)
209
+ # return true unless _should_calculate_data?(data)
179
210
  attr_equal = "#{data[:calculated_field]}="
180
211
 
181
212
  value = case data[:type]
@@ -185,7 +216,6 @@ module HasCalculatedFields
185
216
  _process_method(data)
186
217
  end
187
218
 
188
- puts "#{attr_equal}#{value}"
189
219
  send(attr_equal, value)
190
220
  end
191
221
 
@@ -224,6 +254,16 @@ module HasCalculatedFields
224
254
  value
225
255
  end
226
256
  end
257
+
258
+ def _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
227
267
  end
228
268
  end
229
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(:sample_model, created_at: created_at, name: name) }
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: 1) do
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.4
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: 2020-04-18 00:00:00.000000000 Z
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
@@ -1,6 +0,0 @@
1
- #frozen_string_literal: true
2
-
3
- FactoryBot.define do
4
- factory :sample_model do
5
- end
6
- end
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.configure do |config|
4
- config.include FactoryBot::Syntax::Methods
5
-
6
- config.before(:suite) do
7
- FactoryBot.find_definitions
8
- end
9
- end