1
0
Fork 0
mirror of https://github.com/aasm/aasm synced 2023-03-27 23:22:41 -04:00

added configuration option to disable automatic scope creation

This commit is contained in:
Thorsten Böttger 2013-08-06 16:55:32 +02:00
parent a4bc27cff0
commit a3700b2826
9 changed files with 53 additions and 2 deletions

View file

@ -1,5 +1,9 @@
# CHANGELOG
## 3.0.20
* added configuration option to disable automatic scope creation
## 3.0.19
* fixed deprecation warning with *Rails 4* (`Relation#update_all` with conditions is deprecated)

View file

@ -268,6 +268,22 @@ class JobsController < ApplicationController
end
```
If you don't need scopes (or simply don't want them), disable their creation when
defining the `AASM` states, like this:
```ruby
class Job < ActiveRecord::Base
include AASM
aasm :create_scopes => false do
state :sleeping, :initial => true
state :running
state :cleaning
end
end
```
### Transaction support
Since version *3.0.13* AASM supports ActiveRecord transactions. So whenever a transition

View file

@ -12,6 +12,12 @@ module AASM
@state_machine.config.whiny_transitions = true # this is the default, so let's cry
end
if options.key?(:create_scopes)
@state_machine.config.create_scopes = options[:create_scopes]
elsif @state_machine.config.create_scopes.nil?
@state_machine.config.create_scopes = true # this is the default, so let's create scopes
end
if options.key?(:skip_validation_on_save)
@state_machine.config.skip_validation_on_save = options[:skip_validation_on_save]
elsif @state_machine.config.skip_validation_on_save.nil?

View file

@ -87,7 +87,7 @@ module AASM
# make sure to create a (named) scope for each state
def state_with_scope(name, *args)
state_without_scope(name, *args)
unless @clazz.respond_to?(name)
if AASM::StateMachine[@clazz].config.create_scopes && !@clazz.respond_to?(name)
if @clazz.ancestors.map {|klass| klass.to_s}.include?("ActiveRecord::Base")
conditions = {"#{@clazz.table_name}.#{@clazz.aasm_column}" => name.to_s}

View file

@ -0,0 +1,10 @@
class NoScopeMongoid
include Mongoid::Document
include AASM
field :status, type: String
aasm :create_scopes => false, :column => :status do
state :ignored_scope
end
end

View file

@ -52,6 +52,13 @@ class SimpleNewDsl < ActiveRecord::Base
end
end
class NoScope < ActiveRecord::Base
include AASM
aasm :create_scopes => false do
state :ignored_scope
end
end
class Derivate < Simple
end

View file

@ -1,6 +1,6 @@
ActiveRecord::Schema.define(:version => 0) do
%w{gates readers writers transients simples simple_new_dsls thieves localizer_test_models persisted_states provided_and_persisted_states}.each do |table_name|
%w{gates readers writers transients simples simple_new_dsls no_scopes thieves localizer_test_models persisted_states provided_and_persisted_states}.each do |table_name|
create_table table_name, :force => true do |t|
t.string "aasm_state"
end

View file

@ -110,6 +110,10 @@ describe "named scopes with the new DSL" do
end
end
it "does not create scopes if requested" do
NoScope.should_not respond_to(:ignored_scope)
end
end
describe 'initial states' do

View file

@ -55,6 +55,10 @@ describe 'mongoid', :if => Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version
end
end
it "does not create scopes if requested" do
NoScopeMongoid.should_not respond_to(:ignored_scope)
end
end
describe "#find_in_state" do