acts_as_archival 0.5.1 → 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore CHANGED
@@ -7,4 +7,5 @@ test/aaa_test_app/vendor/bundle
7
7
  *.bkp
8
8
  *.gem
9
9
  Gemfile.lock
10
- pkg/*
10
+ pkg/*
11
+ .DS_Store
@@ -1,5 +1,10 @@
1
1
  # Changes!
2
2
 
3
+ ## 0.5.2
4
+ * More changes to support Rails 4
5
+ * fix scoping combinations with relations for Rails 4
6
+ * BREAKING CHANGE (possibly): removed the scope constants, so if you were using them, you should stop
7
+
3
8
  ## 0.5.1
4
9
  * update to use .table_name for archived scope
5
10
 
@@ -1,3 +1,3 @@
1
1
  module ActsAsArchival
2
- VERSION = "0.5.1"
2
+ VERSION = "0.5.2"
3
3
  end
@@ -2,9 +2,6 @@ module ExpectedBehavior
2
2
  module ActsAsArchival
3
3
  require 'digest/md5'
4
4
 
5
- ARCHIVED_CONDITIONS = lambda { |zelf| %Q{#{zelf.table_name}.archived_at IS NOT NULL AND #{zelf.table_name}.archive_number IS NOT NULL} }
6
- UNARCHIVED_CONDITIONS = { :archived_at => nil, :archive_number => nil }
7
-
8
5
  MissingArchivalColumnError = Class.new(ActiveRecord::ActiveRecordError) unless defined?(MissingArchivalColumnError) == 'constant' && MissingArchivalColumnError.class == Class
9
6
  CouldNotArchiveError = Class.new(ActiveRecord::ActiveRecordError) unless defined?(CouldNotArchiveError) == 'constant' && CouldNotArchiveError.class == Class
10
7
  CouldNotUnarchiveError = Class.new(ActiveRecord::ActiveRecordError) unless defined?(CouldNotUnarchiveError) == 'constant' && CouldNotUnarchiveError.class == Class
@@ -21,9 +18,9 @@ module ExpectedBehavior
21
18
  before_validation :raise_if_not_archival
22
19
  validate :readonly_when_archived if options[:readonly_when_archived]
23
20
 
24
- scope :archived, :conditions => ARCHIVED_CONDITIONS.call(self)
25
- scope :unarchived, :conditions => UNARCHIVED_CONDITIONS
26
- scope :archived_from_archive_number, lambda { |head_archive_number| {:conditions => ['archived_at IS NOT NULL AND archive_number = ?', head_archive_number] } }
21
+ scope :archived, lambda { where %Q{#{self.table_name}.archived_at IS NOT NULL AND #{self.table_name}.archive_number IS NOT NULL} }
22
+ scope :unarchived, lambda { where(:archived_at => nil, :archive_number => nil) }
23
+ scope :archived_from_archive_number, lambda { |head_archive_number| where(['archived_at IS NOT NULL AND archive_number = ?', head_archive_number]) }
27
24
 
28
25
  callbacks = ['archive','unarchive']
29
26
  define_callbacks *[callbacks, {:terminator => 'result == false'}].flatten
@@ -86,15 +83,15 @@ module ExpectedBehavior
86
83
  def unarchive(head_archive_number=nil)
87
84
  self.class.transaction do
88
85
  begin
89
- run_callbacks :unarchive, :before
90
- if self.archived?
91
- head_archive_number ||= self.archive_number
92
- self.archived_at = nil
93
- self.archive_number = nil
94
- self.save!
95
- self.unarchive_associations(head_archive_number)
86
+ run_callbacks :unarchive do
87
+ if self.archived?
88
+ head_archive_number ||= self.archive_number
89
+ self.archived_at = nil
90
+ self.archive_number = nil
91
+ self.save!
92
+ self.unarchive_associations(head_archive_number)
93
+ end
96
94
  end
97
- run_callbacks :unarchive, :after
98
95
  return true
99
96
  rescue => e
100
97
  ActiveRecord::Base.logger.try(:debug, e.message)
@@ -130,13 +127,13 @@ module ExpectedBehavior
130
127
  def act_on_a_related_archival(klass, key_name, id, head_archive_number, options={})
131
128
  return if options.length == 0 || (!options[:archive] && !options[:unarchive])
132
129
  if options[:archive]
133
- klass.unarchived.find(:all, :conditions => ["#{key_name} = ?", id]).each do |related_record|
130
+ klass.unarchived.where(["#{key_name} = ?", id]).each do |related_record|
134
131
  unless related_record.archive(head_archive_number)
135
132
  raise ActiveRecord::Rollback
136
133
  end
137
134
  end
138
135
  else
139
- klass.archived.find(:all, :conditions => ["#{key_name} = ? AND archive_number = ?", id, head_archive_number]).each do |related_record|
136
+ klass.archived.where(["#{key_name} = ? AND archive_number = ?", id, head_archive_number]).each do |related_record|
140
137
  unless related_record.unarchive(head_archive_number)
141
138
  raise ActiveRecord::Rollback
142
139
  end
@@ -3,10 +3,10 @@ require_relative "test_helper"
3
3
  class AmbiguousTableTest < ActiveSupport::TestCase
4
4
  # test against the problem fixed in http://github.com/DarkTatka/acts_as_archival/commit/63d0a2532a15d7a6ab41d081e1591108a5ea9b37
5
5
  test "no ambiguous table problem" do
6
- archival = Archival.create!(:name => "TEST")
6
+ archival = Archival.create!
7
7
  child = archival.archivals.create!
8
8
  child.archive
9
9
 
10
- assert_equal 1, Archival.unarchived.count(:joins => :archivals)
10
+ assert_equal 1, Archival.unarchived.joins(:archivals).count
11
11
  end
12
12
  end
@@ -40,8 +40,8 @@ class BasicTest < ActiveSupport::TestCase
40
40
  archival.archive
41
41
  sleep(0.001)
42
42
  after = DateTime.now
43
-
44
- assert_between before, after, archival.archived_at.to_datetime
43
+ assert before < archival.archived_at.to_datetime
44
+ assert after > archival.archived_at.to_datetime
45
45
  end
46
46
 
47
47
  test "archive sets the archive number to the md5 hexdigest for the model and id that is archived" do
@@ -12,4 +12,6 @@ class Archival < ActiveRecord::Base
12
12
  has_many :plains, :dependent => :destroy
13
13
  has_many :polys, :dependent => :destroy, :as => :archiveable
14
14
  has_many :independent_archivals
15
+
16
+ scope :bobs, lambda { where(:name => ["Bob", "Bobby", "Robert"]) }
15
17
  end
@@ -1,18 +1,20 @@
1
1
  require_relative "test_helper"
2
2
 
3
3
  class MassAttributeTest < ActiveSupport::TestCase
4
- test "archive works when attr_accessible present" do
5
- archival = MassAttributeProtected.create(:color => "pink")
6
- archival.archive
4
+ if $require_mass_protection
5
+ test "archive works when attr_accessible present" do
6
+ archival = MassAttributeProtected.create(:color => "pink")
7
+ archival.archive
7
8
 
8
- assert archival.reload.archived?
9
- end
9
+ assert archival.reload.archived?
10
+ end
10
11
 
11
- test "unarchive works when attr_accessible present" do
12
- archival = MassAttributeProtected.create(:color => "pink")
13
- archival.archive
14
- archival.unarchive
12
+ test "unarchive works when attr_accessible present" do
13
+ archival = MassAttributeProtected.create(:color => "pink")
14
+ archival.archive
15
+ archival.unarchive
15
16
 
16
- assert_not archival.reload.archived?
17
+ assert_not archival.reload.archived?
18
+ end
17
19
  end
18
20
  end
@@ -1,6 +1,6 @@
1
1
  require_relative "test_helper"
2
2
 
3
- class ActsAsArchivalTest < ActiveSupport::TestCase
3
+ class ScopeTest < ActiveSupport::TestCase
4
4
  test "simple unarchived scope" do
5
5
  Archival.create!
6
6
  Archival.create!
@@ -52,4 +52,27 @@ class ActsAsArchivalTest < ActiveSupport::TestCase
52
52
  assert_equal "SELECT `legacy`.* FROM `legacy` WHERE (legacy.archived_at IS NOT NULL AND legacy.archive_number IS NOT NULL)", ArchivalTableName.archived.to_sql
53
53
  assert_equal "SELECT `legacy`.* FROM `legacy` WHERE `legacy`.`archived_at` IS NULL AND `legacy`.`archive_number` IS NULL", ArchivalTableName.unarchived.to_sql
54
54
  end
55
+
56
+ test "combines with other scope properly" do
57
+ Archival.create!(:name => "Robert")
58
+ Archival.create!(:name => "Bobby")
59
+ Archival.create!(:name => "Sue")
60
+ Archival.create!(:name => "Bob").archive
61
+ assert_equal 3, Archival.bobs.count
62
+ assert_equal 3, Archival.unarchived.count
63
+ assert_equal 2, Archival.bobs.unarchived.count
64
+ assert_equal 2, Archival.unarchived.bobs.count
65
+ assert_equal 1, Archival.bobs.archived.count
66
+ assert_equal 1, Archival.archived.bobs.count
67
+ end
68
+
69
+ test "scopes combine with relations correctly" do
70
+ parent = Archival.create!
71
+ parent.archivals.create!
72
+ parent.archivals.create!
73
+ parent.archivals.create!.archive
74
+ assert_equal 3, parent.archivals.count
75
+ assert_equal 1, parent.archivals.archived.count
76
+ assert_equal 2, parent.archivals.unarchived.count
77
+ end
55
78
  end
@@ -1,9 +1,11 @@
1
1
  $:.unshift(File.dirname(__FILE__) + '/../lib')
2
2
  require "bundler/setup"
3
3
  require "test/unit"
4
+
4
5
  require "active_record"
5
6
  require "assertions"
6
7
  require "database_cleaner"
8
+
7
9
  require "acts_as_archival"
8
10
 
9
11
  def prepare_for_tests
@@ -43,19 +45,21 @@ def require_test_classes
43
45
  inflect.irregular "poly", "polys"
44
46
  end
45
47
 
46
- [:archival,
48
+ fixtures = [:archival,
47
49
  :archival_kid,
48
50
  :archival_grandkid,
49
51
  :archival_table_name,
50
52
  :exploder,
51
53
  :independent_archival,
52
- :mass_attribute_protected,
54
+ :plain,
53
55
  :missing_archived_at,
54
56
  :missing_archive_number,
55
57
  :plain,
56
58
  :poly,
57
- :readonly_when_archived
58
- ].each {|test_class_file| require_relative "fixtures/#{test_class_file}"}
59
+ :readonly_when_archived]
60
+ $require_mass_protection = ActiveModel.constants.include?(:MassAssignmentSecurity)
61
+ fixtures << :mass_attribute_protected if $require_mass_protection
62
+ fixtures.each {|test_class_file| require_relative "fixtures/#{test_class_file}"}
59
63
  end
60
64
 
61
65
  prepare_for_tests
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_archival
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: