affairs_of_state 0.0.4 → 0.0.5
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.
- data/.travis.yml +6 -0
- data/README.md +7 -1
- data/lib/affairs_of_state/version.rb +1 -1
- data/lib/affairs_of_state.rb +2 -2
- data/spec/affairs_of_state_spec.rb +54 -2
- metadata +11 -10
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -38,6 +38,12 @@ Or give it a long list of statuses:
|
|
38
38
|
|
39
39
|
affairs_of_state :ordered, :cancelled, :shipped, :lost, :in_transit
|
40
40
|
|
41
|
+
You can also pass a proc or a method name symbol to the :if option to bypass validation:
|
42
|
+
|
43
|
+
affairs_of_state :active, :inactive, :if => lambda{|object| only_validate_if_this_is_true(object) }
|
44
|
+
# or
|
45
|
+
affairs_of_state :active, :inactive, :if => :only_validate_if_this_method_returns_true
|
46
|
+
|
41
47
|
|
42
48
|
## Methods
|
43
49
|
|
@@ -54,7 +60,7 @@ You can also access all your statuses on the model like so:
|
|
54
60
|
It also provides scopes automagically:
|
55
61
|
|
56
62
|
Widget.active
|
57
|
-
|
63
|
+
|
58
64
|
Widget.cancelled
|
59
65
|
|
60
66
|
|
data/lib/affairs_of_state.rb
CHANGED
@@ -8,12 +8,12 @@ module AffairsOfState
|
|
8
8
|
|
9
9
|
module ClassMethods
|
10
10
|
def affairs_of_state(*args)
|
11
|
-
@_status_options = ({:column => :status, :allow_blank => false, :scopes => true}).merge(args.extract_options!)
|
11
|
+
@_status_options = ({:column => :status, :allow_blank => false, :scopes => true, :if => nil}).merge(args.extract_options!)
|
12
12
|
@_statuses = args.flatten.map(&:to_s)
|
13
13
|
|
14
14
|
const_set("STATUSES", @_statuses)
|
15
15
|
|
16
|
-
validates(@_status_options[:column], :inclusion => {:in => @_statuses, :allow_blank => @_status_options[:allow_blank]})
|
16
|
+
validates(@_status_options[:column], :inclusion => {:in => @_statuses, :allow_blank => @_status_options[:allow_blank]}, :if => @_status_options[:if])
|
17
17
|
|
18
18
|
if @_status_options[:scopes]
|
19
19
|
@_statuses.each do |status|
|
@@ -94,7 +94,7 @@ describe AffairsOfState do
|
|
94
94
|
self.table_name = "pies"
|
95
95
|
|
96
96
|
affairs_of_state [:on, :off]
|
97
|
-
end
|
97
|
+
end
|
98
98
|
|
99
99
|
it "should work too if that's what floats your boat" do
|
100
100
|
Pie4::STATUSES.should == ["on", "off"]
|
@@ -106,11 +106,63 @@ describe AffairsOfState do
|
|
106
106
|
self.table_name = "pies"
|
107
107
|
|
108
108
|
affairs_of_state :active, :inactive, :scopes => false
|
109
|
-
end
|
109
|
+
end
|
110
110
|
|
111
111
|
it "should work too if that's what floats your boat" do
|
112
112
|
Pie5.should_not respond_to(:active)
|
113
113
|
end
|
114
114
|
end
|
115
115
|
|
116
|
+
describe "with a conditional proc" do
|
117
|
+
class Pie6 < ActiveRecord::Base
|
118
|
+
self.table_name = "pies"
|
119
|
+
|
120
|
+
affairs_of_state :active, :inactive, :if => lambda{|p| p.is_going_to_validate }
|
121
|
+
|
122
|
+
attr_accessor :is_going_to_validate
|
123
|
+
end
|
124
|
+
|
125
|
+
it "should enforce the validation if the :if param is true" do
|
126
|
+
p = Pie6.new
|
127
|
+
p.is_going_to_validate = true
|
128
|
+
p.status = "pie"
|
129
|
+
p.should_not be_valid
|
130
|
+
end
|
131
|
+
|
132
|
+
it "should not enforce the validation if the :if param evaluates to false" do
|
133
|
+
p = Pie6.new
|
134
|
+
p.is_going_to_validate = false
|
135
|
+
p.status = "pie"
|
136
|
+
p.should be_valid
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
describe "with a conditional method name" do
|
141
|
+
class Pie7 < ActiveRecord::Base
|
142
|
+
self.table_name = "pies"
|
143
|
+
|
144
|
+
affairs_of_state :active, :inactive, :if => :validation_method?
|
145
|
+
|
146
|
+
attr_accessor :is_going_to_validate
|
147
|
+
|
148
|
+
def validation_method?
|
149
|
+
self.is_going_to_validate
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
it "should enforce the validation if the :if param is true" do
|
154
|
+
p = Pie7.new
|
155
|
+
p.is_going_to_validate = true
|
156
|
+
p.status = "pie"
|
157
|
+
p.should_not be_valid
|
158
|
+
end
|
159
|
+
|
160
|
+
it "should not enforce the validation if the :if param evaluates to false" do
|
161
|
+
p = Pie7.new
|
162
|
+
p.is_going_to_validate = false
|
163
|
+
p.status = "pie"
|
164
|
+
p.should be_valid
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
116
168
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: affairs_of_state
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-12-17 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
16
|
-
requirement: &
|
16
|
+
requirement: &81758600 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '3.0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *81758600
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &81758390 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *81758390
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: sqlite3
|
38
|
-
requirement: &
|
38
|
+
requirement: &81758160 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *81758160
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: pry
|
49
|
-
requirement: &
|
49
|
+
requirement: &81757950 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,7 +54,7 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *81757950
|
58
58
|
description: Add a simple state to a gem, without all the hassle of a complex state
|
59
59
|
machine.
|
60
60
|
email:
|
@@ -65,6 +65,7 @@ extra_rdoc_files: []
|
|
65
65
|
files:
|
66
66
|
- .gitignore
|
67
67
|
- .rspec
|
68
|
+
- .travis.yml
|
68
69
|
- Gemfile
|
69
70
|
- LICENSE
|
70
71
|
- README.md
|