Skip to content

Commit

Permalink
added configuration option to disable automatic scope creation
Browse files Browse the repository at this point in the history
  • Loading branch information
alto committed Aug 6, 2013
1 parent a4bc27c commit a3700b2
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -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)
Expand Down
16 changes: 16 additions & 0 deletions README.md
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions lib/aasm/base.rb
Expand Up @@ -12,6 +12,12 @@ def initialize(clazz, options={}, &block)
@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?
Expand Down
2 changes: 1 addition & 1 deletion lib/aasm/persistence/base.rb
Expand Up @@ -87,7 +87,7 @@ class Base
# 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}
Expand Down
10 changes: 10 additions & 0 deletions spec/models/mongoid/no_scope_mongoid.rb
@@ -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
7 changes: 7 additions & 0 deletions spec/models/persistence.rb
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion spec/schema.rb
@@ -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
Expand Down
4 changes: 4 additions & 0 deletions spec/unit/persistence/active_record_persistence_spec.rb
Expand Up @@ -110,6 +110,10 @@
end
end

it "does not create scopes if requested" do
NoScope.should_not respond_to(:ignored_scope)
end

end

describe 'initial states' do
Expand Down
4 changes: 4 additions & 0 deletions spec/unit/persistence/mongoid_persistance_spec.rb
Expand Up @@ -55,6 +55,10 @@
end
end

it "does not create scopes if requested" do
NoScopeMongoid.should_not respond_to(:ignored_scope)
end

end

describe "#find_in_state" do
Expand Down

0 comments on commit a3700b2

Please sign in to comment.