mongo_mapper_versioned 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,7 +2,7 @@
2
2
  module MongoMapper
3
3
  module Plugins
4
4
  module Versioned
5
- Version = '0.1.5'
5
+ Version = '0.1.6'
6
6
  end
7
7
  end
8
8
  end
@@ -5,13 +5,13 @@ module MongoMapper
5
5
  module ClassMethods
6
6
  def versioned(options={})
7
7
  configuration = { :ignored_keys => %w(_id version_number created_at updated_at creator_id updater_id) }
8
- configuration.update(options) if options.is_a?(Hash)
8
+ configuration[:ignored_keys].concat(options[:ignored_keys]).uniq if options.key?(:ignored_keys)
9
9
 
10
10
  key :version_number, Integer, :default => 0, :index => true
11
11
  many :versions, :class => MongoMapper::Plugins::Versioned::Version, :foreign_key => :versioned_id, :dependent => :destroy, :order => :version_number.asc
12
12
 
13
- after_create :create_version
14
- before_update :create_version, :if => Proc.new{ |doc| doc.should_create_version? }
13
+ after_create :create_version
14
+ after_update :create_version, :if => Proc.new{ |doc| doc.should_create_version? || doc.should_create_initial_version? }
15
15
 
16
16
  define_method "ignored_keys" do
17
17
  configuration[:ignored_keys]
@@ -24,7 +24,7 @@ module MongoMapper
24
24
  module InstanceMethods
25
25
 
26
26
  def create_version
27
- self.version_number = (versions.empty? ? 1 : versions.last.version_number+1)
27
+ self.version_number = versions.empty? ? 1 : versions.last.version_number+1
28
28
  self.versions << current_version
29
29
  end
30
30
 
@@ -48,6 +48,10 @@ module MongoMapper
48
48
  def version_at(target_version_number)
49
49
  versions.where(:version_number => target_version_number).first
50
50
  end
51
+
52
+ def should_create_initial_version?
53
+ versions.empty?
54
+ end
51
55
 
52
56
  # this method might be overwritten
53
57
  # by something more sophisticated (esp. in case of EmbeddedDocuments)
@@ -32,6 +32,41 @@ class VersionedTest < ActiveSupport::TestCase
32
32
  should "be last version" do
33
33
  assert @doc.last_version?
34
34
  end
35
+ context "after initial save" do
36
+ setup do
37
+ @doc.save
38
+ end
39
+ should "be on version 1" do
40
+ assert_equal 1, @doc.version_number
41
+ end
42
+ should "have 1 version" do
43
+ assert_equal 1, @doc.versions.count
44
+ end
45
+ should "store version 1" do
46
+ assert @doc.versions.any?{|v| v.version_number == 1}
47
+ end
48
+ should "be last version" do
49
+ assert @doc.last_version?
50
+ end
51
+ end
52
+ context "after save with changes" do
53
+ setup do
54
+ @doc.title = "poof"
55
+ @doc.save
56
+ end
57
+ should "be on version 1" do
58
+ assert_equal 1, @doc.version_number
59
+ end
60
+ should "have 1 version" do
61
+ assert_equal 1, @doc.versions.count
62
+ end
63
+ should "store version 1" do
64
+ assert @doc.versions.any?{|v| v.version_number == 1}
65
+ end
66
+ should "be last version" do
67
+ assert @doc.last_version?
68
+ end
69
+ end
35
70
  end
36
71
 
37
72
  context "when created" do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongo_mapper_versioned
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 5
10
- version: 0.1.5
9
+ - 6
10
+ version: 0.1.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tomas Celizna
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-12-26 00:00:00 +01:00
18
+ date: 2010-12-27 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency