enum_aasm 0.0.1
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.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.rspec +5 -0
- data/.rvmrc +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +55 -0
- data/Rakefile +4 -0
- data/enum_aasm.gemspec +28 -0
- data/lib/enum_aasm.rb +64 -0
- data/lib/enum_aasm/version.rb +3 -0
- data/spec/enum_aasm_spec.rb +101 -0
- data/spec/spec_helper.rb +16 -0
- metadata +142 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d64e7d22497c11c5427ee98ff34102743864f1cd
|
4
|
+
data.tar.gz: fde827f85411ce0e44f16aab64d4f1ab2d0b65ff
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bbc68ee3f4bd005d77c37130e73d2931ccb0625acf3883eab17656cfec55909030acfa76c8c8092a23c762813f740da1140d4af9e2a57396867019a548306afc
|
7
|
+
data.tar.gz: 61d09fb0d2c9d0eb0709ce639eff70e79a3e61d9813d2bfdf634882e137012b698d18ec7001a456d96c8329cf4925f5f6bfcec52c7acb556758dc8ce18b3f495
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use 2.0.0@enum_state_machine --create
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Arthur Shagall
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# EnumAasm
|
2
|
+
|
3
|
+
An extension to the [AASM](https://github.com/aasm/aasm) finite state machine that uses
|
4
|
+
[PowerEnum](https://github.com/albertosaurus/power_enum_2) enumerated attributes
|
5
|
+
for state transitions.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'enum_aasm'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install enum_aasm
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Basically the same usage as the original AASM. The only difference is that the `:column` option is mandatory.
|
24
|
+
Use this option to specify the name of the enumerated attribute. Note that scopes are not defined
|
25
|
+
by the state machine as PowerEnum already handles this functionality.
|
26
|
+
|
27
|
+
### Example
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
require 'enum_aasm'
|
31
|
+
|
32
|
+
class Fruit < ActiveRecord::Base
|
33
|
+
include AASM
|
34
|
+
|
35
|
+
has_enumerated :fruit_color
|
36
|
+
|
37
|
+
aasm :column => :fruit_color do
|
38
|
+
state :green, :initial => true
|
39
|
+
end
|
40
|
+
end
|
41
|
+
```
|
42
|
+
|
43
|
+
## License
|
44
|
+
|
45
|
+
Distributed under the MIT license. See LICENSE.txt for details.
|
46
|
+
|
47
|
+
## Contributing
|
48
|
+
|
49
|
+
1. Fork it
|
50
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
51
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
52
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
53
|
+
5. Create new Pull Request
|
54
|
+
|
55
|
+
Copyright 2014 Arthur Shagall
|
data/Rakefile
ADDED
data/enum_aasm.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'enum_aasm/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "enum_aasm"
|
8
|
+
spec.version = EnumAasm::VERSION
|
9
|
+
spec.authors = ["Arthur Shagall"]
|
10
|
+
spec.email = ["arthur.shagall@gmail.com"]
|
11
|
+
spec.description = %q{This gem patches AASM to use PowerEnum enums for states.}
|
12
|
+
spec.summary = %q{Use PowerEnum enums for AASM states.}
|
13
|
+
spec.homepage = "https://github.com/albertosaurus/enum_aasm"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency 'rspec'
|
24
|
+
spec.add_development_dependency 'simplecov'
|
25
|
+
|
26
|
+
spec.add_dependency 'aasm'
|
27
|
+
spec.add_dependency 'power_enum', '~> 2.0'
|
28
|
+
end
|
data/lib/enum_aasm.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
#
|
2
|
+
# Copyright 2014 Arthur Shagall
|
3
|
+
#
|
4
|
+
|
5
|
+
require "enum_aasm/version"
|
6
|
+
require 'power_enum'
|
7
|
+
require 'aasm'
|
8
|
+
require 'aasm/base'
|
9
|
+
require 'aasm/persistence/active_record_persistence'
|
10
|
+
require 'aasm/persistence/base'
|
11
|
+
|
12
|
+
AASM::Persistence::ActiveRecordPersistence::InstanceMethods.module_eval do
|
13
|
+
|
14
|
+
# Patched to use the enumerated attribute
|
15
|
+
def aasm_write_state(state)
|
16
|
+
attr_name = self.class.aasm_column
|
17
|
+
old_value = self.__send__(attr_name)
|
18
|
+
self.__send__("#{attr_name}=", state)
|
19
|
+
|
20
|
+
success = if AASM::StateMachine[self.class].config.skip_validation_on_save
|
21
|
+
self.update_attribute(attr_name, state)
|
22
|
+
else
|
23
|
+
self.save
|
24
|
+
end
|
25
|
+
if success
|
26
|
+
true
|
27
|
+
else
|
28
|
+
self.__send__("#{attr_name}=", old_value)
|
29
|
+
false
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# Patched to use the enumerated attribute
|
34
|
+
def aasm_write_state_without_persistence(state)
|
35
|
+
self.__send__("#{self.class.aasm_column}=", state)
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
AASM::Base.module_eval do
|
41
|
+
|
42
|
+
# Scopes need to be disabled. PowerEnum already provides this functionality
|
43
|
+
def state(name, *args)
|
44
|
+
state_without_scope(name, *args)
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
AASM::Persistence::ActiveRecordPersistence.module_eval do
|
50
|
+
|
51
|
+
def self.included(base)
|
52
|
+
base.send(:include, AASM::Persistence::Base)
|
53
|
+
base.send(:include, AASM::Persistence::ActiveRecordPersistence::InstanceMethods)
|
54
|
+
|
55
|
+
base.before_validation(:aasm_ensure_initial_state, :on => :create)
|
56
|
+
|
57
|
+
# ensure initial aasm state even when validations are skipped
|
58
|
+
base.before_create(:aasm_ensure_initial_state)
|
59
|
+
|
60
|
+
# ensure state is in the list of states
|
61
|
+
base.validate :aasm_validate_states
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
#
|
2
|
+
# Copyright 2014 Arthur Shagall
|
3
|
+
#
|
4
|
+
require 'spec_helper'
|
5
|
+
|
6
|
+
class SpecModel
|
7
|
+
# Class methods
|
8
|
+
class << self
|
9
|
+
def aasm_column
|
10
|
+
:status
|
11
|
+
end
|
12
|
+
|
13
|
+
def transaction(args)
|
14
|
+
yield
|
15
|
+
end
|
16
|
+
|
17
|
+
[:before_validation, :before_create, :validate].each do |method|
|
18
|
+
module_eval( <<-end_eval, __FILE__, __LINE__ )
|
19
|
+
def #{method}(*args)
|
20
|
+
true
|
21
|
+
end
|
22
|
+
end_eval
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
include AASM
|
28
|
+
include AASM::Persistence::ActiveRecordPersistence
|
29
|
+
|
30
|
+
attr_accessor :status
|
31
|
+
|
32
|
+
def initialize
|
33
|
+
self.status = :foo
|
34
|
+
end
|
35
|
+
|
36
|
+
aasm :column => :status do
|
37
|
+
|
38
|
+
state :foo, :initial => true
|
39
|
+
state :bar
|
40
|
+
state :baz
|
41
|
+
|
42
|
+
event :start do
|
43
|
+
transitions :from => :foo, :to => :bar, :guard => :can_start?
|
44
|
+
transitions :from => :foo, :to => :baz
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
def can_start?
|
50
|
+
true
|
51
|
+
end
|
52
|
+
|
53
|
+
def save
|
54
|
+
true
|
55
|
+
end
|
56
|
+
|
57
|
+
def new_record?
|
58
|
+
true
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe "EnumAasm" do
|
63
|
+
|
64
|
+
let(:model) { SpecModel.new }
|
65
|
+
|
66
|
+
it 'basic transition' do
|
67
|
+
model.start!.should be_true
|
68
|
+
|
69
|
+
model.status.should eq(:bar)
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'transition with guard' do
|
73
|
+
model.should_receive(:can_start?).and_return(false)
|
74
|
+
|
75
|
+
model.start!.should be_true
|
76
|
+
|
77
|
+
model.status.should eq(:baz)
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'if save fails' do
|
81
|
+
model.should_receive(:save).and_return(false)
|
82
|
+
|
83
|
+
model.start!.should be_false
|
84
|
+
|
85
|
+
model.status.should eq(:foo)
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'without saving' do
|
89
|
+
model.start.should be_true
|
90
|
+
|
91
|
+
model.status.should eq(:bar)
|
92
|
+
end
|
93
|
+
|
94
|
+
it 'invalid transition' do
|
95
|
+
model.status = :baz
|
96
|
+
|
97
|
+
expect {
|
98
|
+
model.start.should be_false
|
99
|
+
}.to raise_error(AASM::InvalidTransition)
|
100
|
+
end
|
101
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#
|
2
|
+
# Copyright 2014 Arthur Shagall
|
3
|
+
#
|
4
|
+
unless ENV['TRAVIS']
|
5
|
+
require 'simplecov'
|
6
|
+
SimpleCov.start
|
7
|
+
end
|
8
|
+
|
9
|
+
require 'bundler/setup'
|
10
|
+
|
11
|
+
require 'enum_aasm'
|
12
|
+
|
13
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
14
|
+
RSpec.configure do |config|
|
15
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
16
|
+
end
|
metadata
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: enum_aasm
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Arthur Shagall
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-03-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: simplecov
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: aasm
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: power_enum
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ~>
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '2.0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ~>
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '2.0'
|
97
|
+
description: This gem patches AASM to use PowerEnum enums for states.
|
98
|
+
email:
|
99
|
+
- arthur.shagall@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- .gitignore
|
105
|
+
- .rspec
|
106
|
+
- .rvmrc
|
107
|
+
- Gemfile
|
108
|
+
- LICENSE.txt
|
109
|
+
- README.md
|
110
|
+
- Rakefile
|
111
|
+
- enum_aasm.gemspec
|
112
|
+
- lib/enum_aasm.rb
|
113
|
+
- lib/enum_aasm/version.rb
|
114
|
+
- spec/enum_aasm_spec.rb
|
115
|
+
- spec/spec_helper.rb
|
116
|
+
homepage: https://github.com/albertosaurus/enum_aasm
|
117
|
+
licenses:
|
118
|
+
- MIT
|
119
|
+
metadata: {}
|
120
|
+
post_install_message:
|
121
|
+
rdoc_options: []
|
122
|
+
require_paths:
|
123
|
+
- lib
|
124
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - '>='
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
129
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - '>='
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
requirements: []
|
135
|
+
rubyforge_project:
|
136
|
+
rubygems_version: 2.1.11
|
137
|
+
signing_key:
|
138
|
+
specification_version: 4
|
139
|
+
summary: Use PowerEnum enums for AASM states.
|
140
|
+
test_files:
|
141
|
+
- spec/enum_aasm_spec.rb
|
142
|
+
- spec/spec_helper.rb
|