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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '0901b7d1345001ec725cbb1b9a8611d8014f611f539631f6a71bfdf03c6bbc3a'
4
- data.tar.gz: 9a202da422fad3672d96941d6d5c7b3578f742ec0c5253ef42617217d65013e5
3
+ metadata.gz: 701b2822fe90c9d2a06985ff8db6d8c46501bf57921c47f741f0bee56e6c52d3
4
+ data.tar.gz: 473ee35b769e3c8a422e03647d51ab93a962a9ed3acb61d538e470184d610061
5
5
  SHA512:
6
- metadata.gz: 17bb87c43aa0d9b10e83001f206105bf89ca36f4ce62569ad6eb2d8f9bdc9c51b06c3824f5700c3d5dabb91431908a819d8138b65cc400685732418ee985586d
7
- data.tar.gz: 83f9c14b99d5c6c432708a67f155b65a641cfff762e94e2f6757e53fb8240abef572e6ccf98a0141cd2847a3123d4c9ee03ca7594dd3a68b8fc38bf0355a66dc
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
@@ -1,3 +1,3 @@
1
1
  module TimeForABoolean
2
- VERSION = '0.2.2'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -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.2.2
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: 2024-05-21 00:00:00.000000000 Z
11
+ date: 2026-01-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord