time_for_a_boolean 0.2.1 → 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: af337b26f0dc5ce70747c5146b92e0a115d987ed835cd3131e42aca5611bb58c
4
- data.tar.gz: 59329bac85be78bad931c35fd629278d5676419f2ae8aeaba13c1e7312f55695
3
+ metadata.gz: 701b2822fe90c9d2a06985ff8db6d8c46501bf57921c47f741f0bee56e6c52d3
4
+ data.tar.gz: 473ee35b769e3c8a422e03647d51ab93a962a9ed3acb61d538e470184d610061
5
5
  SHA512:
6
- metadata.gz: 4a78da81585e4c74ec3a3560308020390b3a089726d9461796a9a9b0481f38f9f5de88710e276a9a08d241059942542e8bfa0630d317e8a55be18ef1a800d2eb
7
- data.tar.gz: e9ae065b1573973fc993ec6a918a770261383baa36b41cd0b09dbc77cab9cf40e917bd3c2fffb032c58246585e109e22f379997826d1457150e6901b502164b3
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.1'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -1,3 +1,4 @@
1
+ require "active_support"
1
2
  require "active_support/core_ext/module/delegation"
2
3
  require "active_support/core_ext/time/calculations"
3
4
  require "active_model/type"
@@ -5,7 +6,7 @@ require "time_for_a_boolean/version"
5
6
  require "time_for_a_boolean/railtie"
6
7
 
7
8
  module TimeForABoolean
8
- def time_for_a_boolean(attribute, field=:"#{attribute}_at")
9
+ def time_for_a_boolean(attribute, field=:"#{attribute}_at", scopes: false)
9
10
  define_method(attribute) do
10
11
  !send(field).nil? && send(field) <= -> { Time.current }.()
11
12
  end
@@ -24,5 +25,17 @@ module TimeForABoolean
24
25
  define_method(:"#{attribute}!") do
25
26
  send(:"#{attribute}=", true)
26
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
27
40
  end
28
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
@@ -6,20 +6,24 @@ require "time_for_a_boolean/version"
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "time_for_a_boolean"
8
8
  spec.version = TimeForABoolean::VERSION
9
- spec.authors = ["Caleb Thompson"]
10
- spec.email = ["caleb@calebthompson.io"]
9
+ spec.authors = ["Caleb Hearth"]
10
+ spec.email = ["caleb@calebhearth.com"]
11
11
  spec.description = <<-DESCRIPTION
12
12
  Use timestamp values to represent boolean data such as deleted, published,
13
13
  or subscribed.
14
14
  DESCRIPTION
15
15
  spec.summary = "Back boolean values with timestamps"
16
- spec.homepage = "https://github.com/calebthompson/time_for_a_boolean"
16
+ spec.homepage = "https://github.com/calebhearth/time_for_a_boolean"
17
17
  spec.license = "MIT"
18
18
 
19
19
  spec.files = `git ls-files`.split($/)
20
20
  spec.test_files = spec.files.grep(%r{^spec/})
21
21
  spec.require_paths = ["lib"]
22
22
 
23
+ spec.metadata = {
24
+ "funding-uri" => "https://github.com/sponsors/calebhearth"
25
+ }
26
+
23
27
  spec.add_dependency "activerecord"
24
28
  spec.add_dependency "railties"
25
29
 
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.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
- - Caleb Thompson
7
+ - Caleb Hearth
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-09-15 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
@@ -98,7 +98,7 @@ description: |2
98
98
  Use timestamp values to represent boolean data such as deleted, published,
99
99
  or subscribed.
100
100
  email:
101
- - caleb@calebthompson.io
101
+ - caleb@calebhearth.com
102
102
  executables: []
103
103
  extensions: []
104
104
  extra_rdoc_files: []
@@ -115,10 +115,11 @@ files:
115
115
  - lib/time_for_a_boolean/version.rb
116
116
  - spec/time_for_a_boolean_spec.rb
117
117
  - time_for_a_boolean.gemspec
118
- homepage: https://github.com/calebthompson/time_for_a_boolean
118
+ homepage: https://github.com/calebhearth/time_for_a_boolean
119
119
  licenses:
120
120
  - MIT
121
- metadata: {}
121
+ metadata:
122
+ funding-uri: https://github.com/sponsors/calebhearth
122
123
  post_install_message:
123
124
  rdoc_options: []
124
125
  require_paths:
@@ -134,7 +135,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
134
135
  - !ruby/object:Gem::Version
135
136
  version: '0'
136
137
  requirements: []
137
- rubygems_version: 3.1.2
138
+ rubygems_version: 3.5.9
138
139
  signing_key:
139
140
  specification_version: 4
140
141
  summary: Back boolean values with timestamps