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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bbf0c85160e6f03e435e5c8cd3002883f16881f8
4
- data.tar.gz: 832ed8dd09cb0f949c352fe31ec7025ce473a5f8
3
+ metadata.gz: a74e795120301e9c8003f321b892fafc18bf4375
4
+ data.tar.gz: 910088087ca59037a96fce7271090a3a21c8eecb
5
5
  SHA512:
6
- metadata.gz: b57c1ccca319e9ad9a5540884df5b32837db8a60151750619435205920a05d5bb8b46758a4f1163aba265e162345315869233f55f995479d876e3c0f2459eb9c
7
- data.tar.gz: 8134bd168033a61a6588f5a3538c6ab52f5e18e00353e8b2a03f6108cbdc8dcd23cf9705e4bdfd50953a495f4f9e309be613c208111e5db82a707efa507c6e22
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.
@@ -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("#{attribute}_at").nil? &&
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("#{attribute}_at=", -> { Time.current }.())
17
+ send(setter_attribute, -> { Time.current }.())
16
18
  else
17
- send("#{attribute}_at=", nil)
19
+ send(setter_attribute, nil)
18
20
  end
19
21
  end
20
22
  end
@@ -1,3 +1,3 @@
1
1
  module TimeForABoolean
2
- VERSION = '0.0.6'
2
+ VERSION = '0.1.0'
3
3
  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.6
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: 2015-04-29 00:00:00.000000000 Z
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.4.6
138
+ rubygems_version: 2.5.1
140
139
  signing_key:
141
140
  specification_version: 4
142
141
  summary: Back boolean values with timestamps
@@ -1 +0,0 @@
1
- ruby