in_time_scope 0.1.6 → 0.1.7
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/.rubocop.yml +4 -0
- data/lib/in_time_scope/class_methods.rb +44 -0
- data/lib/in_time_scope/version.rb +1 -1
- metadata +17 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f92f298fce80365dc81befd347745a346ebc80dd457d7a1647c4156ffc2a6f4a
|
|
4
|
+
data.tar.gz: eff899e963184dd3bb00ec69a733e6c0a8932e5437e8667ca74b626a31a1a99a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3e02bc23c847d583d9ad88e7f2d80b63b82d7bb2c2e03375d8967e352a91dc88b4b384a740514a3ff868a79bc6eae606e1f1881dd7e6b15c18f2420c346f54f7
|
|
7
|
+
data.tar.gz: 16840ab3e4231d2aba4403501e5fedf532629395ef3ffe0b615d0401d6df15ce7245ef2aba1c91a71cb8b2a3958e93304d87dad8929e6a21de6f210300cfe881
|
data/.rubocop.yml
CHANGED
|
@@ -191,6 +191,12 @@ module InTimeScope
|
|
|
191
191
|
# This scope efficiently finds the latest record per foreign key,
|
|
192
192
|
# suitable for use with has_one associations and includes.
|
|
193
193
|
#
|
|
194
|
+
# @note When multiple records share the same timestamp for a given foreign key,
|
|
195
|
+
# all of them will be returned. This is safe for +has_one+ associations
|
|
196
|
+
# (ActiveRecord picks one), but callers using this as a standalone scope
|
|
197
|
+
# should be aware that the result may contain multiple records per foreign key
|
|
198
|
+
# in the case of timestamp ties.
|
|
199
|
+
#
|
|
194
200
|
# @param suffix [String] The suffix for method names
|
|
195
201
|
# @param column [Symbol] The timestamp column name
|
|
196
202
|
# @return [void]
|
|
@@ -212,6 +218,23 @@ module InTimeScope
|
|
|
212
218
|
.where(p2[column].gt(arel_table[column]))
|
|
213
219
|
.where(p2[:id].not_eq(arel_table[:id]))
|
|
214
220
|
|
|
221
|
+
# Propagate simple equality conditions from the current scope into the NOT EXISTS
|
|
222
|
+
# subquery. This ensures that chained scopes like `.approved.latest_in_time(:user_id)`
|
|
223
|
+
# only consider approved records when searching for "a newer record exists",
|
|
224
|
+
# preventing a newer rejected record from shadowing the latest approved one.
|
|
225
|
+
where_values_hash.each do |col, val|
|
|
226
|
+
next if col.to_s == column.to_s # time column already handled above
|
|
227
|
+
next if col.to_s == foreign_key.to_s # foreign key already handled above
|
|
228
|
+
|
|
229
|
+
subquery = if val.nil?
|
|
230
|
+
subquery.where(p2[col].eq(nil))
|
|
231
|
+
elsif val.is_a?(Array)
|
|
232
|
+
subquery.where(p2[col].in(val))
|
|
233
|
+
else
|
|
234
|
+
subquery.where(p2[col].eq(val))
|
|
235
|
+
end
|
|
236
|
+
end
|
|
237
|
+
|
|
215
238
|
not_exists = Arel::Nodes::Not.new(Arel::Nodes::Exists.new(subquery.ast))
|
|
216
239
|
|
|
217
240
|
where(column => ..time).where(not_exists)
|
|
@@ -223,6 +246,12 @@ module InTimeScope
|
|
|
223
246
|
# This scope efficiently finds the earliest record per foreign key,
|
|
224
247
|
# suitable for use with has_one associations and includes.
|
|
225
248
|
#
|
|
249
|
+
# @note When multiple records share the same timestamp for a given foreign key,
|
|
250
|
+
# all of them will be returned. This is safe for +has_one+ associations
|
|
251
|
+
# (ActiveRecord picks one), but callers using this as a standalone scope
|
|
252
|
+
# should be aware that the result may contain multiple records per foreign key
|
|
253
|
+
# in the case of timestamp ties.
|
|
254
|
+
#
|
|
226
255
|
# @param suffix [String] The suffix for method names
|
|
227
256
|
# @param column [Symbol] The timestamp column name
|
|
228
257
|
# @return [void]
|
|
@@ -244,6 +273,21 @@ module InTimeScope
|
|
|
244
273
|
.where(p2[column].lt(arel_table[column]))
|
|
245
274
|
.where(p2[:id].not_eq(arel_table[:id]))
|
|
246
275
|
|
|
276
|
+
# Propagate simple equality conditions from the current scope into the NOT EXISTS
|
|
277
|
+
# subquery (same reasoning as define_latest_one_scope).
|
|
278
|
+
where_values_hash.each do |col, val|
|
|
279
|
+
next if col.to_s == column.to_s
|
|
280
|
+
next if col.to_s == foreign_key.to_s
|
|
281
|
+
|
|
282
|
+
subquery = if val.nil?
|
|
283
|
+
subquery.where(p2[col].eq(nil))
|
|
284
|
+
elsif val.is_a?(Array)
|
|
285
|
+
subquery.where(p2[col].in(val))
|
|
286
|
+
else
|
|
287
|
+
subquery.where(p2[col].eq(val))
|
|
288
|
+
end
|
|
289
|
+
end
|
|
290
|
+
|
|
247
291
|
not_exists = Arel::Nodes::Not.new(Arel::Nodes::Exists.new(subquery.ast))
|
|
248
292
|
|
|
249
293
|
where(column => ..time).where(not_exists)
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: in_time_scope
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.7
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- kyohah
|
|
@@ -15,14 +15,14 @@ dependencies:
|
|
|
15
15
|
requirements:
|
|
16
16
|
- - ">="
|
|
17
17
|
- !ruby/object:Gem::Version
|
|
18
|
-
version: '
|
|
18
|
+
version: '6.1'
|
|
19
19
|
type: :runtime
|
|
20
20
|
prerelease: false
|
|
21
21
|
version_requirements: !ruby/object:Gem::Requirement
|
|
22
22
|
requirements:
|
|
23
23
|
- - ">="
|
|
24
24
|
- !ruby/object:Gem::Version
|
|
25
|
-
version: '
|
|
25
|
+
version: '6.1'
|
|
26
26
|
- !ruby/object:Gem::Dependency
|
|
27
27
|
name: irb
|
|
28
28
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -121,6 +121,20 @@ dependencies:
|
|
|
121
121
|
- - ">="
|
|
122
122
|
- !ruby/object:Gem::Version
|
|
123
123
|
version: '0'
|
|
124
|
+
- !ruby/object:Gem::Dependency
|
|
125
|
+
name: timecop
|
|
126
|
+
requirement: !ruby/object:Gem::Requirement
|
|
127
|
+
requirements:
|
|
128
|
+
- - ">="
|
|
129
|
+
- !ruby/object:Gem::Version
|
|
130
|
+
version: '0'
|
|
131
|
+
type: :development
|
|
132
|
+
prerelease: false
|
|
133
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
134
|
+
requirements:
|
|
135
|
+
- - ">="
|
|
136
|
+
- !ruby/object:Gem::Version
|
|
137
|
+
version: '0'
|
|
124
138
|
- !ruby/object:Gem::Dependency
|
|
125
139
|
name: yard
|
|
126
140
|
requirement: !ruby/object:Gem::Requirement
|