aasm 3.1.1 → 3.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a003bac78875452c53f609ff9a3da1bffbe93626
4
- data.tar.gz: 4f32daafb5eaa4a05393204fbe11b6f7e2d361f8
3
+ metadata.gz: 88085e60d048a49a0b7d88c73810dbed9e028926
4
+ data.tar.gz: 86b196aee05e522b0235cbf0d8ed6b2f66cde027
5
5
  SHA512:
6
- metadata.gz: 24d9cf982132e85646e70d27572cd0e6c0e504e2ee95e5cb87226332edcfb24c9cd5b130188f31e72d9fafcd728acae36aa1d8dada302a721650f136a1a6cdaa
7
- data.tar.gz: 2a86daa641a419ab85fc1ee95d8b9cc9a47e447eec20929b50af0d543ed9dd3b0493c991c4af49a044ed6f8a4f78fed8e691211015ee1ae10574f063cdfb40d0
6
+ metadata.gz: b339bead8353bed92cfe761bf0fedab3aa48ee24edb030e1f992ab0de1342ec5d8228f185ed81cd3f45519a778db110edc41baa885dc549624fe7408ea3205ac
7
+ data.tar.gz: 10433114be6eec38b4eb0ebc191b7f093cb25d5f45215f172354cbfbfc5cbcebc5b8e0ead5d8cc42a19731e40ee5e65ea29ac76f5a501405fac427d3ea836e67
@@ -5,7 +5,7 @@ rvm:
5
5
  - 1.9.2
6
6
  - 1.9.3
7
7
  - 2.0.0
8
- - 2.1.0
8
+ - 2.1
9
9
  # - jruby-18mode # JRuby in 1.8 mode
10
10
  - jruby-19mode # JRuby in 1.9 mode
11
11
  - rbx-2.2.1
@@ -22,9 +22,15 @@ matrix:
22
22
  - rvm: rbx-2.2.1
23
23
  - rvm: jruby-19mode
24
24
  exclude:
25
- - { rvm: 1.8.7, gemfile: gemfiles/rails_4.0.gemfile }
26
- - { rvm: 1.8.7, gemfile: gemfiles/rails_4.1.gemfile }
27
- - { rvm: 1.9.2, gemfile: gemfiles/rails_4.0.gemfile }
28
- - { rvm: 1.9.2, gemfile: gemfiles/rails_4.1.gemfile }
29
- - { rvm: 1.9.3, gemfile: gemfiles/rails_4.1.gemfile }
30
- - { rvm: jruby-19mode, gemfile: gemfiles/rails_4.1.gemfile }
25
+ - rvm: 1.8.7
26
+ gemfile: gemfiles/rails_4.0.gemfile
27
+ - rvm: 1.8.7
28
+ gemfile: gemfiles/rails_4.1.gemfile
29
+ - rvm: 1.9.2
30
+ gemfile: gemfiles/rails_4.0.gemfile
31
+ - rvm: 1.9.2
32
+ gemfile: gemfiles/rails_4.1.gemfile
33
+ - rvm: 1.9.3
34
+ gemfile: gemfiles/rails_4.1.gemfile
35
+ - rvm: jruby-19mode
36
+ gemfile: gemfiles/rails_4.1.gemfile
@@ -4,6 +4,11 @@
4
4
 
5
5
  * deprecated old aasm_* class methods (old-style DSL), in preparation for AASM v4.0.0
6
6
 
7
+ ## 3.2.0
8
+
9
+ * support [Sequel](http://sequel.jeremyevans.net/) (see [issue #119](https://github.com/aasm/aasm/issues/119), thanks to [@godfat](https://github.com/godfat))
10
+ * may not fire an unknown event (see [issue #128](https://github.com/aasm/aasm/issues/128)
11
+
7
12
  ## 3.1.1
8
13
 
9
14
  * bugfix: don't require ActiveRecord for localizing AASM event and state name (see [issue #113](https://github.com/aasm/aasm/issues/113), thanks to [@silentshade](https://github.com/silentshade))
data/Gemfile CHANGED
@@ -5,7 +5,8 @@ gem "coveralls", :platforms => :ruby
5
5
  gem 'rubysl', :platforms => :rbx
6
6
  gem "jruby-openssl", :platforms => :jruby
7
7
  gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
8
- gem "rails", "3.2.15"
8
+ gem "rails", "3.2.18"
9
9
  gem 'mongoid' if Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version.create('1.9.3')
10
+ gem 'sequel'
10
11
 
11
12
  gemspec
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # AASM - Ruby state machines
2
2
 
3
3
  <a href="http://badge.fury.io/rb/aasm"><img src="https://badge.fury.io/rb/aasm@2x.png" alt="Gem Version" height="18"></a>
4
- [![Build Status](https://secure.travis-ci.org/aasm/aasm.png?branch=master)](http://travis-ci.org/aasm/aasm)
4
+ [![Build Status](https://travis-ci.org/aasm/aasm.svg?branch=master)](https://travis-ci.org/aasm/aasm)
5
5
  [![Code Climate](https://codeclimate.com/github/aasm/aasm.png)](https://codeclimate.com/github/aasm/aasm)
6
6
  [![Coverage Status](https://coveralls.io/repos/aasm/aasm/badge.png?branch=master)](https://coveralls.io/r/aasm/aasm)
7
7
 
@@ -278,6 +278,23 @@ class Job < ActiveRecord::Base
278
278
  end
279
279
  ```
280
280
 
281
+ ### Sequel
282
+
283
+ AASM also supports [Sequel](http://sequel.jeremyevans.net/) besides _ActiveRecord_ and _Mongoid_.
284
+
285
+ ```ruby
286
+ class Job < Sequel::Model
287
+ include AASM
288
+
289
+ aasm do # default column: aasm_state
290
+ ...
291
+ end
292
+ end
293
+ ```
294
+
295
+ However it's not yet as feature complete as _ActiveRecord_. For example, there are
296
+ scopes defined yet. See [Automatic Scopes](#automatic-scopes).
297
+
281
298
  ### Mongoid
282
299
 
283
300
  AASM also supports persistence to Mongodb if you're using Mongoid. Make sure
@@ -308,7 +325,7 @@ class Job < ActiveRecord::Base
308
325
  state :cleaning
309
326
  end
310
327
 
311
- def sleeping
328
+ def self.sleeping
312
329
  "This method name is in already use"
313
330
  end
314
331
  end
@@ -317,10 +334,10 @@ end
317
334
  ```ruby
318
335
  class JobsController < ApplicationController
319
336
  def index
320
- @running_jobs = jobs.running
321
- @recent_cleaning_jobs = jobs.cleaning.where('created_at >= ?', 3.days.ago)
337
+ @running_jobs = Job.running
338
+ @recent_cleaning_jobs = Job.cleaning.where('created_at >= ?', 3.days.ago)
322
339
 
323
- # @sleeping_jobs = jobs.sleeping #=> "This method name is in already use"
340
+ # @sleeping_jobs = Job.sleeping #=> "This method name is in already use"
324
341
  end
325
342
  end
326
343
  ```
@@ -431,13 +448,13 @@ Given the `Job` class from above:
431
448
  ```ruby
432
449
  job = Job.new
433
450
 
434
- job.aasm.states
451
+ job.aasm.states.map(&:name)
435
452
  => [:sleeping, :running, :cleaning]
436
453
 
437
- job.aasm.states(:permissible => true)
454
+ job.aasm.states(:permissible => true).map(&:name)
438
455
  => [:running]
439
456
  job.run
440
- job.aasm.states(:permissible => true)
457
+ job.aasm.states(:permissible => true).map(&:name)
441
458
  => [:cleaning, :sleeping]
442
459
 
443
460
  job.aasm.events
@@ -6,7 +6,8 @@ gem 'rubysl', :platforms => :rbx
6
6
  gem 'rubinius-developer_tools', :platforms => :rbx
7
7
  gem "jruby-openssl", :platforms => :jruby
8
8
  gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
9
- gem "rails", "3.2.16"
9
+ gem "rails", "3.2.18"
10
10
  gem 'mongoid' if Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version.create('1.9.3')
11
+ gem 'sequel'
11
12
 
12
13
  gemspec :path => "../"
@@ -6,6 +6,11 @@ gem 'rubysl', :platforms => :rbx
6
6
  gem 'rubinius-developer_tools', :platforms => :rbx
7
7
  gem "jruby-openssl", :platforms => :jruby
8
8
  gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
9
- gem "rails", "4.0.2"
9
+ gem "rails", "4.0.5"
10
+
11
+ # mongoid is not yet compatible with Rails >= 4
12
+ # gem 'mongoid' if Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version.create('1.9.3')
13
+
14
+ gem 'sequel'
10
15
 
11
16
  gemspec :path => "../"
@@ -6,6 +6,11 @@ gem 'rubysl', :platforms => :rbx
6
6
  gem 'rubinius-developer_tools', :platforms => :rbx
7
7
  gem "jruby-openssl", :platforms => :jruby
8
8
  gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
9
- gem "rails", "4.1.0.beta1"
9
+ gem "rails", "4.1.1"
10
10
 
11
- gemspec :path => "../"
11
+ # mongoid is not yet compatible with Rails >= 4
12
+ # gem 'mongoid' if Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version.create('1.9.3')
13
+
14
+ gem 'sequel'
15
+
16
+ gemspec :path => "../"
@@ -75,8 +75,11 @@ module AASM
75
75
  end
76
76
 
77
77
  def may_fire_event?(name, *args)
78
- event = @instance.class.aasm.events[name]
79
- event.may_fire?(@instance, *args)
78
+ if event = @instance.class.aasm.events[name]
79
+ event.may_fire?(@instance, *args)
80
+ else
81
+ false # unknown event
82
+ end
80
83
  end
81
84
 
82
85
  def set_current_state_with_persistence(state)
@@ -12,6 +12,9 @@ module AASM
12
12
  elsif hierarchy.include?("Mongoid::Document")
13
13
  require_files_for(:mongoid)
14
14
  base.send(:include, AASM::Persistence::MongoidPersistence)
15
+ elsif hierarchy.include?("Sequel::Model")
16
+ require_files_for(:sequel)
17
+ base.send(:include, AASM::Persistence::SequelPersistence)
15
18
  end
16
19
  end
17
20
 
@@ -0,0 +1,108 @@
1
+ module AASM
2
+ module Persistence
3
+ module SequelPersistence
4
+ def self.included(base)
5
+ base.send(:include, AASM::Persistence::Base)
6
+ base.send(:include, AASM::Persistence::SequelPersistence::InstanceMethods)
7
+ end
8
+
9
+ module InstanceMethods
10
+ def before_validation
11
+ aasm_ensure_initial_state
12
+ super
13
+ end
14
+
15
+ def before_create
16
+ aasm_ensure_initial_state
17
+ super
18
+ end
19
+
20
+ # Returns the value of the aasm_column - called from <tt>aasm.current_state</tt>
21
+ #
22
+ # If it's a new record, and the aasm state column is blank it returns the initial state
23
+ #
24
+ # class Foo < Sequel::Model
25
+ # include AASM
26
+ # aasm :column => :status do
27
+ # state :opened
28
+ # state :closed
29
+ # end
30
+ # end
31
+ #
32
+ # foo = Foo.new
33
+ # foo.current_state # => :opened
34
+ # foo.close
35
+ # foo.current_state # => :closed
36
+ #
37
+ # foo = Foo[1]
38
+ # foo.current_state # => :opened
39
+ # foo.aasm_state = nil
40
+ # foo.current_state # => nil
41
+ #
42
+ # NOTE: intended to be called from an event
43
+ #
44
+ # This allows for nil aasm states - be sure to add validation to your model
45
+ def aasm_read_state
46
+ state = send(self.class.aasm_column)
47
+ if new? && state.to_s.strip.empty?
48
+ aasm.determine_state_name(self.class.aasm.initial_state)
49
+ elsif state.nil?
50
+ nil
51
+ else
52
+ state.to_sym
53
+ end
54
+ end
55
+
56
+ # Ensures that if the aasm_state column is nil and the record is new
57
+ # that the initial state gets populated before validation on create
58
+ #
59
+ # foo = Foo.new
60
+ # foo.aasm_state # => nil
61
+ # foo.valid?
62
+ # foo.aasm_state # => "open" (where :open is the initial state)
63
+ #
64
+ #
65
+ # foo = Foo.find(:first)
66
+ # foo.aasm_state # => 1
67
+ # foo.aasm_state = nil
68
+ # foo.valid?
69
+ # foo.aasm_state # => nil
70
+ #
71
+ def aasm_ensure_initial_state
72
+ aasm.enter_initial_state if
73
+ send(self.class.aasm_column).to_s.strip.empty?
74
+ end
75
+
76
+ # Writes <tt>state</tt> to the state column and persists it to the database
77
+ #
78
+ # foo = Foo[1]
79
+ # foo.aasm.current_state # => :opened
80
+ # foo.close!
81
+ # foo.aasm.current_state # => :closed
82
+ # Foo[1].aasm.current_state # => :closed
83
+ #
84
+ # NOTE: intended to be called from an event
85
+ def aasm_write_state state
86
+ aasm_column = self.class.aasm_column
87
+ update_ony({aasm_column => state.to_s}, aasm_column)
88
+ end
89
+
90
+ # Writes <tt>state</tt> to the state column, but does not persist it to the database
91
+ #
92
+ # foo = Foo[1]
93
+ # foo.aasm.current_state # => :opened
94
+ # foo.close
95
+ # foo.aasm.current_state # => :closed
96
+ # Foo[1].aasm.current_state # => :opened
97
+ # foo.save
98
+ # foo.aasm.current_state # => :closed
99
+ # Foo[1].aasm.current_state # => :closed
100
+ #
101
+ # NOTE: intended to be called from an event
102
+ def aasm_write_state_without_persistence state
103
+ send("#{self.class.aasm_column}=", state.to_s)
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
@@ -1,3 +1,3 @@
1
1
  module AASM
2
- VERSION = "3.1.1"
2
+ VERSION = "3.2.0"
3
3
  end
@@ -72,4 +72,13 @@ describe 'when being unsuspended' do
72
72
 
73
73
  expect(auth.aasm.current_state).to eq(:passive)
74
74
  end
75
+
76
+ it "should be able to fire known events" do
77
+ expect(auth.aasm.may_fire_event?(:activate)).to be_true
78
+ end
79
+
80
+ it "should not be able to fire unknown events" do
81
+ expect(auth.aasm.may_fire_event?(:unknown)).to be_false
82
+ end
83
+
75
84
  end
@@ -147,6 +147,6 @@ describe 'mongoid', :if => Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version
147
147
  end
148
148
 
149
149
  rescue LoadError
150
- puts "Not running Mongoid specs because mongoid gem if not installed!!!"
150
+ puts "Not running Mongoid specs because mongoid gem is not installed!!!"
151
151
  end
152
152
  end
@@ -0,0 +1,103 @@
1
+
2
+ describe 'sequel' do
3
+ begin
4
+ require 'sequel'
5
+ require 'logger'
6
+ require 'spec_helper'
7
+
8
+ before(:all) do
9
+ db = Sequel.sqlite
10
+ # if you want to see the statements while running the spec enable the following line
11
+ # db.loggers << Logger.new($stderr)
12
+ db.create_table(:models) do
13
+ primary_key :id
14
+ String :status
15
+ end
16
+
17
+ @model = Class.new(Sequel::Model(db)) do
18
+ set_dataset(:models)
19
+ attr_accessor :default
20
+ include AASM
21
+ aasm :column => :status
22
+ aasm do
23
+ state :alpha, :initial => true
24
+ state :beta
25
+ state :gamma
26
+ event :release do
27
+ transitions :from => [:alpha, :beta, :gamma], :to => :beta
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ describe "instance methods" do
34
+ let(:model) {@model.new}
35
+
36
+ it "should respond to aasm persistence methods" do
37
+ expect(model).to respond_to(:aasm_read_state)
38
+ expect(model).to respond_to(:aasm_write_state)
39
+ expect(model).to respond_to(:aasm_write_state_without_persistence)
40
+ end
41
+
42
+ it "should return the initial state when new and the aasm field is nil" do
43
+ expect(model.aasm.current_state).to eq(:alpha)
44
+ end
45
+
46
+ it "should return the aasm column when new and the aasm field is not nil" do
47
+ model.status = "beta"
48
+ expect(model.aasm.current_state).to eq(:beta)
49
+ end
50
+
51
+ it "should return the aasm column when not new and the aasm_column is not nil" do
52
+ allow(model).to receive(:new?).and_return(false)
53
+ model.status = "gamma"
54
+ expect(model.aasm.current_state).to eq(:gamma)
55
+ end
56
+
57
+ it "should allow a nil state" do
58
+ allow(model).to receive(:new?).and_return(false)
59
+ model.status = nil
60
+ expect(model.aasm.current_state).to be_nil
61
+ end
62
+
63
+ it "should call aasm_ensure_initial_state on validation before create" do
64
+ expect(model).to receive(:aasm_ensure_initial_state).and_return(true)
65
+ model.valid?
66
+ end
67
+
68
+ it "should call aasm_ensure_initial_state before create, even if skipping validations" do
69
+ expect(model).to receive(:aasm_ensure_initial_state).and_return(true)
70
+ model.save(:validate => false)
71
+ end
72
+ end
73
+
74
+ describe 'subclasses' do
75
+ it "should have the same states as its parent class" do
76
+ expect(Class.new(@model).aasm.states).to eq(@model.aasm.states)
77
+ end
78
+
79
+ it "should have the same events as its parent class" do
80
+ expect(Class.new(@model).aasm.events).to eq(@model.aasm.events)
81
+ end
82
+
83
+ it "should have the same column as its parent even for the new dsl" do
84
+ expect(@model.aasm_column).to eq(:status)
85
+ expect(Class.new(@model).aasm_column).to eq(:status)
86
+ end
87
+ end
88
+
89
+ describe 'initial states' do
90
+ it 'should support conditions' do
91
+ @model.aasm do
92
+ initial_state lambda{ |m| m.default }
93
+ end
94
+
95
+ expect(@model.new(:default => :beta).aasm.current_state).to eq(:beta)
96
+ expect(@model.new(:default => :gamma).aasm.current_state).to eq(:gamma)
97
+ end
98
+ end
99
+
100
+ rescue LoadError
101
+ puts "Not running Sequel specs because sequel gem is not installed!!!"
102
+ end
103
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aasm
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.1
4
+ version: 3.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott Barron
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2014-03-01 00:00:00.000000000 Z
14
+ date: 2014-05-16 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rake
@@ -129,6 +129,7 @@ files:
129
129
  - lib/aasm/persistence/active_record_persistence.rb
130
130
  - lib/aasm/persistence/base.rb
131
131
  - lib/aasm/persistence/mongoid_persistence.rb
132
+ - lib/aasm/persistence/sequel_persistence.rb
132
133
  - lib/aasm/state.rb
133
134
  - lib/aasm/state_machine.rb
134
135
  - lib/aasm/transition.rb
@@ -174,6 +175,7 @@ files:
174
175
  - spec/unit/new_dsl_spec.rb
175
176
  - spec/unit/persistence/active_record_persistence_spec.rb
176
177
  - spec/unit/persistence/mongoid_persistance_spec.rb
178
+ - spec/unit/persistence/sequel_persistence_spec.rb
177
179
  - spec/unit/simple_example_spec.rb
178
180
  - spec/unit/state_spec.rb
179
181
  - spec/unit/subclassing_spec.rb
@@ -244,6 +246,7 @@ test_files:
244
246
  - spec/unit/new_dsl_spec.rb
245
247
  - spec/unit/persistence/active_record_persistence_spec.rb
246
248
  - spec/unit/persistence/mongoid_persistance_spec.rb
249
+ - spec/unit/persistence/sequel_persistence_spec.rb
247
250
  - spec/unit/simple_example_spec.rb
248
251
  - spec/unit/state_spec.rb
249
252
  - spec/unit/subclassing_spec.rb