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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f46c50d90f896b920a86f45ea49329a9629843c4c7550e065f22c4b20df35149
4
- data.tar.gz: 6c927de51e73ba689314a7caaf5f419a23af0f61f35b80c09f1c5d4cfa63273d
3
+ metadata.gz: f92f298fce80365dc81befd347745a346ebc80dd457d7a1647c4156ffc2a6f4a
4
+ data.tar.gz: eff899e963184dd3bb00ec69a733e6c0a8932e5437e8667ca74b626a31a1a99a
5
5
  SHA512:
6
- metadata.gz: 4d861b025f3f455367ef485e40df6f98d83e55b1fd3c3b5fdf2cc6f57bbae2dd3dec94586bb1265b76859a83a3bd0c782a523315cf5188e3ccf9f62a8dcb6b2f
7
- data.tar.gz: f712f1b0e82dfa596c1d805ef5f7ddd715bc39eb1fffee564df6eb98a68582f21d1f9822f7980471d91c70823e86c1e693599adcb2d77eb96c909059e7d90576
6
+ metadata.gz: 3e02bc23c847d583d9ad88e7f2d80b63b82d7bb2c2e03375d8967e352a91dc88b4b384a740514a3ff868a79bc6eae606e1f1881dd7e6b15c18f2420c346f54f7
7
+ data.tar.gz: 16840ab3e4231d2aba4403501e5fedf532629395ef3ffe0b615d0401d6df15ce7245ef2aba1c91a71cb8b2a3958e93304d87dad8929e6a21de6f210300cfe881
data/.rubocop.yml CHANGED
@@ -39,5 +39,9 @@ Layout/LineLength:
39
39
  Style/Documentation:
40
40
  Enabled: false
41
41
 
42
+ Style/OneClassPerFile:
43
+ Exclude:
44
+ - "spec/support/create_test_database.rb"
45
+
42
46
  Gemspec/DevelopmentDependencies:
43
47
  EnforcedStyle: gemspec
@@ -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)
@@ -2,5 +2,5 @@
2
2
 
3
3
  module InTimeScope
4
4
  # The current version of the InTimeScope gem
5
- VERSION = "0.1.6"
5
+ VERSION = "0.1.7"
6
6
  end
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.6
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: '0'
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: '0'
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