date_as_bool 0.1.0 → 0.1.1
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 +67 -43
- data/Rakefile +0 -6
- data/lib/date_as_bool.rb +0 -5
- data/lib/date_as_bool/active_record/as/bool.rb +19 -17
- data/lib/date_as_bool/version.rb +1 -1
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bdc34c40345621e238b8a0c9a09fc7f74dc6eb0e
|
4
|
+
data.tar.gz: 04b0263600b2144c2a986c86a5d875bd9514fd72
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 97eb4ed694c678696252be8eb64a06a10bb5adcbef6cf7b66d8419ac5211af4fe2cd53d8795df9cdc35aa0d23f4dd44f8879fc3b671c9a876c749d68e44a2bac
|
7
|
+
data.tar.gz: c85e04a19289a5745bdb3c96e42a56f6405db777db3ff1f9697a31e6142b4b2d739bd4cd2c53e9b7d016afd63597f9677352a9fe1ab3b32e7a9ff289b68f6650
|
data/README.md
CHANGED
@@ -1,36 +1,23 @@
|
|
1
1
|
# DateAsBool
|
2
|
-
A simple Rails Gem to get both a datetime and a boolean behaviour from the same database column. I found myself quite often with the following design pattern:
|
3
2
|
|
4
|
-
|
5
|
-
# == Schema Information
|
6
|
-
#
|
7
|
-
# Table name: subscriptions
|
8
|
-
#
|
9
|
-
# id :integer not null, primary key
|
10
|
-
# paid_at :datetime
|
11
|
-
# ...
|
12
|
-
#
|
3
|
+
DateAsBool is a simple Rails Gem to get both a datetime and a boolean behaviour from the same database column.
|
13
4
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
def paid
|
20
|
-
paid_at.present?
|
21
|
-
end
|
22
|
-
def paid!
|
23
|
-
paid = true
|
24
|
-
save
|
25
|
-
end
|
26
|
-
def paid?
|
27
|
-
paid_at.present?
|
28
|
-
end
|
29
|
-
...
|
5
|
+
## Installation
|
6
|
+
Add this line to your application's Gemfile:
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
gem 'date_as_bool'
|
30
10
|
```
|
31
11
|
|
32
|
-
|
12
|
+
And then execute:
|
13
|
+
```bash
|
14
|
+
$ bundle
|
15
|
+
```
|
33
16
|
|
17
|
+
Or install it yourself as:
|
18
|
+
```bash
|
19
|
+
$ gem install date_as_bool
|
20
|
+
```
|
34
21
|
|
35
22
|
## Usage
|
36
23
|
Let's say you have a `users` table with column `verified_at:datetime`.
|
@@ -42,33 +29,70 @@ class User < ApplicationRecord
|
|
42
29
|
...
|
43
30
|
```
|
44
31
|
|
45
|
-
Now you can use `user.verified?` to test for `verified_at` presence, and `user.verified!` as an alias for `user.update(verified_at: Time.now)`.
|
32
|
+
Now you can use `user.verified` or `user.verified?` to test for `verified_at` presence, and `user.verified!` as an alias for `user.update(verified_at: Time.now)`.
|
46
33
|
|
47
|
-
You can
|
34
|
+
You can change the field with `user.update(verified: bool)`. If false, it resets `verified_at` to `nil`, if true, it sets it to `Time.now`.
|
48
35
|
|
36
|
+
You also get the scopes `User.verified`, equivalent to `User.where.not(verified_at: nil)`, and `User.not_verified` equivalent to `User.where(verified_at: nil)`.
|
49
37
|
|
50
|
-
|
51
|
-
If you want to give a different name to the boolean method, you can specify it as the second argument: `date_as_bool :verified_at` gets translated to `date_as_bool :verified_at, :verified`.
|
38
|
+
### Methods
|
52
39
|
|
53
|
-
|
54
|
-
|
40
|
+
* `User.verified == User.where.not(verified_at: nil)`
|
41
|
+
* `User.not_verified == User.where(verified_at: nil)`
|
42
|
+
* `user.verified == user.verified_at.present?`
|
43
|
+
* `user.verified? == user.verified_at.present?`
|
44
|
+
* `user.verified! == user.update(verified: true) == user.update(verified_at: Time.now)`
|
45
|
+
* `user.update(verified: false) == user.update(verified_at: nil)`
|
46
|
+
|
47
|
+
## Motivation
|
48
|
+
|
49
|
+
Often, when you need a boolean flag on a model, you can accomplish more by using a datetime attribute instead, that captures the first time the flag turned on. But to recover the simplicity of dealing with a boolean instead of a datetime you have to write some lines of code.
|
50
|
+
|
51
|
+
So, I found myself quite often with the following design pattern:
|
55
52
|
|
56
53
|
```ruby
|
57
|
-
|
58
|
-
|
54
|
+
# == Schema Information
|
55
|
+
#
|
56
|
+
# Table name: users
|
57
|
+
#
|
58
|
+
# id :integer not null, primary key
|
59
|
+
# verified_at :datetime
|
60
|
+
# ...
|
61
|
+
#
|
59
62
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
```
|
63
|
+
class User < ApplicationRecord
|
64
|
+
scope :verified, -> { where.not(verified_at: nil) }
|
65
|
+
scope :not_verified, -> { where(verified_at: nil) }
|
64
66
|
|
65
|
-
|
66
|
-
|
67
|
-
|
67
|
+
def verified=(bool)
|
68
|
+
self.verified_at = bool ? Time.now : nil
|
69
|
+
end
|
70
|
+
|
71
|
+
def verified
|
72
|
+
verified_at.present?
|
73
|
+
end
|
74
|
+
alias verified? verified
|
75
|
+
|
76
|
+
def verified!
|
77
|
+
verified = true
|
78
|
+
save
|
79
|
+
end
|
80
|
+
|
81
|
+
# ...
|
82
|
+
end
|
68
83
|
```
|
69
84
|
|
85
|
+
This way, I can both use `User.verified` and `User.where(verified_at: 2.days.ago..Time.now)`.
|
86
|
+
|
87
|
+
DateAsBool abstracts this behaviour in one line.
|
88
|
+
|
89
|
+
## Advanced Usage
|
90
|
+
If you want to give a different name to the boolean method, you can specify it as the second argument.
|
91
|
+
|
92
|
+
`date_as_bool :verified_at` is just a shortcut for `date_as_bool :verified_at, :verified`.
|
93
|
+
|
70
94
|
## Contributing
|
71
|
-
|
95
|
+
Bug reports and pull requests are greatly appreciated.
|
72
96
|
|
73
97
|
## License
|
74
98
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
CHANGED
@@ -14,11 +14,6 @@ RDoc::Task.new(:rdoc) do |rdoc|
|
|
14
14
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
15
|
end
|
16
16
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
17
|
require 'bundler/gem_tasks'
|
23
18
|
|
24
19
|
require 'rake/testtask'
|
@@ -29,5 +24,4 @@ Rake::TestTask.new(:test) do |t|
|
|
29
24
|
t.verbose = false
|
30
25
|
end
|
31
26
|
|
32
|
-
|
33
27
|
task default: :test
|
data/lib/date_as_bool.rb
CHANGED
@@ -1,34 +1,36 @@
|
|
1
1
|
module ActiveRecord
|
2
2
|
module As #:nodoc:
|
3
3
|
module Bool #:nodoc:
|
4
|
+
module ClassMethods #:nodoc:
|
5
|
+
def date_as_bool(datetime_name, bool_name = nil)
|
6
|
+
bool_name ||= datetime_name.to_s.split('_')[0..-2].join('_').to_sym
|
4
7
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
singleton_class.class_eval do
|
9
|
+
define_method bool_name do
|
10
|
+
where.not(datetime_name => nil)
|
11
|
+
end
|
12
|
+
define_method "not_#{bool_name}" do
|
13
|
+
where(datetime_name => nil)
|
14
|
+
end
|
11
15
|
end
|
12
16
|
|
13
|
-
|
14
|
-
attr_accessor bool_name
|
17
|
+
class_eval do
|
15
18
|
define_method bool_name do
|
16
|
-
|
19
|
+
send(datetime_name).present?
|
20
|
+
end
|
21
|
+
define_method :"#{bool_name}?" do
|
22
|
+
send(bool_name)
|
17
23
|
end
|
18
24
|
define_method :"#{bool_name}=" do |bool|
|
19
|
-
|
25
|
+
send(:"#{datetime_name}=", bool ? Time.now : nil)
|
20
26
|
end
|
21
27
|
define_method :"#{bool_name}!" do
|
22
|
-
|
23
|
-
|
24
|
-
end
|
25
|
-
define_method :"#{bool_name}?" do
|
26
|
-
self.send(bool_name)
|
28
|
+
send(:"#{bool_name}=", true)
|
29
|
+
save!
|
27
30
|
end
|
28
31
|
end
|
29
|
-
|
30
32
|
end
|
31
33
|
end
|
32
34
|
end
|
33
35
|
end
|
34
|
-
end
|
36
|
+
end
|
data/lib/date_as_bool/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: date_as_bool
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Carlo Martinucci
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-07-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -38,9 +38,9 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
-
description:
|
42
|
-
the same database column
|
43
|
-
|
41
|
+
description: DateAsBool is a simple Rails Gem to get both a datetime and a boolean
|
42
|
+
behaviour from the same database column. Save verified_at:datetime on DB, use user.verified
|
43
|
+
for instances and User.verified as a scope.
|
44
44
|
email:
|
45
45
|
- carlo.martinucci@gmail.com
|
46
46
|
executables: []
|
@@ -78,5 +78,5 @@ rubyforge_project:
|
|
78
78
|
rubygems_version: 2.6.8
|
79
79
|
signing_key:
|
80
80
|
specification_version: 4
|
81
|
-
summary: Get both a datetime and a boolean behaviour
|
81
|
+
summary: Get both a datetime and a boolean behaviour from the same database column.
|
82
82
|
test_files: []
|