has_calculated_fields 1.0.3.2 → 1.0.3.4

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: 3d3ea6225ba6315220787cc8182604663b03acb695342072a707c7710ab015aa
4
- data.tar.gz: c70fbf60a71b26f946313e3cc4f0a22de31da55f92653c011ef50fe323495457
3
+ metadata.gz: 0afedc16cbc98af23216b3d921975247b1852fb83ada078232ae8878605ecd40
4
+ data.tar.gz: 0c527a880f8dc6164fdfc2328475b6de0327c0bbb172b21940e5ec0f1344f8ec
5
5
  SHA512:
6
- metadata.gz: 2980b8f0942749902f3d9c22e61b6d73706229142c4cf05bbdccad83520560d04dec6e8d131f56a6f445e3de32ab12c8158fc4d27f4118f499cdc592ee200b8b
7
- data.tar.gz: b62d335f136953751f7bcfea723cacd850fe6c1dc18b1402eeff402984ec1df838eae4a59fede58e7905b75386f6149c93ec79121068ef689765c4c02d9710b6
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.2"
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
@@ -206,14 +206,7 @@ module HasCalculatedFields
206
206
  end
207
207
 
208
208
  def _process_data(data)
209
- if data.key?(:if_changed)
210
- return unless changes.keys.include?(data[:if_changed])
211
- end
212
-
213
- if data.key?(:unless_changed)
214
- return if changes.keys.include?(data[:unless_changed])
215
- end
216
-
209
+ # return true unless _should_calculate_data?(data)
217
210
  attr_equal = "#{data[:calculated_field]}="
218
211
 
219
212
  value = case data[:type]
@@ -261,6 +254,16 @@ module HasCalculatedFields
261
254
  value
262
255
  end
263
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
264
267
  end
265
268
  end
266
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,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.2
4
+ version: 1.0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adrian Fernandez
@@ -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,10 +154,8 @@ 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
@@ -196,7 +180,5 @@ 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