in_time_scope 0.1.3 → 0.1.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 +4 -4
- data/README.md +3 -3
- data/lib/in_time_scope/version.rb +1 -1
- data/lib/in_time_scope.rb +15 -3
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c5ab9dff9b5a5abce4d0418d2a8ec3895126a910a0926f1a449fc55bad3cebd4
|
|
4
|
+
data.tar.gz: '082a9f889d860c0dc7f587fafb77baaa932ccc864e8dfe06b10b92ab9644308a'
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 7df8620094c225af106a3ed909dfa3af5abd328630e1a571cbe0943ecf5a8f965bc1c56286493181305a44fc87a148b055247c12291ee9110b49ca700b45b1bc
|
|
7
|
+
data.tar.gz: 7dc373ec631d291cbdb9c73acaec4bc21d5271495a1131681a3a8aa6929480a28a886010eb3bcc209951825b5c92590a13730aae7ec119a09d96fb66d17f8218
|
data/README.md
CHANGED
|
@@ -277,16 +277,16 @@ The `earliest_in_time(:foreign_key)` scope uses a `NOT EXISTS` subquery to find
|
|
|
277
277
|
|
|
278
278
|
### Error Handling
|
|
279
279
|
|
|
280
|
-
If you specify a scope name but the expected columns don't exist,
|
|
280
|
+
If you specify a scope name but the expected columns don't exist, a `ColumnNotFoundError` is raised at class load time:
|
|
281
281
|
|
|
282
282
|
```ruby
|
|
283
283
|
class Event < ActiveRecord::Base
|
|
284
284
|
include InTimeScope
|
|
285
285
|
|
|
286
|
-
# This will raise
|
|
286
|
+
# This will raise ColumnNotFoundError if hoge_start_at or hoge_end_at columns don't exist
|
|
287
287
|
in_time_scope :hoge
|
|
288
288
|
end
|
|
289
|
-
# =>
|
|
289
|
+
# => InTimeScope::ColumnNotFoundError: Column 'hoge_start_at' does not exist on table 'events'
|
|
290
290
|
```
|
|
291
291
|
|
|
292
292
|
This helps catch configuration errors early during development.
|
data/lib/in_time_scope.rb
CHANGED
|
@@ -5,6 +5,8 @@ require_relative "in_time_scope/version"
|
|
|
5
5
|
|
|
6
6
|
module InTimeScope
|
|
7
7
|
class Error < StandardError; end
|
|
8
|
+
class ColumnNotFoundError < Error; end
|
|
9
|
+
class ConfigurationError < Error; end
|
|
8
10
|
|
|
9
11
|
def self.included(model)
|
|
10
12
|
model.extend ClassMethods
|
|
@@ -18,8 +20,8 @@ module InTimeScope
|
|
|
18
20
|
start_at_column = start_at.fetch(:column, :"#{time_column_prefix}start_at")
|
|
19
21
|
end_at_column = end_at.fetch(:column, :"#{time_column_prefix}end_at")
|
|
20
22
|
|
|
21
|
-
start_at_null = start_at
|
|
22
|
-
end_at_null = end_at
|
|
23
|
+
start_at_null = fetch_null_option(start_at, start_at_column, table_column_hash)
|
|
24
|
+
end_at_null = fetch_null_option(end_at, end_at_column, table_column_hash)
|
|
23
25
|
|
|
24
26
|
scope_method_name = method_name(scope_name, prefix)
|
|
25
27
|
|
|
@@ -28,6 +30,16 @@ module InTimeScope
|
|
|
28
30
|
|
|
29
31
|
private
|
|
30
32
|
|
|
33
|
+
def fetch_null_option(config, column, table_column_hash)
|
|
34
|
+
return nil if column.nil?
|
|
35
|
+
return config[:null] if config.key?(:null)
|
|
36
|
+
|
|
37
|
+
column_info = table_column_hash[column.to_s]
|
|
38
|
+
raise ColumnNotFoundError, "Column '#{column}' does not exist on table '#{table_name}'" if column_info.nil?
|
|
39
|
+
|
|
40
|
+
column_info.null
|
|
41
|
+
end
|
|
42
|
+
|
|
31
43
|
def method_name(scope_name, prefix)
|
|
32
44
|
return :in_time if scope_name == :in_time
|
|
33
45
|
|
|
@@ -38,7 +50,7 @@ module InTimeScope
|
|
|
38
50
|
# Define class-level scope
|
|
39
51
|
if start_at_column.nil? && end_at_column.nil?
|
|
40
52
|
# Both disabled - return all
|
|
41
|
-
|
|
53
|
+
raise ConfigurationError, "At least one of start_at or end_at must be specified"
|
|
42
54
|
elsif end_at_column.nil?
|
|
43
55
|
# Start-only pattern (history tracking)
|
|
44
56
|
define_start_only_scope(scope_method_name, start_at_column, start_at_null)
|