affairs_of_state 0.1.1 → 0.1.2
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/README.md +54 -21
- data/lib/affairs_of_state/version.rb +1 -1
- data/lib/affairs_of_state.rb +1 -1
- metadata +13 -35
data/README.md
CHANGED
|
@@ -1,70 +1,100 @@
|
|
|
1
1
|
# Affairs of State
|
|
2
2
|
|
|
3
|
+
[](https://travis-ci.org/kmcphillips/affairs_of_state)
|
|
4
|
+
|
|
3
5
|
You have an Active Record model. It nees to have multiple states, but not complex rules. This gem gives you validation, easy check and change methods, and a single configuration line.
|
|
4
6
|
|
|
5
7
|
## Installation
|
|
6
8
|
|
|
7
9
|
Add this line to your application's Gemfile:
|
|
8
10
|
|
|
9
|
-
|
|
11
|
+
```ruby
|
|
12
|
+
gem 'affairs_of_state'
|
|
13
|
+
```
|
|
10
14
|
|
|
11
15
|
And then execute:
|
|
12
16
|
|
|
13
|
-
|
|
17
|
+
```ruby
|
|
18
|
+
$ bundle
|
|
19
|
+
```
|
|
14
20
|
|
|
15
21
|
Or install it yourself as:
|
|
16
22
|
|
|
17
|
-
|
|
23
|
+
```ruby
|
|
24
|
+
$ gem install affairs_of_state
|
|
25
|
+
```
|
|
18
26
|
|
|
19
27
|
## Usage
|
|
20
28
|
|
|
21
|
-
The gem assumes you have a string column named
|
|
29
|
+
The gem assumes you have a string column named `status` on your model:
|
|
22
30
|
|
|
23
|
-
|
|
31
|
+
```ruby
|
|
32
|
+
add_column :model_name, :status, :default => "active"
|
|
33
|
+
```
|
|
24
34
|
|
|
25
35
|
Then you just list your states in the model:
|
|
26
36
|
|
|
27
|
-
|
|
37
|
+
```ruby
|
|
38
|
+
affairs_of_state :active, :inactive
|
|
39
|
+
```
|
|
28
40
|
|
|
29
|
-
If you'd like to use another column, lets say
|
|
41
|
+
If you'd like to use another column, lets say `state`, pass it in as a configuration option:
|
|
30
42
|
|
|
31
|
-
|
|
43
|
+
```ruby
|
|
44
|
+
affairs_of_state :active, :inactive, :column => :state
|
|
45
|
+
```
|
|
32
46
|
|
|
33
47
|
You can also turn off validation:
|
|
34
48
|
|
|
35
|
-
|
|
49
|
+
```ruby
|
|
50
|
+
affairs_of_state :active, :inactive, :allow_blank => true
|
|
51
|
+
```
|
|
36
52
|
|
|
37
53
|
Or give it a long list of statuses:
|
|
38
54
|
|
|
39
|
-
|
|
55
|
+
```ruby
|
|
56
|
+
affairs_of_state :ordered, :cancelled, :shipped, :lost, :in_transit
|
|
57
|
+
```
|
|
40
58
|
|
|
41
59
|
You can also pass a proc or a method name symbol to the :if option to bypass validation:
|
|
42
60
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
61
|
+
```ruby
|
|
62
|
+
affairs_of_state :active, :inactive, :if => lambda{|object| only_validate_if_this_is_true(object) }
|
|
63
|
+
# or
|
|
64
|
+
affairs_of_state :active, :inactive, :if => :only_validate_if_this_method_returns_true
|
|
65
|
+
```
|
|
46
66
|
|
|
47
67
|
|
|
48
68
|
## Methods
|
|
49
69
|
|
|
50
70
|
The gem provides methods for checking and setting your status. The question mark method returns a boolean, and the bang method changes to that status. Lets assume you have "active" and "cancelled" as defined status:
|
|
51
71
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
72
|
+
```ruby
|
|
73
|
+
widget = Widget.first
|
|
74
|
+
widget.cancelled! if widget.active?
|
|
75
|
+
```
|
|
55
76
|
|
|
56
77
|
You can also access all your statuses on the model like so:
|
|
57
78
|
|
|
58
|
-
|
|
79
|
+
```ruby
|
|
80
|
+
Widget::STATUSES # -> ["active", "cancelled"]
|
|
81
|
+
```
|
|
59
82
|
|
|
60
83
|
It also provides scopes automagically:
|
|
61
84
|
|
|
62
|
-
|
|
85
|
+
```ruby
|
|
86
|
+
Widget.active
|
|
87
|
+
Widget.cancelled
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
For select inputs in forms there is a convenience method that returns all states in the format expected by `options_for_select`
|
|
63
91
|
|
|
64
|
-
|
|
92
|
+
```ruby
|
|
93
|
+
<%= f.select :status, options_for_select(Widget.statuses_for_select) %>
|
|
94
|
+
```
|
|
65
95
|
|
|
66
96
|
|
|
67
|
-
## "But I want callbacks and validations
|
|
97
|
+
## "But I want callbacks and validations etc."
|
|
68
98
|
|
|
69
99
|
Then this gem isn't for you. Consider:
|
|
70
100
|
|
|
@@ -77,10 +107,13 @@ https://github.com/pluginaweek/state_machine
|
|
|
77
107
|
|
|
78
108
|
Just run rspec:
|
|
79
109
|
|
|
80
|
-
|
|
110
|
+
```
|
|
111
|
+
rspec
|
|
112
|
+
```
|
|
81
113
|
|
|
82
114
|
|
|
83
115
|
## The usual
|
|
84
116
|
|
|
85
117
|
Author: Kevin McPhillips - github@kevinmcphillips.ca
|
|
86
118
|
|
|
119
|
+
License: [MIT](http://opensource.org/licenses/MIT)
|
data/lib/affairs_of_state.rb
CHANGED
|
@@ -17,7 +17,7 @@ module AffairsOfState
|
|
|
17
17
|
|
|
18
18
|
if @_status_options[:scopes]
|
|
19
19
|
@_statuses.each do |status|
|
|
20
|
-
scope status.to_sym, -> { where(@_status_options[:column] => status.to_s) }
|
|
20
|
+
self.scope status.to_sym, -> { where(@_status_options[:column] => status.to_s) }
|
|
21
21
|
end
|
|
22
22
|
end
|
|
23
23
|
|
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.1.
|
|
4
|
+
version: 0.1.2
|
|
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:
|
|
12
|
+
date: 2014-06-05 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
|
14
14
|
- !ruby/object:Gem::Dependency
|
|
15
15
|
name: activerecord
|
|
16
|
-
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirement: &81631330 !ruby/object:Gem::Requirement
|
|
17
17
|
none: false
|
|
18
18
|
requirements:
|
|
19
19
|
- - ! '>'
|
|
@@ -21,15 +21,10 @@ dependencies:
|
|
|
21
21
|
version: '3.0'
|
|
22
22
|
type: :runtime
|
|
23
23
|
prerelease: false
|
|
24
|
-
version_requirements:
|
|
25
|
-
none: false
|
|
26
|
-
requirements:
|
|
27
|
-
- - ! '>'
|
|
28
|
-
- !ruby/object:Gem::Version
|
|
29
|
-
version: '3.0'
|
|
24
|
+
version_requirements: *81631330
|
|
30
25
|
- !ruby/object:Gem::Dependency
|
|
31
26
|
name: rspec
|
|
32
|
-
requirement: !ruby/object:Gem::Requirement
|
|
27
|
+
requirement: &81631020 !ruby/object:Gem::Requirement
|
|
33
28
|
none: false
|
|
34
29
|
requirements:
|
|
35
30
|
- - ! '>='
|
|
@@ -37,15 +32,10 @@ dependencies:
|
|
|
37
32
|
version: '0'
|
|
38
33
|
type: :development
|
|
39
34
|
prerelease: false
|
|
40
|
-
version_requirements:
|
|
41
|
-
none: false
|
|
42
|
-
requirements:
|
|
43
|
-
- - ! '>='
|
|
44
|
-
- !ruby/object:Gem::Version
|
|
45
|
-
version: '0'
|
|
35
|
+
version_requirements: *81631020
|
|
46
36
|
- !ruby/object:Gem::Dependency
|
|
47
37
|
name: sqlite3
|
|
48
|
-
requirement: !ruby/object:Gem::Requirement
|
|
38
|
+
requirement: &81630660 !ruby/object:Gem::Requirement
|
|
49
39
|
none: false
|
|
50
40
|
requirements:
|
|
51
41
|
- - ! '>='
|
|
@@ -53,15 +43,10 @@ dependencies:
|
|
|
53
43
|
version: '0'
|
|
54
44
|
type: :development
|
|
55
45
|
prerelease: false
|
|
56
|
-
version_requirements:
|
|
57
|
-
none: false
|
|
58
|
-
requirements:
|
|
59
|
-
- - ! '>='
|
|
60
|
-
- !ruby/object:Gem::Version
|
|
61
|
-
version: '0'
|
|
46
|
+
version_requirements: *81630660
|
|
62
47
|
- !ruby/object:Gem::Dependency
|
|
63
48
|
name: pry
|
|
64
|
-
requirement: !ruby/object:Gem::Requirement
|
|
49
|
+
requirement: &81630350 !ruby/object:Gem::Requirement
|
|
65
50
|
none: false
|
|
66
51
|
requirements:
|
|
67
52
|
- - ! '>='
|
|
@@ -69,12 +54,7 @@ dependencies:
|
|
|
69
54
|
version: '0'
|
|
70
55
|
type: :development
|
|
71
56
|
prerelease: false
|
|
72
|
-
version_requirements:
|
|
73
|
-
none: false
|
|
74
|
-
requirements:
|
|
75
|
-
- - ! '>='
|
|
76
|
-
- !ruby/object:Gem::Version
|
|
77
|
-
version: '0'
|
|
57
|
+
version_requirements: *81630350
|
|
78
58
|
description: Add a simple state to a gem, without all the hassle of a complex state
|
|
79
59
|
machine.
|
|
80
60
|
email:
|
|
@@ -117,13 +97,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
117
97
|
version: '0'
|
|
118
98
|
requirements: []
|
|
119
99
|
rubyforge_project:
|
|
120
|
-
rubygems_version: 1.8.
|
|
100
|
+
rubygems_version: 1.8.15
|
|
121
101
|
signing_key:
|
|
122
102
|
specification_version: 3
|
|
123
103
|
summary: You have an Active Record model. It nees to have multiple states, but not
|
|
124
104
|
complex rules. This gem gives you validation, easy check and change methods, and
|
|
125
105
|
a single configuration line.
|
|
126
|
-
test_files:
|
|
127
|
-
|
|
128
|
-
- spec/db/.gitkeep
|
|
129
|
-
- spec/spec_helper.rb
|
|
106
|
+
test_files: []
|
|
107
|
+
has_rdoc:
|