act_as_time_as_boolean 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -26,6 +26,14 @@ module ActAsTimeAsBoolean
26
26
  ActAsTimeAsBoolean.opposite_field_scope field, options[:opposite]
27
27
  end
28
28
  end
29
+
30
+ if ActAsTimeAsBoolean.is_active_record_inherited?
31
+ ActAsTimeAsBoolean.field_bang_method field
32
+
33
+ if options[:opposite]
34
+ ActAsTimeAsBoolean.opposite_bang_method field, options[:opposite]
35
+ end
36
+ end
29
37
  end
30
38
 
31
39
  def self.field_getter_method(field)
@@ -48,6 +56,13 @@ module ActAsTimeAsBoolean
48
56
  end
49
57
  end
50
58
 
59
+ def self.field_bang_method(field)
60
+ send :define_method, :"#{field}!" do
61
+ send :"#{field}=", true
62
+ save!
63
+ end
64
+ end
65
+
51
66
  def self.opposite_getter_method(field, opposite)
52
67
  send :define_method, :"#{opposite}" do
53
68
  send(:"#{field}_at").nil?
@@ -56,6 +71,13 @@ module ActAsTimeAsBoolean
56
71
  send :alias_method, :"#{opposite}?", :"#{opposite}"
57
72
  end
58
73
 
74
+ def self.opposite_bang_method(field, opposite)
75
+ send :define_method, :"#{opposite}!" do
76
+ send :"#{field}=", false
77
+ save!
78
+ end
79
+ end
80
+
59
81
  def self.field_scope(field)
60
82
  @base.send :scope, field, -> {
61
83
  @base.send :where, [ "#{field}_at IS NOT NULL AND #{field}_at <= ?", Time.current]
@@ -68,9 +90,14 @@ module ActAsTimeAsBoolean
68
90
  }
69
91
  end
70
92
 
71
- def self.has_scope_method?
93
+ def self.is_active_record_inherited?
72
94
  defined?(ActiveRecord::Base) &&
73
95
  ActiveRecord::Base.is_a?(Class) &&
96
+ @base < ActiveRecord::Base
97
+ end
98
+
99
+ def self.has_scope_method?
100
+ is_active_record_inherited? &&
74
101
  ActiveRecord::Base.methods.include?(:scope)
75
102
  end
76
103
  end
@@ -1,3 +1,3 @@
1
1
  module ActAsTimeAsBoolean
2
- VERSION = '0.3.0'
2
+ VERSION = '0.4.0'
3
3
  end
@@ -30,6 +30,9 @@ class ActiveRecord::Base
30
30
  # Nothing to do here, just a mock
31
31
  end
32
32
  end
33
+
34
+ def save!
35
+ end
33
36
  end
34
37
 
35
38
  class InheritedModel < ActiveRecord::Base
@@ -81,39 +84,51 @@ describe ActAsTimeAsBoolean do
81
84
  end
82
85
 
83
86
  describe 'with :active and opposite param' do
84
- subject { TimeAsBooleanWithOppositeModel.new }
87
+ subject { TimeAsBooleanWithOppositeModel.new.methods }
85
88
 
86
89
  it 'defines active method' do
87
- subject.methods.should include(:active)
90
+ subject.should include(:active)
88
91
  end
89
92
  it 'defines active? method' do
90
- subject.methods.should include(:active?)
93
+ subject.should include(:active?)
91
94
  end
92
95
  it 'defines active= method' do
93
- subject.methods.should include(:active=)
96
+ subject.should include(:active=)
94
97
  end
95
98
  it 'defines inactive method' do
96
- subject.methods.should include(:inactive)
99
+ subject.should include(:inactive)
97
100
  end
98
101
  it 'defines inactive? method' do
99
- subject.methods.should include(:inactive?)
102
+ subject.should include(:inactive?)
100
103
  end
101
104
  end
102
105
 
103
106
  describe 'on a rails app' do
104
107
  describe 'with :active param' do
108
+ subject { InheritedModel.new }
109
+
105
110
  it 'define active scope' do
106
- InheritedModel.methods.should include(:active)
111
+ subject.class.methods.should include(:active)
112
+ end
113
+
114
+ it 'defines active! method' do
115
+ subject.methods.should include(:active!)
107
116
  end
108
117
  end
109
118
 
110
119
  describe 'with :active and opposite param' do
120
+ subject { InheritedModelWithOpposite.new }
121
+
111
122
  it 'define active scope' do
112
- InheritedModelWithOpposite.methods.should include(:active)
123
+ subject.class.methods.should include(:active)
113
124
  end
114
125
 
115
126
  it 'define inactive scope' do
116
- InheritedModelWithOpposite.methods.should include(:inactive)
127
+ subject.class.methods.should include(:inactive)
128
+ end
129
+
130
+ it 'defines inactive! method' do
131
+ subject.methods.should include(:inactive!)
117
132
  end
118
133
  end
119
134
  end
@@ -204,5 +219,31 @@ describe ActAsTimeAsBoolean do
204
219
  it { subject.inactive?.should be_true }
205
220
  end
206
221
  end
222
+
223
+ describe 'with an ActiveRecord inherited instance' do
224
+ subject { InheritedModel.new }
225
+
226
+ describe 'calling active!' do
227
+ before { subject.active! }
228
+
229
+ it { subject.active?.should be_true }
230
+ end
231
+ end
232
+
233
+ describe 'with an ActiveRecord inherited with opposite instance' do
234
+ subject { InheritedModelWithOpposite.new }
235
+
236
+ describe 'calling active!' do
237
+ before { subject.active! }
238
+
239
+ it { subject.active?.should be_true }
240
+ end
241
+
242
+ describe 'calling inactive!' do
243
+ before { subject.inactive! }
244
+
245
+ it { subject.active?.should be_false }
246
+ end
247
+ end
207
248
  end
208
249
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: act_as_time_as_boolean
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-02-04 00:00:00.000000000 Z
12
+ date: 2014-02-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake