time_for_a_boolean 0.0.6 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +15 -1
- data/lib/time_for_a_boolean.rb +7 -5
- data/lib/time_for_a_boolean/version.rb +1 -1
- data/spec/time_for_a_boolean_spec.rb +33 -0
- metadata +3 -4
- data/.ruby-version +0 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a74e795120301e9c8003f321b892fafc18bf4375
|
4
|
+
data.tar.gz: 910088087ca59037a96fce7271090a3a21c8eecb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8637e3a73d58bb6e98b0d5204a10ae0ae02b7881b1b430444fd61330710ef10a9024f39a59496cc64dbfb2fc7a8f5e657b329a2a125996bdc99be579afa3d940
|
7
|
+
data.tar.gz: 1c459534ccb24b5f005788920d23ac18d088998faac2a20d528bf9ff745515e877e4f847cf5cc0a3b4dcc9c0746e06b52dffa0decbae9c3762934e714f098685
|
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
Time for A Boolean
|
2
2
|
==================
|
3
3
|
|
4
|
-
[![Build Status](https://travis-ci.org/calebthompson/time_for_a_boolean.png)](https://travis-ci.org/calebthompson/time_for_a_boolean)
|
4
|
+
[![Build Status](https://api.travis-ci.org/calebthompson/time_for_a_boolean.png?branch=master)](https://travis-ci.org/calebthompson/time_for_a_boolean)
|
5
5
|
[![Code Climate](https://codeclimate.com/github/calebthompson/time_for_a_boolean.png)](https://codeclimate.com/github/calebthompson/time_for_a_boolean)
|
6
6
|
[![Coverage Status](https://coveralls.io/repos/calebthompson/time_for_a_boolean/badge.png)](https://coveralls.io/r/calebthompson/time_for_a_boolean)
|
7
7
|
|
@@ -77,3 +77,17 @@ Okay... why?
|
|
77
77
|
* `COUNT(posts.deleted_at)` gives you the count of deleted posts, which is
|
78
78
|
useful when writing a report. Define and use `Post.deleted.count` when you
|
79
79
|
have Ruby available.
|
80
|
+
|
81
|
+
Other Options
|
82
|
+
-------------
|
83
|
+
|
84
|
+
If you have a date or time column that does not follow the `attribute_at` convention,
|
85
|
+
you can specify the attribute name:
|
86
|
+
|
87
|
+
```
|
88
|
+
class User < ActiveRecord::Base
|
89
|
+
time_for_a_boolean :expires, :expires_on
|
90
|
+
end
|
91
|
+
```
|
92
|
+
|
93
|
+
This is especially useful when using date only columns.
|
data/lib/time_for_a_boolean.rb
CHANGED
@@ -4,17 +4,19 @@ require "time_for_a_boolean/version"
|
|
4
4
|
require "time_for_a_boolean/railtie"
|
5
5
|
|
6
6
|
module TimeForABoolean
|
7
|
-
def time_for_a_boolean(attribute)
|
7
|
+
def time_for_a_boolean(attribute, field="#{attribute}_at")
|
8
8
|
define_method(attribute) do
|
9
|
-
!send(
|
10
|
-
send("#{attribute}_at") <= -> { Time.current }.()
|
9
|
+
!send(field).nil? && send(field) <= -> { Time.current }.()
|
11
10
|
end
|
11
|
+
|
12
12
|
alias_method "#{attribute}?", attribute
|
13
|
+
|
14
|
+
setter_attribute = "#{field}="
|
13
15
|
define_method("#{attribute}=") do |value|
|
14
16
|
if ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.include?(value)
|
15
|
-
send(
|
17
|
+
send(setter_attribute, -> { Time.current }.())
|
16
18
|
else
|
17
|
-
send(
|
19
|
+
send(setter_attribute, nil)
|
18
20
|
end
|
19
21
|
end
|
20
22
|
end
|
@@ -53,6 +53,39 @@ describe TimeForABoolean do
|
|
53
53
|
|
54
54
|
expect(object.attribute).to be_falsey
|
55
55
|
end
|
56
|
+
|
57
|
+
context 'when the user has defined their own attribute name' do
|
58
|
+
it 'calls nil? on the backing value' do
|
59
|
+
klass.time_for_a_boolean :attribute, :attribute_on
|
60
|
+
date = double(nil?: true)
|
61
|
+
allow(object).to receive(:attribute_on).and_return(date)
|
62
|
+
|
63
|
+
object.attribute
|
64
|
+
|
65
|
+
expect(date).to have_received(:nil?)
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'is true if the attribute is not nil' do
|
69
|
+
klass.time_for_a_boolean :attribute, :attribute_on
|
70
|
+
allow(object).to receive(:attribute_on).and_return(Date.current - 10)
|
71
|
+
|
72
|
+
expect(object.attribute).to be_truthy
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'is false if the attribute is nil' do
|
76
|
+
klass.time_for_a_boolean :attribute, :attribute_on
|
77
|
+
allow(object).to receive(:attribute_on)
|
78
|
+
|
79
|
+
expect(object.attribute).to be_falsey
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'is false if the attribute date is in the future' do
|
83
|
+
klass.time_for_a_boolean :attribute, :attribute_on
|
84
|
+
allow(object).to receive(:attribute_on).and_return(Date.current + 1)
|
85
|
+
|
86
|
+
expect(object.attribute).to be_falsey
|
87
|
+
end
|
88
|
+
end
|
56
89
|
end
|
57
90
|
|
58
91
|
describe 'the query method' do
|
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.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Caleb Thompson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-02-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -105,7 +105,6 @@ extra_rdoc_files: []
|
|
105
105
|
files:
|
106
106
|
- ".gitignore"
|
107
107
|
- ".ruby-gemset"
|
108
|
-
- ".ruby-version"
|
109
108
|
- CONTRIBUTING.md
|
110
109
|
- Gemfile
|
111
110
|
- LICENSE
|
@@ -136,7 +135,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
136
135
|
version: '0'
|
137
136
|
requirements: []
|
138
137
|
rubyforge_project:
|
139
|
-
rubygems_version: 2.
|
138
|
+
rubygems_version: 2.5.1
|
140
139
|
signing_key:
|
141
140
|
specification_version: 4
|
142
141
|
summary: Back boolean values with timestamps
|
data/.ruby-version
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
ruby
|