active_record-events 4.1.0 → 4.1.1
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 +4 -4
- data/README.md +2 -1
- data/lib/active_record/events/method_factory.rb +14 -10
- data/lib/active_record/events/version.rb +1 -1
- data/spec/active_record/events_spec.rb +70 -12
- data/spec/dummy/app/models/task.rb +2 -1
- data/spec/dummy/db/migrate/20230308160359_add_notified_on_to_tasks.rb +5 -0
- data/spec/dummy/db/schema.rb +2 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7ec4cd90a44c6c550a55b4fed0e50686380441cb86a4089a9716d8d5db350984
|
4
|
+
data.tar.gz: bf03cc5c153173f2bf842d240374e00e7a94957fcdd4b855dab0a00fc4999a12
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7c567f576431e641cd39dad28b71c033093b8cd51e368003ec93b1a44ff1177ac0272a257d1b73b2398072aeb4d962d568865bbd110e2116085d76b513c5da3a
|
7
|
+
data.tar.gz: 1b83157999bf3ad63a76475fcf4b011f0f95cd19d83792f1db2a9f5eaedcca178a69fcfccb01898f7d9452ed06d2ba1c28cac3bb0fe657f5e6ed7304b2db4dd2
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# ActiveRecord::Events [](https://rubygems.org/gems/active_record-events) [](https://rubygems.org/gems/active_record-events) [](https://github.com/pienkowb/active_record-events/actions/workflows/test.yml?query=branch%3Adevelop) [](https://coveralls.io/github/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
|
-
|
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].
|
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 =>
|
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(
|
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(
|
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
|
@@ -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.
|
77
|
+
task.update!(expired_at: 1.hour.from_now)
|
78
78
|
expect(task.expired?).to eq(false)
|
79
79
|
|
80
|
-
task.
|
80
|
+
task.update!(expired_at: 1.hour.ago)
|
81
81
|
expect(task.expired?).to eq(true)
|
82
82
|
|
83
|
-
task.
|
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.
|
88
|
+
task.update!(expired_at: 1.hour.from_now)
|
89
89
|
expect(task.not_expired?).to eq(true)
|
90
90
|
|
91
|
-
task.
|
91
|
+
task.update!(expired_at: 1.hour.ago)
|
92
92
|
expect(task.not_expired?).to eq(false)
|
93
93
|
|
94
|
-
task.
|
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.
|
99
|
+
task.update!(expired_at: 1.hour.from_now)
|
100
100
|
expect(Task.expired).not_to include(task)
|
101
101
|
|
102
|
-
task.
|
102
|
+
task.update!(expired_at: 1.hour.ago)
|
103
103
|
expect(Task.expired).to include(task)
|
104
104
|
|
105
|
-
task.
|
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.
|
110
|
+
task.update!(expired_at: 1.hour.from_now)
|
111
111
|
expect(Task.not_expired).to include(task)
|
112
112
|
|
113
|
-
task.
|
113
|
+
task.update!(expired_at: 1.hour.ago)
|
114
114
|
expect(Task.not_expired).not_to include(task)
|
115
115
|
|
116
|
-
task.
|
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.today)
|
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.today)
|
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.today)
|
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.today)
|
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
|
-
|
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
|
data/spec/dummy/db/schema.rb
CHANGED
@@ -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:
|
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
|
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.
|
4
|
+
version: 4.1.1
|
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:
|
11
|
+
date: 2023-03-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -206,6 +206,7 @@ files:
|
|
206
206
|
- spec/dummy/config/environment.rb
|
207
207
|
- spec/dummy/db/migrate/20150813132804_create_tasks.rb
|
208
208
|
- spec/dummy/db/migrate/20220609015338_add_expired_at_to_tasks.rb
|
209
|
+
- spec/dummy/db/migrate/20230308160359_add_notified_on_to_tasks.rb
|
209
210
|
- spec/dummy/db/schema.rb
|
210
211
|
- spec/generators/active_record/event/event_generator_spec.rb
|
211
212
|
- spec/spec_helper.rb
|
@@ -244,6 +245,7 @@ test_files:
|
|
244
245
|
- spec/dummy/db/schema.rb
|
245
246
|
- spec/dummy/db/migrate/20150813132804_create_tasks.rb
|
246
247
|
- spec/dummy/db/migrate/20220609015338_add_expired_at_to_tasks.rb
|
248
|
+
- spec/dummy/db/migrate/20230308160359_add_notified_on_to_tasks.rb
|
247
249
|
- spec/active_record/events/method_factory_spec.rb
|
248
250
|
- spec/active_record/events/naming_spec.rb
|
249
251
|
- spec/active_record/events/macro_spec.rb
|