From 11b628fd7756ce1c5792f126cb9f7bf7c58ecfea Mon Sep 17 00:00:00 2001 From: Gary Weaver Date: Fri, 11 Sep 2015 16:16:29 -0400 Subject: [PATCH] adding support for defining scopes via scopes: true option --- .gitignore | 1 + .travis.yml | 5 +- Appraisals | 2 +- Gemfile | 5 +- Gemfile.lock | 63 +++++++++++----------- README.md | 15 +++++- Rakefile | 2 +- gemfiles/activerecord_3.1.gemfile | 10 ---- gemfiles/activerecord_3.1.gemfile.lock | 41 -------------- gemfiles/activerecord_3.2.gemfile | 10 ---- gemfiles/activerecord_3.2.gemfile.lock | 41 -------------- gemfiles/activerecord_4.0.gemfile | 10 ---- gemfiles/activerecord_4.0.gemfile.lock | 49 ----------------- gemfiles/activerecord_4.1.gemfile | 11 ++++ gemfiles/activerecord_4.2.gemfile | 11 ++++ lib/mark_only.rb | 21 +++++++- lib/mark_only/version.rb | 2 +- test/mark_only_test.rb | 75 +++++++++++++++++--------- 18 files changed, 146 insertions(+), 228 deletions(-) delete mode 100644 gemfiles/activerecord_3.1.gemfile delete mode 100644 gemfiles/activerecord_3.1.gemfile.lock delete mode 100644 gemfiles/activerecord_3.2.gemfile delete mode 100644 gemfiles/activerecord_3.2.gemfile.lock delete mode 100644 gemfiles/activerecord_4.0.gemfile delete mode 100644 gemfiles/activerecord_4.0.gemfile.lock create mode 100644 gemfiles/activerecord_4.1.gemfile create mode 100644 gemfiles/activerecord_4.2.gemfile diff --git a/.gitignore b/.gitignore index 46ee239..474a1f8 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ ruby-gemset ruby-version tmp/ *.gem +*emfile.lock diff --git a/.travis.yml b/.travis.yml index 256d0f7..35a6479 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,4 @@ language: ruby rvm: - - 2.0.0 - - 1.9.3 -script: 'travis_retry bundle install && travis_retry rake appraisal:install && rake appraisal' \ No newline at end of file + - 2.2.3 +script: 'travis_retry gem install bundler && travis_retry bundle install && travis_retry appraisal install && rake appraisal' \ No newline at end of file diff --git a/Appraisals b/Appraisals index ad4f031..d47371a 100644 --- a/Appraisals +++ b/Appraisals @@ -1,4 +1,4 @@ -[3.1, 3.2, 4.0].each do |version| +[4.1, 4.2].each do |version| appraise "activerecord_#{version}" do gem 'activerecord', "~> #{version}" end diff --git a/Gemfile b/Gemfile index 6b74269..94d2514 100644 --- a/Gemfile +++ b/Gemfile @@ -2,6 +2,7 @@ source 'https://rubygems.org' gemspec -gem 'appraisal', '~> 0.5.2' -gem 'bundler', '>= 1.2.2' +gem 'appraisal' +gem 'bundler' gem 'sqlite3' +gem 'minitest' \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index 2a364ec..2c4a87b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,48 +1,51 @@ PATH remote: . specs: - mark_only (1.0.1) + mark_only (1.2.0) activerecord (>= 3.1, < 5) activesupport (>= 3.1, < 5) GEM remote: https://rubygems.org/ specs: - activemodel (4.0.2) - activesupport (= 4.0.2) - builder (~> 3.1.0) - activerecord (4.0.2) - activemodel (= 4.0.2) - activerecord-deprecated_finders (~> 1.0.2) - activesupport (= 4.0.2) - arel (~> 4.0.0) - activerecord-deprecated_finders (1.0.3) - activesupport (4.0.2) - i18n (~> 0.6, >= 0.6.4) - minitest (~> 4.2) - multi_json (~> 1.3) - thread_safe (~> 0.1) - tzinfo (~> 0.3.37) - appraisal (0.5.2) + activemodel (4.2.4) + activesupport (= 4.2.4) + builder (~> 3.1) + activerecord (4.2.4) + activemodel (= 4.2.4) + activesupport (= 4.2.4) + arel (~> 6.0) + activesupport (4.2.4) + i18n (~> 0.7) + json (~> 1.7, >= 1.7.7) + minitest (~> 5.1) + thread_safe (~> 0.3, >= 0.3.4) + tzinfo (~> 1.1) + appraisal (2.1.0) bundler rake - arel (4.0.1) - atomic (1.1.14) - builder (3.1.4) - i18n (0.6.9) - minitest (4.7.5) - multi_json (1.8.2) - rake (10.1.0) - sqlite3 (1.3.8) - thread_safe (0.1.3) - atomic - tzinfo (0.3.38) + thor (>= 0.14.0) + arel (6.0.3) + builder (3.2.2) + i18n (0.7.0) + json (1.8.3) + minitest (5.8.0) + rake (10.4.2) + sqlite3 (1.3.10) + thor (0.19.1) + thread_safe (0.3.5) + tzinfo (1.2.2) + thread_safe (~> 0.1) PLATFORMS ruby DEPENDENCIES - appraisal (~> 0.5.2) - bundler (>= 1.2.2) + appraisal + bundler mark_only! + minitest sqlite3 + +BUNDLED WITH + 1.10.6 diff --git a/README.md b/README.md index 614bc67..37ff330 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Want to only mark a column with a value when the record is deleted or destroyed, Once a record is marked, `deleted?`/`destroyed?` methods on a retrieved model instance return false. -Tested with ActiveRecord 3.1.x, 3.2.x, and 4.0.x via travis and appraisal. +Tested with ActiveRecord 4.1.x, 4.2.x via travis and appraisal. Code originally based on [Paranoia][paranoia] (by Ryan Bigg and others), but heavily modified. @@ -54,6 +54,9 @@ MarkOnly.configure do self.debug = false # the value that should indicate that a record is deleted self.deleted_value = 'deleted' + # only needed if you use scopes: + self.active_scope_name = 'active' + self.deleted_scope_name = 'deleted' end ... @@ -71,6 +74,16 @@ class Client < ActiveRecord::Base end ``` +To also add scopes to filter by the marked column value, use the scopes option: + +```ruby +class Client < ActiveRecord::Base + mark_only :some_column_to_mark, scopes: true + + ... +end +``` + #### Disabling Globally If you need to globally disable temporarily so that everything marked as mark_only will destroy/delete instead of work normally, which might be useful in a data cleanup script, use: diff --git a/Rakefile b/Rakefile index c516644..444a6de 100644 --- a/Rakefile +++ b/Rakefile @@ -11,6 +11,6 @@ task :appraise do |t| if ENV['BUNDLE_GEMFILE'] =~ /gemfiles/ load 'test/mark_only_test.rb' else - exec 'rake appraisal:install && rake appraisal' + exec 'appraisal install && rake appraisal' end end diff --git a/gemfiles/activerecord_3.1.gemfile b/gemfiles/activerecord_3.1.gemfile deleted file mode 100644 index 7de98fe..0000000 --- a/gemfiles/activerecord_3.1.gemfile +++ /dev/null @@ -1,10 +0,0 @@ -# This file was generated by Appraisal - -source "https://rubygems.org" - -gem "appraisal", "~> 0.5.2" -gem "bundler", ">= 1.2.2" -gem "sqlite3" -gem "activerecord", "~> 3.1" - -gemspec :path=>"../" \ No newline at end of file diff --git a/gemfiles/activerecord_3.1.gemfile.lock b/gemfiles/activerecord_3.1.gemfile.lock deleted file mode 100644 index dca528e..0000000 --- a/gemfiles/activerecord_3.1.gemfile.lock +++ /dev/null @@ -1,41 +0,0 @@ -PATH - remote: /Users/gary/github/FineLinePrototyping/mark_only - specs: - mark_only (1.0.1) - activerecord (>= 3.1, < 5) - activesupport (>= 3.1, < 5) - -GEM - remote: https://rubygems.org/ - specs: - activemodel (3.2.16) - activesupport (= 3.2.16) - builder (~> 3.0.0) - activerecord (3.2.16) - activemodel (= 3.2.16) - activesupport (= 3.2.16) - arel (~> 3.0.2) - tzinfo (~> 0.3.29) - activesupport (3.2.16) - i18n (~> 0.6, >= 0.6.4) - multi_json (~> 1.0) - appraisal (0.5.2) - bundler - rake - arel (3.0.3) - builder (3.0.4) - i18n (0.6.9) - multi_json (1.8.2) - rake (10.1.0) - sqlite3 (1.3.8) - tzinfo (0.3.38) - -PLATFORMS - ruby - -DEPENDENCIES - activerecord (~> 3.1) - appraisal (~> 0.5.2) - bundler (>= 1.2.2) - mark_only! - sqlite3 diff --git a/gemfiles/activerecord_3.2.gemfile b/gemfiles/activerecord_3.2.gemfile deleted file mode 100644 index ddd0701..0000000 --- a/gemfiles/activerecord_3.2.gemfile +++ /dev/null @@ -1,10 +0,0 @@ -# This file was generated by Appraisal - -source "https://rubygems.org" - -gem "appraisal", "~> 0.5.2" -gem "bundler", ">= 1.2.2" -gem "sqlite3" -gem "activerecord", "~> 3.2" - -gemspec :path=>"../" \ No newline at end of file diff --git a/gemfiles/activerecord_3.2.gemfile.lock b/gemfiles/activerecord_3.2.gemfile.lock deleted file mode 100644 index e46ad6e..0000000 --- a/gemfiles/activerecord_3.2.gemfile.lock +++ /dev/null @@ -1,41 +0,0 @@ -PATH - remote: /Users/gary/github/FineLinePrototyping/mark_only - specs: - mark_only (1.0.1) - activerecord (>= 3.1, < 5) - activesupport (>= 3.1, < 5) - -GEM - remote: https://rubygems.org/ - specs: - activemodel (3.2.16) - activesupport (= 3.2.16) - builder (~> 3.0.0) - activerecord (3.2.16) - activemodel (= 3.2.16) - activesupport (= 3.2.16) - arel (~> 3.0.2) - tzinfo (~> 0.3.29) - activesupport (3.2.16) - i18n (~> 0.6, >= 0.6.4) - multi_json (~> 1.0) - appraisal (0.5.2) - bundler - rake - arel (3.0.3) - builder (3.0.4) - i18n (0.6.9) - multi_json (1.8.2) - rake (10.1.0) - sqlite3 (1.3.8) - tzinfo (0.3.38) - -PLATFORMS - ruby - -DEPENDENCIES - activerecord (~> 3.2) - appraisal (~> 0.5.2) - bundler (>= 1.2.2) - mark_only! - sqlite3 diff --git a/gemfiles/activerecord_4.0.gemfile b/gemfiles/activerecord_4.0.gemfile deleted file mode 100644 index 0519d79..0000000 --- a/gemfiles/activerecord_4.0.gemfile +++ /dev/null @@ -1,10 +0,0 @@ -# This file was generated by Appraisal - -source "https://rubygems.org" - -gem "appraisal", "~> 0.5.2" -gem "bundler", ">= 1.2.2" -gem "sqlite3" -gem "activerecord", "~> 4.0" - -gemspec :path=>"../" \ No newline at end of file diff --git a/gemfiles/activerecord_4.0.gemfile.lock b/gemfiles/activerecord_4.0.gemfile.lock deleted file mode 100644 index 6ffef66..0000000 --- a/gemfiles/activerecord_4.0.gemfile.lock +++ /dev/null @@ -1,49 +0,0 @@ -PATH - remote: /Users/gary/github/FineLinePrototyping/mark_only - specs: - mark_only (1.0.1) - activerecord (>= 3.1, < 5) - activesupport (>= 3.1, < 5) - -GEM - remote: https://rubygems.org/ - specs: - activemodel (4.0.2) - activesupport (= 4.0.2) - builder (~> 3.1.0) - activerecord (4.0.2) - activemodel (= 4.0.2) - activerecord-deprecated_finders (~> 1.0.2) - activesupport (= 4.0.2) - arel (~> 4.0.0) - activerecord-deprecated_finders (1.0.3) - activesupport (4.0.2) - i18n (~> 0.6, >= 0.6.4) - minitest (~> 4.2) - multi_json (~> 1.3) - thread_safe (~> 0.1) - tzinfo (~> 0.3.37) - appraisal (0.5.2) - bundler - rake - arel (4.0.1) - atomic (1.1.14) - builder (3.1.4) - i18n (0.6.9) - minitest (4.7.5) - multi_json (1.8.2) - rake (10.1.0) - sqlite3 (1.3.8) - thread_safe (0.1.3) - atomic - tzinfo (0.3.38) - -PLATFORMS - ruby - -DEPENDENCIES - activerecord (~> 4.0) - appraisal (~> 0.5.2) - bundler (>= 1.2.2) - mark_only! - sqlite3 diff --git a/gemfiles/activerecord_4.1.gemfile b/gemfiles/activerecord_4.1.gemfile new file mode 100644 index 0000000..779f17e --- /dev/null +++ b/gemfiles/activerecord_4.1.gemfile @@ -0,0 +1,11 @@ +# This file was generated by Appraisal + +source "https://rubygems.org" + +gem "appraisal" +gem "bundler" +gem "sqlite3" +gem "minitest" +gem "activerecord", "~> 4.1" + +gemspec :path => "../" diff --git a/gemfiles/activerecord_4.2.gemfile b/gemfiles/activerecord_4.2.gemfile new file mode 100644 index 0000000..ba23ee1 --- /dev/null +++ b/gemfiles/activerecord_4.2.gemfile @@ -0,0 +1,11 @@ +# This file was generated by Appraisal + +source "https://rubygems.org" + +gem "appraisal" +gem "bundler" +gem "sqlite3" +gem "minitest" +gem "activerecord", "~> 4.2" + +gemspec :path => "../" diff --git a/lib/mark_only.rb b/lib/mark_only.rb index 085e986..9d5364d 100644 --- a/lib/mark_only.rb +++ b/lib/mark_only.rb @@ -2,7 +2,7 @@ module MarkOnly class << self - attr_accessor :debug, :deleted_value, :enabled + attr_accessor :active_scope_name, :debug, :deleted_scope_name, :deleted_value, :enabled def configure(&blk) class_eval(&blk) end @@ -70,6 +70,9 @@ def destroy! self.debug = false self.deleted_value = 'deleted' self.enabled = true + # these are unused unless scopes: true + self.active_scope_name = 'active' + self.deleted_scope_name = 'deleted' end class ActiveRecord::Relation @@ -119,7 +122,7 @@ def destroy_all(*args, &block) class ActiveRecord::Base - def self.mark_only(col_name) + def self.mark_only(col_name, opts = {}) raise "#{self} must call mark_only with a column name!" unless col_name class_attribute :mark_only_column, instance_writer: true self.mark_only_column = col_name.to_sym @@ -135,6 +138,20 @@ class << self alias_method :mark_only_orig_destroy!, :destroy! include MarkOnlyRails4Extensions end + + if opts[:scopes] + raise "#{self} cannot call mark_only with scopes option unless MarkOnly active_scope_name and deleted_scope_name are configured" unless ::MarkOnly.active_scope_name && ::MarkOnly.deleted_scope_name + raise "#{self} cannot call mark_only with unsupported scopes value: #{opts[:scopes].inspect}" unless [true,false].include?(opts[:scopes]) + if defined?(ActiveRecord::VERSION::MAJOR) && ActiveRecord::VERSION::MAJOR > 4 + # Rails 5+ + scope ::MarkOnly.active_scope_name, ->() { where(col_name => nil).or.not(col_name => ::MarkOnly.deleted_value) } + else + quoted_col_name = self.connection.quote_column_name col_name + scope ::MarkOnly.active_scope_name, ->() { where("#{quoted_col_name} IS NULL OR #{quoted_col_name} != ?", ::MarkOnly.deleted_value) } + end + + scope ::MarkOnly.deleted_scope_name, ->() { where(col_name => ::MarkOnly.deleted_value) } + end end def self.mark_only? diff --git a/lib/mark_only/version.rb b/lib/mark_only/version.rb index 3437834..80f758c 100644 --- a/lib/mark_only/version.rb +++ b/lib/mark_only/version.rb @@ -1,3 +1,3 @@ module MarkOnly - VERSION = "1.1.0" + VERSION = "1.2.0" end diff --git a/test/mark_only_test.rb b/test/mark_only_test.rb index 86c0686..674c3f9 100644 --- a/test/mark_only_test.rb +++ b/test/mark_only_test.rb @@ -1,4 +1,4 @@ -require 'test/unit' +require 'minitest/autorun' require 'active_record' require File.expand_path(File.dirname(__FILE__) + "/../lib/mark_only") @@ -11,6 +11,7 @@ ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => DB_FILE ActiveRecord::Base.connection.execute 'CREATE TABLE parent_models (id INTEGER NOT NULL PRIMARY KEY, some_marked_column VARCHAR(32))' ActiveRecord::Base.connection.execute 'CREATE TABLE mark_only_models (id INTEGER NOT NULL PRIMARY KEY, some_marked_column VARCHAR(32))' +ActiveRecord::Base.connection.execute 'CREATE TABLE mark_only_scopes_true_models (id INTEGER NOT NULL PRIMARY KEY, some_marked_column VARCHAR(32))' ActiveRecord::Base.connection.execute 'CREATE TABLE featureful_models (id INTEGER NOT NULL PRIMARY KEY, name VARCHAR(32), some_marked_column VARCHAR(32))' ActiveRecord::Base.connection.execute 'CREATE TABLE child_models (id INTEGER NOT NULL PRIMARY KEY, parent_model_id INTEGER, some_marked_column VARCHAR(32))' ActiveRecord::Base.connection.execute 'CREATE TABLE plain_models (id INTEGER NOT NULL PRIMARY KEY)' @@ -20,17 +21,18 @@ ActiveRecord::Base.connection.execute 'CREATE TABLE employees (id INTEGER NOT NULL PRIMARY KEY, some_marked_column VARCHAR(32))' ActiveRecord::Base.connection.execute 'CREATE TABLE jobs (id INTEGER NOT NULL PRIMARY KEY, employer_id INTEGER NOT NULL, employee_id INTEGER NOT NULL, some_marked_column VARCHAR(32))' -class MiniTest::Filter +class Minitest::Filter def filter(bt) bt end end -class MarkOnlyTest < Test::Unit::TestCase +class MarkOnlyTest < Minitest::Test def setup ActiveRecord::Base.connection.execute 'DELETE FROM parent_models' ActiveRecord::Base.connection.execute 'DELETE FROM mark_only_models' + ActiveRecord::Base.connection.execute 'DELETE FROM mark_only_scopes_true_models' ActiveRecord::Base.connection.execute 'DELETE FROM featureful_models' ActiveRecord::Base.connection.execute 'DELETE FROM child_models' ActiveRecord::Base.connection.execute 'DELETE FROM plain_models' @@ -84,7 +86,7 @@ def test_mark_only_instance_delete assert_equal 0, model.class.count model.save assert_equal 1, model.class.count - assert_equal nil, model.some_marked_column + assert_nil model.some_marked_column model.delete assert_equal 1, model.class.count # won't work unless you reload model: assert_equal DELETED_MARK, model.some_marked_column @@ -97,7 +99,7 @@ def test_mark_only_disabled_instance_delete assert_equal 0, model.class.count model.save assert_equal 1, model.class.count - assert_equal nil, model.some_marked_column + assert_nil model.some_marked_column MarkOnly.enabled = false model.delete assert_equal 0, model.class.count @@ -109,7 +111,7 @@ def test_mark_only_instance_destroy assert_equal 0, model.class.count model.save assert_equal 1, model.class.count - assert_equal nil, model.some_marked_column + assert_nil model.some_marked_column model.destroy assert_equal 1, model.class.count # won't work unless you reload model: assert_equal DELETED_MARK, model.some_marked_column @@ -122,7 +124,7 @@ def test_mark_only_disabled_instance_destroy assert_equal 0, model.class.count model.save assert_equal 1, model.class.count - assert_equal nil, model.some_marked_column + assert_nil model.some_marked_column MarkOnly.enabled = false model.destroy assert_equal 0, model.class.count @@ -141,7 +143,7 @@ def test_mark_only_instance_destroy_bang_if_supported assert_equal 0, model.class.count model.save assert_equal 1, model.class.count - assert_equal nil, model.some_marked_column + assert_nil model.some_marked_column # Rails 4 raises ActiveRecord::RecordNotDestroyed model.destroy! fail "should raise ActiveRecord::RecordNotDestroyed. destroy! implemented in #{model.method(:destroy!)}" @@ -165,7 +167,7 @@ def test_mark_only_disabled_instance_destroy_bang_if_supported assert_equal 0, model.class.count model.save assert_equal 1, model.class.count - assert_equal nil, model.some_marked_column + assert_nil model.some_marked_column MarkOnly.enabled = false model.destroy! assert_equal 0, model.class.count @@ -178,7 +180,7 @@ def test_mark_only_class_delete assert_equal 0, model.class.count model.save assert_equal 1, model.class.count - assert_equal nil, model.some_marked_column + assert_nil model.some_marked_column assert_equal false, model.class.where(id: model.id).first.deleted? MarkOnlyModel.delete(model.id) assert_equal 1, model.class.count @@ -193,7 +195,7 @@ def test_mark_only_disabled_class_delete assert_equal 0, model.class.count model.save assert_equal 1, model.class.count - assert_equal nil, model.some_marked_column + assert_nil model.some_marked_column assert_equal false, model.class.where(id: model.id).first.deleted? MarkOnly.enabled = false MarkOnlyModel.delete(model.id) @@ -206,7 +208,7 @@ def test_mark_only_class_delete_all assert_equal 0, model.class.count model.save assert_equal 1, model.class.count - assert_equal nil, model.some_marked_column + assert_nil model.some_marked_column MarkOnlyModel.delete_all assert_equal 1, model.class.count # won't work unless you reload model: assert_equal DELETED_MARK, model.some_marked_column @@ -219,7 +221,7 @@ def test_mark_only_disabled_class_delete_all assert_equal 0, model.class.count model.save assert_equal 1, model.class.count - assert_equal nil, model.some_marked_column + assert_nil model.some_marked_column MarkOnly.enabled = false MarkOnlyModel.delete_all assert_equal 0, model.class.count @@ -233,7 +235,7 @@ def test_mark_only_to_param_destroy to_param = model.to_param assert_equal 1, model.class.count model.destroy - assert_not_equal nil, model.to_param + refute_nil model.to_param assert_equal to_param, model.to_param # won't work unless you reload model: assert_equal DELETED_MARK, model.some_marked_column assert_equal DELETED_MARK, model.class.where(id: model.id).first.some_marked_column @@ -343,7 +345,7 @@ def test_no_callback_on_instance_delete model = CallbackModel.new model.save model.delete - assert_equal nil, model.instance_variable_get(:@callback_called) + assert_nil model.instance_variable_get(:@callback_called) end def test_mark_only_disabled_no_callback_on_instance_delete @@ -351,7 +353,7 @@ def test_mark_only_disabled_no_callback_on_instance_delete model.save MarkOnly.enabled = false model.delete - assert_equal nil, model.instance_variable_get(:@callback_called) + assert_nil model.instance_variable_get(:@callback_called) end def test_does_callback_on_instance_destroy @@ -375,7 +377,7 @@ def test_mark_only_where_delete_id model = MarkOnlyModel.new model.save assert_equal 1, model.class.count - assert_equal nil, model.some_marked_column + assert_nil model.some_marked_column MarkOnlyModel.where('').delete(model.id) assert_equal 1, model.class.count # won't work unless you reload model: assert_equal DELETED_MARK, model.some_marked_column @@ -387,7 +389,7 @@ def test_mark_only_disabled_where_delete_id model = MarkOnlyModel.new model.save assert_equal 1, model.class.count - assert_equal nil, model.some_marked_column + assert_nil model.some_marked_column MarkOnly.enabled = false MarkOnlyModel.where('').delete(model.id) assert_equal 0, model.class.count @@ -398,7 +400,7 @@ def test_mark_only_where_delete_ids model = MarkOnlyModel.new model.save assert_equal 1, model.class.count - assert_equal nil, model.some_marked_column + assert_nil model.some_marked_column MarkOnlyModel.where('').delete([model.id]) assert_equal 1, model.class.count # won't work unless you reload model: assert_equal DELETED_MARK, model.some_marked_column @@ -410,7 +412,7 @@ def test_mark_only_disabled_where_delete_ids model = MarkOnlyModel.new model.save assert_equal 1, model.class.count - assert_equal nil, model.some_marked_column + assert_nil model.some_marked_column MarkOnly.enabled = false MarkOnlyModel.where('').delete([model.id]) assert_equal 0, model.class.count @@ -421,7 +423,7 @@ def test_mark_only_where_delete_all model = MarkOnlyModel.new model.save assert_equal 1, model.class.count - assert_equal nil, model.some_marked_column + assert_nil model.some_marked_column MarkOnlyModel.where('').delete_all assert_equal 1, model.class.count # won't work unless you reload model: assert_equal DELETED_MARK, model.some_marked_column @@ -433,7 +435,7 @@ def test_mark_only_disabled_where_delete_all model = MarkOnlyModel.new model.save assert_equal 1, model.class.count - assert_equal nil, model.some_marked_column + assert_nil model.some_marked_column MarkOnly.enabled = false MarkOnlyModel.where('').delete_all assert_equal 0, model.class.count @@ -444,7 +446,7 @@ def test_mark_only_where_destroy_id model = MarkOnlyModel.new model.save assert_equal 1, model.class.count - assert_equal nil, model.some_marked_column + assert_nil model.some_marked_column MarkOnlyModel.where('').destroy(model.id) assert_equal 1, model.class.count # won't work unless you reload model: assert_equal DELETED_MARK, model.some_marked_column @@ -456,7 +458,7 @@ def test_mark_only_disabled_where_destroy_id model = MarkOnlyModel.new model.save assert_equal 1, model.class.count - assert_equal nil, model.some_marked_column + assert_nil model.some_marked_column MarkOnly.enabled = false #require 'tracer' #Tracer.on do @@ -470,7 +472,7 @@ def test_mark_only_where_destroy_all model = MarkOnlyModel.new model.save assert_equal 1, model.class.count - assert_equal nil, model.some_marked_column + assert_nil model.some_marked_column MarkOnlyModel.where('').destroy_all assert_equal 1, model.class.count # won't work unless you reload model: assert_equal DELETED_MARK, model.some_marked_column @@ -482,13 +484,31 @@ def test_mark_only_disabled_where_destroy_all model = MarkOnlyModel.new model.save assert_equal 1, model.class.count - assert_equal nil, model.some_marked_column + assert_nil model.some_marked_column MarkOnly.enabled = false MarkOnlyModel.where('').destroy_all assert_equal 0, model.class.count assert_equal 0, ActiveRecord::Base.connection.select_all("SELECT count(*) as c FROM #{model.class.table_name} WHERE id = '#{model.id}'").first['c'] end + def test_mark_only_scopes_and_instance_delete + model = MarkOnlyScopesTrueModel.new + model.save + assert_nil model.some_marked_column + assert_equal 1, MarkOnlyScopesTrueModel.all.count + assert_equal 1, MarkOnlyScopesTrueModel.active.count + assert_equal 0, MarkOnlyScopesTrueModel.deleted.count + model.some_marked_column = 'active' + model.save + assert_equal 1, MarkOnlyScopesTrueModel.all.count + assert_equal 1, MarkOnlyScopesTrueModel.active.count + assert_equal 0, MarkOnlyScopesTrueModel.deleted.count + model.delete + refute_nil model.some_marked_column + assert_equal 0, MarkOnlyScopesTrueModel.active.count + assert_equal 1, MarkOnlyScopesTrueModel.deleted.count + end + end # Helper classes @@ -544,3 +564,6 @@ class CallbackModel < ActiveRecord::Base before_destroy {|model| model.instance_variable_set :@callback_called, true } end +class MarkOnlyScopesTrueModel < ActiveRecord::Base + mark_only :some_marked_column, scopes: true +end \ No newline at end of file