affairs_of_state 0.1.4 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +18 -3
- data/README.md +12 -16
- data/affairs_of_state.gemspec +3 -2
- data/gemfiles/activerecord40.gemfile +5 -0
- data/gemfiles/activerecord41.gemfile +5 -0
- data/gemfiles/activerecord42.gemfile +5 -0
- data/gemfiles/activerecord50.gemfile +5 -0
- data/lib/affairs_of_state.rb +12 -14
- data/lib/affairs_of_state/version.rb +1 -1
- data/spec/affairs_of_state_spec.rb +1 -1
- data/spec/spec_helper.rb +3 -3
- metadata +29 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 71da77675fa54352c44a00554fdde62d56b50881
|
4
|
+
data.tar.gz: d2a0215074c06bc571b51d8b0e918696ec1b027f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 777b4d67fde6f03c6ce0320d23ec7879d9dce2f9da70b026d1d70d1c78f8cb15c4c90393e381c46b1fc0935cde20b3f5a6122794fd93b2b81b9a9d08d5bb6b22
|
7
|
+
data.tar.gz: edd41d5cace467b7d629ee7d1596be76491272f6e2f66208a8bb9bb05dcc1ad05f4d10ec4920071a762123b415d834ad2ca9f5715dfe45559ea0297755053083
|
data/.travis.yml
CHANGED
@@ -1,6 +1,21 @@
|
|
1
1
|
language: ruby
|
2
|
+
script: bundle exec rspec
|
3
|
+
sudo: false
|
2
4
|
rvm:
|
3
|
-
- "1.9.3"
|
4
|
-
- "2.0.0"
|
5
5
|
- "2.1.0"
|
6
|
-
|
6
|
+
- "2.2.2"
|
7
|
+
- "2.3.1"
|
8
|
+
|
9
|
+
gemfile:
|
10
|
+
- Gemfile
|
11
|
+
- gemfiles/activerecord40.gemfile
|
12
|
+
- gemfiles/activerecord41.gemfile
|
13
|
+
- gemfiles/activerecord42.gemfile
|
14
|
+
- gemfiles/activerecord50.gemfile
|
15
|
+
|
16
|
+
matrix:
|
17
|
+
exclude:
|
18
|
+
- rvm: "2.1.0"
|
19
|
+
gemfile: Gemfile
|
20
|
+
- rvm: "2.1.0"
|
21
|
+
gemfile: gemfiles/activerecord50.gemfile
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
[![Build Status](https://travis-ci.org/kmcphillips/affairs_of_state.png?branch=master)](https://travis-ci.org/kmcphillips/affairs_of_state)
|
4
4
|
|
5
|
-
You have an Active Record model. It nees to have multiple states, but not complex rules. This gem gives you
|
5
|
+
You have an Active Record model. It nees to have multiple states, boolean convenience methods, simple validation, but not complex rules. This gem gives you this in a single line class method.
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
@@ -12,13 +12,7 @@ Add this line to your application's Gemfile:
|
|
12
12
|
gem 'affairs_of_state'
|
13
13
|
```
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
```ruby
|
18
|
-
$ bundle
|
19
|
-
```
|
20
|
-
|
21
|
-
Or install it yourself as:
|
15
|
+
Or install it with:
|
22
16
|
|
23
17
|
```ruby
|
24
18
|
$ gem install affairs_of_state
|
@@ -29,7 +23,7 @@ $ gem install affairs_of_state
|
|
29
23
|
The gem assumes you have a string column named `status` on your model:
|
30
24
|
|
31
25
|
```ruby
|
32
|
-
add_column :model_name, :status, :
|
26
|
+
add_column :model_name, :status, default: "active"
|
33
27
|
```
|
34
28
|
|
35
29
|
Then you just list your states in the model:
|
@@ -41,13 +35,13 @@ affairs_of_state :active, :inactive
|
|
41
35
|
If you'd like to use another column, lets say `state`, pass it in as a configuration option:
|
42
36
|
|
43
37
|
```ruby
|
44
|
-
affairs_of_state :active, :inactive, :
|
38
|
+
affairs_of_state :active, :inactive, column: :state
|
45
39
|
```
|
46
40
|
|
47
41
|
You can also turn off validation:
|
48
42
|
|
49
43
|
```ruby
|
50
|
-
affairs_of_state :active, :inactive, :
|
44
|
+
affairs_of_state :active, :inactive, allow_blank: true
|
51
45
|
```
|
52
46
|
|
53
47
|
Or give it a long list of statuses:
|
@@ -59,9 +53,11 @@ affairs_of_state :ordered, :cancelled, :shipped, :lost, :in_transit
|
|
59
53
|
You can also pass a proc or a method name symbol to the :if option to bypass validation:
|
60
54
|
|
61
55
|
```ruby
|
62
|
-
affairs_of_state :active, :inactive, :
|
63
|
-
|
64
|
-
|
56
|
+
affairs_of_state :active, :inactive, if: ->(object) { only_validate_if_this_is_true(object) }
|
57
|
+
```
|
58
|
+
or
|
59
|
+
```ruby
|
60
|
+
affairs_of_state :active, :inactive, if: :only_validate_if_this_method_returns_true
|
65
61
|
```
|
66
62
|
|
67
63
|
|
@@ -114,6 +110,6 @@ rspec
|
|
114
110
|
|
115
111
|
## The usual
|
116
112
|
|
117
|
-
|
113
|
+
By Kevin McPhillips (github@kevinmcphillips.ca)
|
118
114
|
|
119
|
-
|
115
|
+
[MIT License](http://opensource.org/licenses/MIT)
|
data/affairs_of_state.gemspec
CHANGED
@@ -16,9 +16,10 @@ Gem::Specification.new do |gem|
|
|
16
16
|
gem.require_paths = ["lib"]
|
17
17
|
gem.version = AffairsOfState::VERSION
|
18
18
|
|
19
|
-
gem.add_dependency "activerecord", "
|
19
|
+
gem.add_dependency "activerecord", ">= 4.0"
|
20
|
+
gem.add_dependency "activesupport", ">= 4.0"
|
20
21
|
|
21
|
-
gem.add_development_dependency "rspec"
|
22
|
+
gem.add_development_dependency "rspec"
|
22
23
|
gem.add_development_dependency "sqlite3"
|
23
24
|
gem.add_development_dependency "pry"
|
24
25
|
|
data/lib/affairs_of_state.rb
CHANGED
@@ -1,29 +1,27 @@
|
|
1
1
|
require "affairs_of_state/version"
|
2
2
|
|
3
3
|
module AffairsOfState
|
4
|
+
extend ActiveSupport::Concern
|
4
5
|
|
5
|
-
|
6
|
-
base.extend ClassMethods
|
7
|
-
end
|
8
|
-
|
9
|
-
module ClassMethods
|
6
|
+
class_methods do
|
10
7
|
def affairs_of_state(*args)
|
11
|
-
@_status_options =
|
8
|
+
@_status_options = { column: :status, allow_blank: false, scopes: true, if: nil }.merge(args.extract_options!)
|
12
9
|
@_statuses = args.flatten.map(&:to_s)
|
13
10
|
|
14
11
|
const_set("STATUSES", @_statuses)
|
15
12
|
|
16
|
-
validates(@_status_options[:column], :
|
13
|
+
validates(@_status_options[:column], inclusion: { in: @_statuses, allow_blank: @_status_options[:allow_blank] }, if: @_status_options[:if])
|
17
14
|
|
18
15
|
if @_status_options[:scopes]
|
19
16
|
@_statuses.each do |status|
|
20
|
-
raise "Affairs of State: '#{ status }' is not a valid status" unless valid_status?(status)
|
17
|
+
raise ArgumentError.new("Affairs of State: '#{ status }' is not a valid status") unless valid_status?(status)
|
21
18
|
self.scope status.to_sym, -> { where(@_status_options[:column] => status.to_s) }
|
22
19
|
end
|
23
20
|
end
|
24
21
|
|
25
22
|
include InstanceMethods
|
26
23
|
extend SingletonMethods
|
24
|
+
|
27
25
|
true
|
28
26
|
end
|
29
27
|
|
@@ -36,20 +34,20 @@ module AffairsOfState
|
|
36
34
|
|
37
35
|
module InstanceMethods
|
38
36
|
def method_missing(method, *args)
|
39
|
-
if self.class::STATUSES.map{|s| "#{s}?".to_sym }.include?(method)
|
37
|
+
if self.class::STATUSES.map{ |s| "#{ s }?".to_sym }.include?(method)
|
40
38
|
self.class.send(:define_method, method) do
|
41
39
|
self.status == method.to_s.gsub(/\?$/, "")
|
42
40
|
end
|
43
41
|
|
44
|
-
send
|
42
|
+
send(method)
|
45
43
|
|
46
|
-
elsif self.class::STATUSES.map{|s| "#{s}!".to_sym }.include?(method)
|
44
|
+
elsif self.class::STATUSES.map{ |s| "#{ s }!".to_sym }.include?(method)
|
47
45
|
self.class.send(:define_method, method) do
|
48
|
-
self.send("#{self.class.instance_variable_get('@_status_options')[:column]}=", method.to_s.gsub(/\!$/, ""))
|
46
|
+
self.send("#{ self.class.instance_variable_get('@_status_options')[:column] }=", method.to_s.gsub(/\!$/, ""))
|
49
47
|
self.save
|
50
48
|
end
|
51
49
|
|
52
|
-
send
|
50
|
+
send(method)
|
53
51
|
else
|
54
52
|
super
|
55
53
|
end
|
@@ -58,7 +56,7 @@ module AffairsOfState
|
|
58
56
|
|
59
57
|
module SingletonMethods
|
60
58
|
def statuses_for_select
|
61
|
-
@_statuses.map{|s| [s.humanize, s]}
|
59
|
+
@_statuses.map{ |s| [s.humanize, s] }
|
62
60
|
end
|
63
61
|
end
|
64
62
|
|
@@ -163,7 +163,7 @@ describe AffairsOfState do
|
|
163
163
|
|
164
164
|
describe "invalid status name" do
|
165
165
|
it "should raise a good warning" do
|
166
|
-
expect(->{ class Pie8 < ActiveRecord::Base ; affairs_of_state :new ; end }).to raise_error("Affairs of State: 'new' is not a valid status")
|
166
|
+
expect(->{ class Pie8 < ActiveRecord::Base ; affairs_of_state :new ; end }).to raise_error(ArgumentError, "Affairs of State: 'new' is not a valid status")
|
167
167
|
end
|
168
168
|
end
|
169
169
|
|
data/spec/spec_helper.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'active_record'
|
2
|
+
require 'active_support/all'
|
2
3
|
require 'affairs_of_state'
|
3
4
|
require 'pry'
|
4
5
|
|
@@ -6,13 +7,12 @@ RSpec.configure do |config|
|
|
6
7
|
config.run_all_when_everything_filtered = true
|
7
8
|
end
|
8
9
|
|
9
|
-
|
10
10
|
## Create an AR model to test with
|
11
11
|
I18n.enforce_available_locales = false
|
12
12
|
|
13
13
|
ActiveRecord::Base.establish_connection(
|
14
|
-
:
|
15
|
-
:
|
14
|
+
adapter: "sqlite3",
|
15
|
+
database: "#{File.expand_path(File.join(File.dirname(__FILE__), '..'))}/spec/db/test.sqlite3"
|
16
16
|
)
|
17
17
|
|
18
18
|
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS 'pies'")
|
metadata
CHANGED
@@ -1,43 +1,57 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: affairs_of_state
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kevin McPhillips
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-07-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '4.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '4.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '4.0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: rspec
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
30
44
|
requirements:
|
31
|
-
- - "
|
45
|
+
- - ">="
|
32
46
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
47
|
+
version: '0'
|
34
48
|
type: :development
|
35
49
|
prerelease: false
|
36
50
|
version_requirements: !ruby/object:Gem::Requirement
|
37
51
|
requirements:
|
38
|
-
- - "
|
52
|
+
- - ">="
|
39
53
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
54
|
+
version: '0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: sqlite3
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -82,6 +96,10 @@ files:
|
|
82
96
|
- README.md
|
83
97
|
- Rakefile
|
84
98
|
- affairs_of_state.gemspec
|
99
|
+
- gemfiles/activerecord40.gemfile
|
100
|
+
- gemfiles/activerecord41.gemfile
|
101
|
+
- gemfiles/activerecord42.gemfile
|
102
|
+
- gemfiles/activerecord50.gemfile
|
85
103
|
- lib/affairs_of_state.rb
|
86
104
|
- lib/affairs_of_state/version.rb
|
87
105
|
- spec/affairs_of_state_spec.rb
|
@@ -107,7 +125,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
107
125
|
version: '0'
|
108
126
|
requirements: []
|
109
127
|
rubyforge_project:
|
110
|
-
rubygems_version: 2.
|
128
|
+
rubygems_version: 2.4.5.1
|
111
129
|
signing_key:
|
112
130
|
specification_version: 4
|
113
131
|
summary: You have an Active Record model. It nees to have multiple states, but not
|