time_for_a_boolean 0.2.2 → 0.3.0
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 +22 -0
- data/lib/time_for_a_boolean/version.rb +1 -1
- data/lib/time_for_a_boolean.rb +13 -1
- data/spec/time_for_a_boolean_spec.rb +30 -0
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 701b2822fe90c9d2a06985ff8db6d8c46501bf57921c47f741f0bee56e6c52d3
|
|
4
|
+
data.tar.gz: 473ee35b769e3c8a422e03647d51ab93a962a9ed3acb61d538e470184d610061
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 0b410a85f621d1df5c9830d3449290621d88d28b59178ccec0fc31a53b985106113e504883e925e1d2f32c4a93ad24047903cdb0805a83e0a361d6e3b1632d74
|
|
7
|
+
data.tar.gz: ac178e46e85b90df61b2cbbb83f4219a4a3ba2ac5dc61ac28899938bbbd73cacf72266d117b15688286a7ca1d0e2267cb02e8d229ed64f1e507a58ba8afa1223
|
data/README.md
CHANGED
|
@@ -81,6 +81,8 @@ Okay... why?
|
|
|
81
81
|
Other Options
|
|
82
82
|
-------------
|
|
83
83
|
|
|
84
|
+
### Using a different attribute name
|
|
85
|
+
|
|
84
86
|
If you have a date or time column that does not follow the `attribute_at` convention,
|
|
85
87
|
you can specify the attribute name:
|
|
86
88
|
|
|
@@ -91,3 +93,23 @@ end
|
|
|
91
93
|
```
|
|
92
94
|
|
|
93
95
|
This is especially useful when using date only columns.
|
|
96
|
+
|
|
97
|
+
### Scopes to filter records
|
|
98
|
+
|
|
99
|
+
[ActiveRecord Scopes](https://guides.rubyonrails.org/active_record_querying.html#scopes)
|
|
100
|
+
can be used to filter records based on the attribute. These are not defined by
|
|
101
|
+
default, but can be added by passing the `scopes: true` option:
|
|
102
|
+
|
|
103
|
+
```ruby
|
|
104
|
+
class Post < ActiveRecord::Base
|
|
105
|
+
time_for_a_boolean :deleted, scopes: true
|
|
106
|
+
...
|
|
107
|
+
end
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
This will define the following scopes:
|
|
111
|
+
|
|
112
|
+
| Scope | Description |
|
|
113
|
+
| -------------------- | ----------- |
|
|
114
|
+
| `Post.deleted` | Returns all posts where `deleted_at` is set to a time before `Time.current`
|
|
115
|
+
| `Post.not_deleted` | Returns all posts where `deleted_at` is unset or in the future
|
data/lib/time_for_a_boolean.rb
CHANGED
|
@@ -6,7 +6,7 @@ require "time_for_a_boolean/version"
|
|
|
6
6
|
require "time_for_a_boolean/railtie"
|
|
7
7
|
|
|
8
8
|
module TimeForABoolean
|
|
9
|
-
def time_for_a_boolean(attribute, field=:"#{attribute}_at")
|
|
9
|
+
def time_for_a_boolean(attribute, field=:"#{attribute}_at", scopes: false)
|
|
10
10
|
define_method(attribute) do
|
|
11
11
|
!send(field).nil? && send(field) <= -> { Time.current }.()
|
|
12
12
|
end
|
|
@@ -25,5 +25,17 @@ module TimeForABoolean
|
|
|
25
25
|
define_method(:"#{attribute}!") do
|
|
26
26
|
send(:"#{attribute}=", true)
|
|
27
27
|
end
|
|
28
|
+
|
|
29
|
+
if scopes && respond_to?(:where)
|
|
30
|
+
singleton_class.instance_eval do
|
|
31
|
+
define_method(:"#{attribute}") do
|
|
32
|
+
where(field => ..Time.current)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
define_method(:"not_#{attribute}") do
|
|
36
|
+
where(field => [nil, Time.current...])
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
28
40
|
end
|
|
29
41
|
end
|
|
@@ -153,12 +153,42 @@ describe TimeForABoolean do
|
|
|
153
153
|
end
|
|
154
154
|
end
|
|
155
155
|
|
|
156
|
+
describe 'scopes' do
|
|
157
|
+
it 'does not define the scopes by default' do
|
|
158
|
+
klass.time_for_a_boolean :attribute
|
|
159
|
+
|
|
160
|
+
expect(klass).not_to respond_to :attribute
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
it 'does not define the scopes if the class does not respond to where' do
|
|
164
|
+
klass.time_for_a_boolean :attribute, scopes: true
|
|
165
|
+
|
|
166
|
+
expect(klass).not_to respond_to :attribute
|
|
167
|
+
expect(klass).not_to respond_to :not_attribute
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
it 'defines the scopes if the class has a where method' do
|
|
171
|
+
klass_with_where.time_for_a_boolean :attribute, scopes: true
|
|
172
|
+
|
|
173
|
+
expect(klass_with_where).to respond_to :attribute
|
|
174
|
+
expect(klass_with_where).to respond_to :not_attribute
|
|
175
|
+
end
|
|
176
|
+
end
|
|
156
177
|
def klass
|
|
157
178
|
@klass ||= Class.new do
|
|
158
179
|
extend TimeForABoolean
|
|
159
180
|
end
|
|
160
181
|
end
|
|
161
182
|
|
|
183
|
+
def klass_with_where
|
|
184
|
+
@klass ||= Class.new do
|
|
185
|
+
extend TimeForABoolean
|
|
186
|
+
|
|
187
|
+
def self.where(...)
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
|
|
162
192
|
def object
|
|
163
193
|
@object ||= klass.new
|
|
164
194
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: time_for_a_boolean
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Caleb Hearth
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2026-01-13 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activerecord
|