active_record-events 4.1.0 → 4.1.2

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: 1eace6bdc4fb78e2ac6c90a8521c976f8b707caeed3e035da39d0218e0724ad7
4
- data.tar.gz: 8e9754766994b37d29dd29a9a44963fa9c90b1d4f90418e8e06bf3863f32ea02
3
+ metadata.gz: fdd9b91dd64522db4ca3d1d4da95870d2118d948b88f77f2c2e4ab729b1ab49f
4
+ data.tar.gz: 2a05bbc38a682f65f40dab0d55b1bf070d0f9905f543df2c7de1c5bd563704f8
5
5
  SHA512:
6
- metadata.gz: 1260729b2209565867c7824850be4450b45565da6fc1d7864d247ff6fcc659b89f1969ef2f9530e2bfc7d8de934a90f3eab585c18fb2c0d8ec3809f8242a864f
7
- data.tar.gz: b876227c57657edb00198bda46eb42e2b2aebdb39379cd61261c9586805a5e736575515b2ddbbd336ed7e1612b603b0f6c30982b3c8e721da9ef849a93f41a2f
6
+ metadata.gz: bce05e0ddc66b433962d60210d4924d80b78e91aba9571932f3cdcad9a1d0df339c0b4e201e7ef366684661e40b1d1c791a61438ed96225a33b58500cff68a40
7
+ data.tar.gz: e9cf9e1afe72c52c77736eae172b6d05dfd831c067d9f854ab18d7eb3727f21794bb11dc61ae5ccae974c26b32fac67111b99f774ecef634909976ba5cb9cb0f
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # ActiveRecord::Events [![Gem version](https://img.shields.io/gem/v/active_record-events)](https://rubygems.org/gems/active_record-events) [![Build status](https://img.shields.io/github/workflow/status/pienkowb/active_record-events/Test/develop)](https://github.com/pienkowb/active_record-events/actions/workflows/test.yml?query=branch%3Adevelop) [![Coverage status](https://img.shields.io/coveralls/github/pienkowb/active_record-events/develop)](https://coveralls.io/github/pienkowb/active_record-events) [![Maintainability status](https://img.shields.io/codeclimate/maintainability/pienkowb/active_record-events)](https://codeclimate.com/github/pienkowb/active_record-events)
1
+ # ActiveRecord::Events [![Gem version](https://img.shields.io/gem/v/active_record-events)](https://rubygems.org/gems/active_record-events) [![Build status](https://img.shields.io/github/actions/workflow/status/pienkowb/active_record-events/test.yml?branch=develop)](https://github.com/pienkowb/active_record-events/actions/workflows/test.yml?query=branch%3Adevelop) [![Coverage status](https://img.shields.io/coveralls/github/pienkowb/active_record-events/develop)](https://coveralls.io/github/pienkowb/active_record-events) [![Maintainability status](https://img.shields.io/codeclimate/maintainability/pienkowb/active_record-events)](https://codeclimate.com/github/pienkowb/active_record-events)
2
2
 
3
3
  An ActiveRecord extension providing convenience methods for timestamp management.
4
4
 
@@ -229,6 +229,7 @@ end
229
229
  - [Oskar Janusz](https://github.com/oskaror)
230
230
  - [Mike Rogers](https://github.com/MikeRogers0)
231
231
  - [Spencer Rogers](https://github.com/serogers)
232
+ - [Benno Bielmeier](https://github.com/bbenno)
232
233
 
233
234
  ## See also
234
235
 
@@ -7,6 +7,7 @@ module ActiveRecord
7
7
  @options = options
8
8
  @naming = Naming.new(event_name, options)
9
9
  @strategy = options[:strategy].try(:to_sym)
10
+ @field_type = options[:field_type].try(:to_sym)
10
11
  end
11
12
 
12
13
  def instance_methods
@@ -21,11 +22,13 @@ module ActiveRecord
21
22
 
22
23
  def class_methods
23
24
  Module.new.tap do |module_|
24
- define_collective_action_method(module_, naming)
25
+ time_class = field_type == :date ? Date : Time
26
+
27
+ define_collective_action_method(module_, naming, time_class)
25
28
 
26
29
  unless options[:skip_scopes]
27
- define_scope_method(module_, naming, strategy)
28
- define_inverse_scope_method(module_, naming, strategy)
30
+ define_scope_method(module_, naming, strategy, time_class)
31
+ define_inverse_scope_method(module_, naming, strategy, time_class)
29
32
  end
30
33
  end
31
34
  end
@@ -35,11 +38,12 @@ module ActiveRecord
35
38
  attr_reader :options
36
39
  attr_reader :naming
37
40
  attr_reader :strategy
41
+ attr_reader :field_type
38
42
 
39
43
  def define_predicate_method(module_, naming, strategy)
40
44
  module_.send(:define_method, naming.predicate) do
41
45
  if strategy == :time_comparison
42
- self[naming.field].present? && self[naming.field].past?
46
+ self[naming.field].present? && !self[naming.field].future?
43
47
  else
44
48
  self[naming.field].present?
45
49
  end
@@ -68,32 +72,32 @@ module ActiveRecord
68
72
  end
69
73
  end
70
74
 
71
- def define_collective_action_method(module_, naming)
75
+ def define_collective_action_method(module_, naming, time_class)
72
76
  module_.send(:define_method, naming.collective_action) do
73
77
  if respond_to?(:touch_all)
74
78
  touch_all(naming.field)
75
79
  else
76
- update_all(naming.field => Time.current)
80
+ update_all(naming.field => time_class.current)
77
81
  end
78
82
  end
79
83
  end
80
84
 
81
- def define_scope_method(module_, naming, strategy)
85
+ def define_scope_method(module_, naming, strategy, time_class)
82
86
  module_.send(:define_method, naming.scope) do
83
87
  if strategy == :time_comparison
84
- where(arel_table[naming.field].lteq(Time.current))
88
+ where(arel_table[naming.field].lteq(time_class.current))
85
89
  else
86
90
  where(arel_table[naming.field].not_eq(nil))
87
91
  end
88
92
  end
89
93
  end
90
94
 
91
- def define_inverse_scope_method(module_, naming, strategy)
95
+ def define_inverse_scope_method(module_, naming, strategy, time_class)
92
96
  module_.send(:define_method, naming.inverse_scope) do
93
97
  arel_field = arel_table[naming.field]
94
98
 
95
99
  if strategy == :time_comparison
96
- where(arel_field.eq(nil).or(arel_field.gt(Time.current)))
100
+ where(arel_field.eq(nil).or(arel_field.gt(time_class.current)))
97
101
  else
98
102
  where(arel_field.eq(nil))
99
103
  end
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module Events
3
- VERSION = '4.1.0'.freeze
3
+ VERSION = '4.1.2'.freeze
4
4
  end
5
5
  end
@@ -74,47 +74,105 @@ RSpec.describe ActiveRecord::Events do
74
74
 
75
75
  describe 'time comparison strategy' do
76
76
  it 'defines a predicate method comparing against current time' do
77
- task.update_columns(expired_at: 1.hour.from_now)
77
+ task.update!(expired_at: 1.hour.from_now)
78
78
  expect(task.expired?).to eq(false)
79
79
 
80
- task.update_columns(expired_at: 1.hour.ago)
80
+ task.update!(expired_at: 1.hour.ago)
81
81
  expect(task.expired?).to eq(true)
82
82
 
83
- task.update_columns(expired_at: nil)
83
+ task.update!(expired_at: nil)
84
84
  expect(task.expired?).to eq(false)
85
85
  end
86
86
 
87
87
  it 'defines an inverse predicate method comparing against current time' do
88
- task.update_columns(expired_at: 1.hour.from_now)
88
+ task.update!(expired_at: 1.hour.from_now)
89
89
  expect(task.not_expired?).to eq(true)
90
90
 
91
- task.update_columns(expired_at: 1.hour.ago)
91
+ task.update!(expired_at: 1.hour.ago)
92
92
  expect(task.not_expired?).to eq(false)
93
93
 
94
- task.update_columns(expired_at: nil)
94
+ task.update!(expired_at: nil)
95
95
  expect(task.not_expired?).to eq(true)
96
96
  end
97
97
 
98
98
  it 'defines a scope comparing against current time' do
99
- task.update_columns(expired_at: 1.hour.from_now)
99
+ task.update!(expired_at: 1.hour.from_now)
100
100
  expect(Task.expired).not_to include(task)
101
101
 
102
- task.update_columns(expired_at: 1.hour.ago)
102
+ task.update!(expired_at: 1.hour.ago)
103
103
  expect(Task.expired).to include(task)
104
104
 
105
- task.update_columns(expired_at: nil)
105
+ task.update!(expired_at: nil)
106
106
  expect(Task.expired).not_to include(task)
107
107
  end
108
108
 
109
109
  it 'defines an inverse scope comparing against current time' do
110
- task.update_columns(expired_at: 1.hour.from_now)
110
+ task.update!(expired_at: 1.hour.from_now)
111
111
  expect(Task.not_expired).to include(task)
112
112
 
113
- task.update_columns(expired_at: 1.hour.ago)
113
+ task.update!(expired_at: 1.hour.ago)
114
114
  expect(Task.not_expired).not_to include(task)
115
115
 
116
- task.update_columns(expired_at: nil)
116
+ task.update!(expired_at: nil)
117
117
  expect(Task.not_expired).to include(task)
118
118
  end
119
+
120
+ context 'with a date field' do
121
+ it 'defines a predicate method comparing against current date' do
122
+ task.update!(notified_on: Date.yesterday)
123
+ expect(task.notified?).to eq(true)
124
+
125
+ task.update!(notified_on: Date.current)
126
+ expect(task.notified?).to eq(true)
127
+
128
+ task.update!(notified_on: Date.tomorrow)
129
+ expect(task.notified?).to eq(false)
130
+
131
+ task.update!(notified_on: nil)
132
+ expect(task.notified?).to eq(false)
133
+ end
134
+
135
+ it 'defines an inverse predicate method comparing against current date' do
136
+ task.update!(notified_on: Date.yesterday)
137
+ expect(task.not_notified?).to eq(false)
138
+
139
+ task.update!(notified_on: Date.current)
140
+ expect(task.not_notified?).to eq(false)
141
+
142
+ task.update!(notified_on: Date.tomorrow)
143
+ expect(task.not_notified?).to eq(true)
144
+
145
+ task.update!(notified_on: nil)
146
+ expect(task.not_notified?).to eq(true)
147
+ end
148
+
149
+ it 'defines a scope comparing against current date' do
150
+ task.update!(notified_on: Date.yesterday)
151
+ expect(Task.notified).to include(task)
152
+
153
+ task.update!(notified_on: Date.current)
154
+ expect(Task.notified).to include(task)
155
+
156
+ task.update!(notified_on: Date.tomorrow)
157
+ expect(Task.notified).not_to include(task)
158
+
159
+ task.update!(notified_on: nil)
160
+ expect(Task.notified).not_to include(task)
161
+ end
162
+
163
+ it 'defines an inverse scope comparing against current date' do
164
+ task.update!(notified_on: Date.yesterday)
165
+ expect(Task.not_notified).not_to include(task)
166
+
167
+ task.update!(notified_on: Date.current)
168
+ expect(Task.not_notified).not_to include(task)
169
+
170
+ task.update!(notified_on: Date.tomorrow)
171
+ expect(Task.not_notified).to include(task)
172
+
173
+ task.update!(notified_on: nil)
174
+ expect(Task.not_notified).to include(task)
175
+ end
176
+ end
119
177
  end
120
178
  end
@@ -1,7 +1,8 @@
1
1
  class Task < ActiveRecord::Base
2
2
  has_event :complete
3
3
  has_event :archive, skip_scopes: true
4
- has_events :expire, strategy: :time_comparison
4
+ has_event :expire, strategy: :time_comparison
5
+ has_event :notify, field_type: :date, strategy: :time_comparison
5
6
 
6
7
  def complete!
7
8
  super
@@ -0,0 +1,5 @@
1
+ class AddNotifiedOnToTasks < ACTIVE_RECORD_MIGRATION_CLASS
2
+ def change
3
+ add_column :tasks, :notified_on, :date
4
+ end
5
+ end
@@ -10,13 +10,14 @@
10
10
  #
11
11
  # It's strongly recommended that you check this file into your version control system.
12
12
 
13
- ActiveRecord::Schema.define(version: 2022_06_09_015338) do
13
+ ActiveRecord::Schema.define(version: 2023_03_08_160359) do
14
14
 
15
15
  create_table "tasks", force: :cascade do |t|
16
16
  t.datetime "completed_at"
17
17
  t.datetime "created_at", null: false
18
18
  t.datetime "updated_at", null: false
19
19
  t.datetime "expired_at"
20
+ t.date "notified_on"
20
21
  end
21
22
 
22
23
  end
data/spec/spec_helper.rb CHANGED
@@ -22,10 +22,20 @@ require 'factory_girl'
22
22
  require 'generator_spec'
23
23
  require 'timecop'
24
24
  require 'zonebie/rspec'
25
+ require 'database_cleaner'
25
26
 
26
27
  Dir["#{__dir__}/support/**/*.rb"].each { |f| require f }
27
28
 
28
29
  RSpec.configure do |config|
30
+ config.before(:suite) do
31
+ DatabaseCleaner.strategy = :transaction
32
+ DatabaseCleaner.clean_with(:truncation)
33
+ end
34
+
35
+ config.around(:each) do |example|
36
+ DatabaseCleaner.cleaning { example.run }
37
+ end
38
+
29
39
  config.include FactoryGirl::Syntax::Methods
30
40
  config.include GeneratorHelpers, type: :generator
31
41
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record-events
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.1.0
4
+ version: 4.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bartosz Pieńkowski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-06-11 00:00:00.000000000 Z
11
+ date: 2023-03-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -52,6 +52,26 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '2.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: database_cleaner
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '1.99'
62
+ - - "<"
63
+ - !ruby/object:Gem::Version
64
+ version: '3'
65
+ type: :development
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '1.99'
72
+ - - "<"
73
+ - !ruby/object:Gem::Version
74
+ version: '3'
55
75
  - !ruby/object:Gem::Dependency
56
76
  name: factory_girl
57
77
  requirement: !ruby/object:Gem::Requirement
@@ -206,6 +226,7 @@ files:
206
226
  - spec/dummy/config/environment.rb
207
227
  - spec/dummy/db/migrate/20150813132804_create_tasks.rb
208
228
  - spec/dummy/db/migrate/20220609015338_add_expired_at_to_tasks.rb
229
+ - spec/dummy/db/migrate/20230308160359_add_notified_on_to_tasks.rb
209
230
  - spec/dummy/db/schema.rb
210
231
  - spec/generators/active_record/event/event_generator_spec.rb
211
232
  - spec/spec_helper.rb
@@ -244,6 +265,7 @@ test_files:
244
265
  - spec/dummy/db/schema.rb
245
266
  - spec/dummy/db/migrate/20150813132804_create_tasks.rb
246
267
  - spec/dummy/db/migrate/20220609015338_add_expired_at_to_tasks.rb
268
+ - spec/dummy/db/migrate/20230308160359_add_notified_on_to_tasks.rb
247
269
  - spec/active_record/events/method_factory_spec.rb
248
270
  - spec/active_record/events/naming_spec.rb
249
271
  - spec/active_record/events/macro_spec.rb