From a3700b28267437c12c94e34f67a6a02c287167ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorsten=20Bo=CC=88ttger?= Date: Tue, 6 Aug 2013 16:55:32 +0200 Subject: [PATCH] added configuration option to disable automatic scope creation --- CHANGELOG.md | 4 ++++ README.md | 16 ++++++++++++++++ lib/aasm/base.rb | 6 ++++++ lib/aasm/persistence/base.rb | 2 +- spec/models/mongoid/no_scope_mongoid.rb | 10 ++++++++++ spec/models/persistence.rb | 7 +++++++ spec/schema.rb | 2 +- .../active_record_persistence_spec.rb | 4 ++++ .../unit/persistence/mongoid_persistance_spec.rb | 4 ++++ 9 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 spec/models/mongoid/no_scope_mongoid.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index f89a5e4..889054c 100644 --- a/CHANGELOG.md +++ b/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) diff --git a/README.md b/README.md index 55e8f6f..ff77d40 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/aasm/base.rb b/lib/aasm/base.rb index 87f8420..d992a62 100644 --- a/lib/aasm/base.rb +++ b/lib/aasm/base.rb @@ -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? diff --git a/lib/aasm/persistence/base.rb b/lib/aasm/persistence/base.rb index a771f8d..81aaebd 100644 --- a/lib/aasm/persistence/base.rb +++ b/lib/aasm/persistence/base.rb @@ -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} diff --git a/spec/models/mongoid/no_scope_mongoid.rb b/spec/models/mongoid/no_scope_mongoid.rb new file mode 100644 index 0000000..bf8c495 --- /dev/null +++ b/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 diff --git a/spec/models/persistence.rb b/spec/models/persistence.rb index 165c6e8..f38f2e5 100644 --- a/spec/models/persistence.rb +++ b/spec/models/persistence.rb @@ -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 diff --git a/spec/schema.rb b/spec/schema.rb index 7ddf433..d3854c6 100644 --- a/spec/schema.rb +++ b/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 diff --git a/spec/unit/persistence/active_record_persistence_spec.rb b/spec/unit/persistence/active_record_persistence_spec.rb index 78b63df..19a8e64 100644 --- a/spec/unit/persistence/active_record_persistence_spec.rb +++ b/spec/unit/persistence/active_record_persistence_spec.rb @@ -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 diff --git a/spec/unit/persistence/mongoid_persistance_spec.rb b/spec/unit/persistence/mongoid_persistance_spec.rb index 63df81d..8b07fd2 100644 --- a/spec/unit/persistence/mongoid_persistance_spec.rb +++ b/spec/unit/persistence/mongoid_persistance_spec.rb @@ -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