vestal_versions 1.0.1 → 1.0.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/Rakefile CHANGED
@@ -13,8 +13,7 @@ begin
13
13
  g.email = 'steve@laserlemon.com'
14
14
  g.homepage = 'http://github.com/laserlemon/vestal_versions'
15
15
  g.authors = %w(laserlemon)
16
- g.add_development_dependency 'activerecord'
17
- g.add_development_dependency 'activesupport'
16
+ g.add_dependency 'activerecord', '>= 2.1.0'
18
17
  g.add_development_dependency 'shoulda'
19
18
  g.add_development_dependency 'mocha'
20
19
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.1
1
+ 1.0.2
@@ -35,7 +35,7 @@ module VestalVersions
35
35
  private
36
36
  # Returns whether a new version should be created upon updating the parent record.
37
37
  def create_version?
38
- version_changes.present?
38
+ !version_changes.blank?
39
39
  end
40
40
 
41
41
  # Creates a new version upon updating the parent record.
@@ -28,6 +28,9 @@ module VestalVersions
28
28
  :class_name => 'VestalVersions::Version',
29
29
  :dependent => :delete_all
30
30
  )
31
+ options.reverse_merge!(
32
+ :order => "#{options[:class_name].constantize.table_name}.number ASC"
33
+ )
31
34
 
32
35
  class_inheritable_accessor :vestal_versions_options
33
36
  self.vestal_versions_options = options.dup
@@ -2,8 +2,6 @@ module VestalVersions
2
2
  # Adds the ability to "reset" (or hard revert) a versioned ActiveRecord::Base instance.
3
3
  module Reset
4
4
  def self.included(base) # :nodoc:
5
- Version.send(:include, VersionMethods)
6
-
7
5
  base.class_eval do
8
6
  include InstanceMethods
9
7
  end
@@ -20,37 +18,11 @@ module VestalVersions
20
18
  # documentation for more details.
21
19
  def reset_to!(value)
22
20
  if saved = skip_version{ revert_to!(value) }
23
- versions.after(value).each(&version_reset_method)
21
+ versions.send(:delete_records, versions.after(value))
24
22
  reset_version
25
23
  end
26
24
  saved
27
25
  end
28
-
29
- private
30
- # The method used to individually remove versions from the version history by way of the
31
- # +reset_to!+ method. There are three options for the <tt>:dependent</tt> option given
32
- # to the +versioned+ method: <tt>:delete_all</tt>, <tt>:destroy</tt> and <tt>:nullify</tt>.
33
- # If none is given, <tt>:delete_all</tt> is the default.
34
- #
35
- # If <tt>:delete_all</tt> is given, each version will be deleted from the database,
36
- # triggering no callbacks. If <tt>:destroy</tt> is given, each version will likewise be
37
- # deleted from the database, but any callbacks associated with version destruction will be
38
- # triggered. If <tt>:nullify</tt> is specified, the version records will simply be
39
- # dissociated from the versioned parent record by setting its foreign key to nil.
40
- def version_reset_method
41
- vestal_versions_options[:dependent].to_s.sub(/_all$/, '').to_sym
42
- end
43
- end
44
-
45
- # Instance methods added to the VestalVersions::Version model to accomodate resetting the
46
- # parent ActiveRecord::Base instance.
47
- module VersionMethods
48
- # The +nullify+ method is meant to mimic the behavior of ActiveRecord when the parent of a
49
- # +has_many+ association (with <tt>:dependent => :nullify</tt>) is destroyed and the child
50
- # records are dissociated from the parent's primary key.
51
- def nullify
52
- update_attribute(:versioned_id, nil)
53
- end
54
26
  end
55
27
  end
56
28
  end
@@ -6,9 +6,6 @@ module VestalVersions
6
6
  # Associate polymorphically with the parent record.
7
7
  belongs_to :versioned, :polymorphic => true
8
8
 
9
- # Order versions by number, ascending by default.
10
- default_scope :order => "#{table_name}.number ASC"
11
-
12
9
  # ActiveRecord::Base#changes is an existing method, so before serializing the +changes+ column,
13
10
  # the existing +changes+ method is undefined. The overridden +changes+ method pertained to
14
11
  # dirty attributes, but will not affect the partial updates functionality as that's based on
@@ -64,9 +64,9 @@ module VestalVersions
64
64
  # still return a valid version number (useful for reversion).
65
65
  def number_at(value)
66
66
  case value
67
- when Date, Time then at(value).try(:number) || 1
67
+ when Date, Time then (v = at(value)) ? v.number : 1
68
68
  when Numeric then value.floor
69
- when String, Symbol then at(value).try(:number)
69
+ when String, Symbol then (v = at(value)) ? v.number : nil
70
70
  when Version then value.number
71
71
  end
72
72
  end
data/test/control_test.rb CHANGED
@@ -122,7 +122,7 @@ class ControlTest < Test::Unit::TestCase
122
122
  end
123
123
 
124
124
  should 'update the last version' do
125
- last_version = @user.versions.last
125
+ last_version = @user.versions(true).last
126
126
  assert_equal @last_version.id, last_version.id
127
127
  assert_not_equal @last_version.attributes, last_version.attributes
128
128
  end
@@ -142,7 +142,7 @@ class ControlTest < Test::Unit::TestCase
142
142
  end
143
143
 
144
144
  should 'update the last version' do
145
- last_version = @user.versions.last
145
+ last_version = @user.versions(true).last
146
146
  assert_equal @last_version.id, last_version.id
147
147
  assert_not_equal @last_version.attributes, last_version.attributes
148
148
  end
data/test/reset_test.rb CHANGED
@@ -3,6 +3,7 @@ require File.join(File.dirname(__FILE__), 'test_helper')
3
3
  class ResetTest < Test::Unit::TestCase
4
4
  context 'Resetting a model' do
5
5
  setup do
6
+ @original_dependent = User.reflect_on_association(:versions).options[:dependent]
6
7
  @user, @versions = User.new, []
7
8
  @names = ['Steve Richert', 'Stephen Richert', 'Stephen Jobs', 'Steve Jobs']
8
9
  @names.each do |name|
@@ -19,7 +20,7 @@ class ResetTest < Test::Unit::TestCase
19
20
  end
20
21
 
21
22
  should 'dissociate all versions after the target' do
22
- @versions.reverse.each_with_index do |version, i|
23
+ @versions.reverse.each do |version|
23
24
  @user.reset_to!(version)
24
25
  assert_equal 0, @user.versions(true).after(version).count
25
26
  end
@@ -27,11 +28,11 @@ class ResetTest < Test::Unit::TestCase
27
28
 
28
29
  context 'with the :dependent option as :delete_all' do
29
30
  setup do
30
- User.prepare_versioned_options(:dependent => :delete_all)
31
+ User.reflect_on_association(:versions).options[:dependent] = :delete_all
31
32
  end
32
33
 
33
34
  should 'delete all versions after the target version' do
34
- @versions.reverse.each_with_index do |version, i|
35
+ @versions.reverse.each do |version|
35
36
  later_versions = @user.versions.after(version)
36
37
  @user.reset_to!(version)
37
38
  later_versions.each do |later_version|
@@ -44,7 +45,7 @@ class ResetTest < Test::Unit::TestCase
44
45
 
45
46
  should 'not destroy all versions after the target version' do
46
47
  VestalVersions::Version.any_instance.stubs(:destroy).raises(RuntimeError)
47
- @versions.reverse.each_with_index do |version, i|
48
+ @versions.reverse.each do |version|
48
49
  assert_nothing_raised do
49
50
  @user.reset_to!(version)
50
51
  end
@@ -54,11 +55,11 @@ class ResetTest < Test::Unit::TestCase
54
55
 
55
56
  context 'with the :dependent option as :destroy' do
56
57
  setup do
57
- User.prepare_versioned_options(:dependent => :destroy)
58
+ User.reflect_on_association(:versions).options[:dependent] = :destroy
58
59
  end
59
60
 
60
61
  should 'delete all versions after the target version' do
61
- @versions.reverse.each_with_index do |version, i|
62
+ @versions.reverse.each do |version|
62
63
  later_versions = @user.versions.after(version)
63
64
  @user.reset_to!(version)
64
65
  later_versions.each do |later_version|
@@ -71,7 +72,7 @@ class ResetTest < Test::Unit::TestCase
71
72
 
72
73
  should 'destroy all versions after the target version' do
73
74
  VestalVersions::Version.any_instance.stubs(:destroy).raises(RuntimeError)
74
- @versions.reverse.each_with_index do |version, i|
75
+ @versions.reverse.each do |version|
75
76
  later_versions = @user.versions.after(version)
76
77
  if later_versions.empty?
77
78
  assert_nothing_raised do
@@ -88,11 +89,11 @@ class ResetTest < Test::Unit::TestCase
88
89
 
89
90
  context 'with the :dependent option as :nullify' do
90
91
  setup do
91
- User.prepare_versioned_options(:dependent => :nullify)
92
+ User.reflect_on_association(:versions).options[:dependent] = :nullify
92
93
  end
93
94
 
94
95
  should 'leave all versions after the target version' do
95
- @versions.reverse.each_with_index do |version, i|
96
+ @versions.reverse.each do |version|
96
97
  later_versions = @user.versions.after(version)
97
98
  @user.reset_to!(version)
98
99
  later_versions.each do |later_version|
@@ -103,5 +104,9 @@ class ResetTest < Test::Unit::TestCase
103
104
  end
104
105
  end
105
106
  end
107
+
108
+ teardown do
109
+ User.reflect_on_association(:versions).options[:dependent] = @original_dependent
110
+ end
106
111
  end
107
112
  end
@@ -10,7 +10,9 @@ class ReversionTest < Test::Unit::TestCase
10
10
  @user.update_attribute(:name, name)
11
11
  @attributes[@user.version] = @user.attributes
12
12
  time += 1.hour
13
- @user.versions.last.try(:update_attribute, :created_at, time)
13
+ if last_version = @user.versions.last
14
+ last_version.update_attribute(:created_at, time)
15
+ end
14
16
  @times[@user.version] = time
15
17
  end
16
18
  @user.reload.versions.reload
data/test/schema.rb CHANGED
@@ -38,3 +38,6 @@ class User < ActiveRecord::Base
38
38
  self[:first_name], self[:last_name] = names.split(' ', 2)
39
39
  end
40
40
  end
41
+
42
+ class MyCustomVersion < VestalVersions::Version
43
+ end
data/test/tagging_test.rb CHANGED
@@ -9,9 +9,10 @@ class TaggingTest < Test::Unit::TestCase
9
9
 
10
10
  should "update the version record's tag column" do
11
11
  tag_name = 'TAG'
12
- assert_not_equal tag_name, @user.versions.last.tag
12
+ last_version = @user.versions.last
13
+ assert_not_equal tag_name, last_version.tag
13
14
  @user.tag_version(tag_name)
14
- assert_equal tag_name, @user.versions.last.tag
15
+ assert_equal tag_name, last_version.reload.tag
15
16
  end
16
17
 
17
18
  should 'create a version record for an initial version' do
data/test/version_test.rb CHANGED
@@ -29,8 +29,8 @@ class VersionTest < Test::Unit::TestCase
29
29
  assert_not_equal @last_version, last_version
30
30
  end
31
31
 
32
- should 'default to ordering by number when finding' do
33
- order = VestalVersions::Version.send(:scope, :find)[:order]
32
+ should 'default to ordering by number when finding through association' do
33
+ order = @user.versions.send(:scope, :find)[:order]
34
34
  assert_equal 'versions.number ASC', order
35
35
  end
36
36
 
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{vestal_versions}
8
- s.version = "1.0.1"
8
+ s.version = "1.0.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["laserlemon"]
12
- s.date = %q{2010-01-03}
12
+ s.date = %q{2010-01-13}
13
13
  s.description = %q{Keep a DRY history of your ActiveRecord models' changes}
14
14
  s.email = %q{steve@laserlemon.com}
15
15
  s.extra_rdoc_files = [
@@ -88,19 +88,16 @@ Gem::Specification.new do |s|
88
88
  s.specification_version = 3
89
89
 
90
90
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
91
- s.add_development_dependency(%q<activerecord>, [">= 0"])
92
- s.add_development_dependency(%q<activesupport>, [">= 0"])
91
+ s.add_runtime_dependency(%q<activerecord>, [">= 2.1.0"])
93
92
  s.add_development_dependency(%q<shoulda>, [">= 0"])
94
93
  s.add_development_dependency(%q<mocha>, [">= 0"])
95
94
  else
96
- s.add_dependency(%q<activerecord>, [">= 0"])
97
- s.add_dependency(%q<activesupport>, [">= 0"])
95
+ s.add_dependency(%q<activerecord>, [">= 2.1.0"])
98
96
  s.add_dependency(%q<shoulda>, [">= 0"])
99
97
  s.add_dependency(%q<mocha>, [">= 0"])
100
98
  end
101
99
  else
102
- s.add_dependency(%q<activerecord>, [">= 0"])
103
- s.add_dependency(%q<activesupport>, [">= 0"])
100
+ s.add_dependency(%q<activerecord>, [">= 2.1.0"])
104
101
  s.add_dependency(%q<shoulda>, [">= 0"])
105
102
  s.add_dependency(%q<mocha>, [">= 0"])
106
103
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vestal_versions
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - laserlemon
@@ -9,28 +9,18 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-03 00:00:00 -05:00
12
+ date: 2010-01-13 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord
17
- type: :development
17
+ type: :runtime
18
18
  version_requirement:
19
19
  version_requirements: !ruby/object:Gem::Requirement
20
20
  requirements:
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: "0"
24
- version:
25
- - !ruby/object:Gem::Dependency
26
- name: activesupport
27
- type: :development
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: "0"
23
+ version: 2.1.0
34
24
  version:
35
25
  - !ruby/object:Gem::Dependency
36
26
  name: shoulda