yesterday 1.0.rc1 → 1.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -14,9 +14,17 @@ And then run the generator in your Rails 3 project:
14
14
 
15
15
  == Use tracking in your models
16
16
 
17
+ First create model that will hold your changesets:
18
+
19
+ class Changeset < ActiveRecord::Base
20
+ include Yesterday::ChangesetBase
21
+ end
22
+
23
+ Now assign the models you want to track:
24
+
17
25
  class Contact < ActiveRecord::Base
18
26
  has_many :addresses
19
- track_changes
27
+ track_changes :with => :changesets
20
28
  end
21
29
 
22
30
  class Address < ActiveRecord::Base
@@ -8,6 +8,9 @@ class CreateChangesets < ActiveRecord::Migration
8
8
  t.datetime :created_at
9
9
  t.datetime :updated_at
10
10
  end
11
+
12
+ create_index :changesets, [ :changed_object_id, :changed_object_type ]
13
+ create_index :changesets, [ :changed_object_id, :changed_object_type, :version_number ]
11
14
  end
12
15
 
13
16
  def self.down
data/lib/yesterday.rb CHANGED
@@ -2,7 +2,7 @@ require 'active_support'
2
2
  require 'active_support/core_ext'
3
3
  require 'active_record'
4
4
 
5
- require 'yesterday/changeset'
5
+ require 'yesterday/changeset_base'
6
6
  require 'yesterday/versioning'
7
7
  require 'yesterday/differ'
8
8
  require 'yesterday/versioned_object_creator'
@@ -1,25 +1,11 @@
1
1
  module Yesterday
2
- class Changeset < ActiveRecord::Base
2
+ module ChangesetBase
3
3
 
4
- before_create :determine_version_number, :determine_object_attributes
5
- belongs_to :changed_object, :polymorphic => true
6
-
7
- serialize :object_attributes
8
-
9
- def self.version(version_number)
10
- where(:version_number => version_number)
11
- end
12
-
13
- def self.for_changed_object(object)
14
- where(:changed_object_type => object.class.to_s, :changed_object_id => object.id)
15
- end
16
-
17
- def self.last_for(object)
18
- for_changed_object(object).order('created_at DESC').first
19
- end
20
-
21
- def self.version_number_for(object)
22
- last_for(object).try(:version_number) || 0
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ base.before_create :determine_version_number, :determine_object_attributes
7
+ base.belongs_to :changed_object, :polymorphic => true
8
+ base.serialize :object_attributes
23
9
  end
24
10
 
25
11
  def object
@@ -50,5 +36,24 @@ module Yesterday
50
36
  self.object_attributes = Yesterday::Serializer.new(changed_object).to_hash
51
37
  end
52
38
 
39
+
40
+ module ClassMethods
41
+ def version(version_number)
42
+ where(:version_number => version_number)
43
+ end
44
+
45
+ def for_changed_object(object)
46
+ where(:changed_object_type => object.class.to_s, :changed_object_id => object.id)
47
+ end
48
+
49
+ def last_for(object)
50
+ for_changed_object(object).order('created_at DESC').first
51
+ end
52
+
53
+ def version_number_for(object)
54
+ last_for(object).try(:version_number) || 0
55
+ end
56
+ end
57
+
53
58
  end
54
59
  end
@@ -46,11 +46,21 @@ module Yesterday
46
46
  @tracked_attributes || []
47
47
  end
48
48
 
49
+ def changeset_model
50
+ @changeset_model
51
+ end
52
+
53
+ def changeset_klass
54
+ changeset_model.classify.constantize
55
+ end
56
+
49
57
  def track_changes(options = {})
50
- send :include, InstanceMethods
58
+ with = options[:with] || :changeset
59
+ @changeset_model = with.to_s
51
60
 
61
+ send :include, InstanceMethods
52
62
  after_save :serialize_current_state
53
- exclude_tracking_for :associations => :changesets
63
+ exclude_tracking_for :associations => with.to_s.pluralize.to_sym
54
64
  end
55
65
 
56
66
  def version(version_number)
@@ -1,3 +1,3 @@
1
1
  module Yesterday
2
- VERSION = "1.0.rc1"
2
+ VERSION = "1.0.rc2"
3
3
  end
@@ -2,11 +2,11 @@ module Yesterday
2
2
  class Versioning
3
3
  class << self
4
4
  def create_changeset_for(object)
5
- Changeset.create :changed_object => object
5
+ object.class.changeset_klass.create :changed_object => object
6
6
  end
7
7
 
8
8
  def changesets_for(object)
9
- Changeset.for_changed_object(object)
9
+ object.class.changeset_klass.for_changed_object(object)
10
10
  end
11
11
 
12
12
  def current_version_number_for(object)
@@ -26,7 +26,7 @@ module Yesterday
26
26
  end
27
27
 
28
28
  def changeset_for(version_number, object)
29
- Changeset.for_changed_object(object).version(version_number).first
29
+ object.class.changeset_klass.for_changed_object(object).version(version_number).first
30
30
  end
31
31
 
32
32
  def object_attributes_for(version_number, object)
data/spec/models.rb CHANGED
@@ -1,20 +1,22 @@
1
+ class Changeset < ActiveRecord::Base
2
+ include Yesterday::ChangesetBase
3
+ end
4
+
1
5
  class Report < ActiveRecord::Base
2
6
  has_many :contacts
3
7
  has_many :companies
4
8
 
5
- track_changes
9
+ track_changes :with => :changesets
6
10
  end
7
11
 
8
12
  class Contact < ActiveRecord::Base
9
13
  has_and_belongs_to_many :addresses
10
14
  end
11
15
 
12
- class Address < ActiveRecord::Base
13
- end
16
+ class Address < ActiveRecord::Base; end
14
17
 
15
18
  class Company < ActiveRecord::Base
16
19
  has_and_belongs_to_many :addresses
17
-
18
- track_changes
20
+ track_changes :with => :changesets
19
21
  exclude_tracking_for :attributes => [:created_at, :updated_at]
20
22
  end
data/spec/spec_helper.rb CHANGED
@@ -9,7 +9,7 @@ RSpec.configure do |config|
9
9
  config.before(:each) do
10
10
  Contact.delete_all
11
11
  Address.delete_all
12
- Yesterday::Changeset.delete_all
12
+ Changeset.delete_all
13
13
  end
14
14
  end
15
15
 
@@ -10,9 +10,9 @@ describe Yesterday::Model do
10
10
  end
11
11
 
12
12
  it 'should create a new changeset when saved' do
13
- Yesterday::Changeset.count.should == 0
13
+ Changeset.count.should == 0
14
14
  Report.create :description => 'bar'
15
- Yesterday::Changeset.count.should == 1
15
+ Changeset.count.should == 1
16
16
  end
17
17
 
18
18
  it 'should return all related changesets' do
metadata CHANGED
@@ -1,56 +1,75 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: yesterday
3
- version: !ruby/object:Gem::Version
4
- version: 1.0.rc1
3
+ version: !ruby/object:Gem::Version
4
+ hash: 15424241
5
5
  prerelease: 4
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - rc
10
+ - 2
11
+ version: 1.0.rc2
6
12
  platform: ruby
7
- authors:
13
+ authors:
8
14
  - Diederick Lawson
9
15
  autorequire:
10
16
  bindir: bin
11
17
  cert_chain: []
12
- date: 2011-09-09 00:00:00.000000000Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
18
+
19
+ date: 2011-09-20 00:00:00 Z
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
15
22
  name: rspec
16
- requirement: &70215422679120 !ruby/object:Gem::Requirement
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
17
25
  none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '0'
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
22
33
  type: :development
23
- prerelease: false
24
- version_requirements: *70215422679120
25
- - !ruby/object:Gem::Dependency
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
26
36
  name: sqlite3
27
- requirement: &70215422678700 !ruby/object:Gem::Requirement
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
28
39
  none: false
29
- requirements:
30
- - - ! '>='
31
- - !ruby/object:Gem::Version
32
- version: '0'
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
33
47
  type: :development
34
- prerelease: false
35
- version_requirements: *70215422678700
36
- - !ruby/object:Gem::Dependency
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
37
50
  name: rails
38
- requirement: &70215422678280 !ruby/object:Gem::Requirement
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
39
53
  none: false
40
- requirements:
41
- - - ! '>='
42
- - !ruby/object:Gem::Version
43
- version: '0'
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
44
61
  type: :runtime
45
- prerelease: false
46
- version_requirements: *70215422678280
62
+ version_requirements: *id003
47
63
  description: Track history and view any made changes in your active record models
48
- email:
64
+ email:
49
65
  - webmaster@altovista.nl
50
66
  executables: []
67
+
51
68
  extensions: []
69
+
52
70
  extra_rdoc_files: []
53
- files:
71
+
72
+ files:
54
73
  - .gitignore
55
74
  - Gemfile
56
75
  - README.rdoc
@@ -58,7 +77,7 @@ files:
58
77
  - lib/generators/yesterday/install_generator.rb
59
78
  - lib/generators/yesterday/templates/create_changesets.rb
60
79
  - lib/yesterday.rb
61
- - lib/yesterday/changeset.rb
80
+ - lib/yesterday/changeset_base.rb
62
81
  - lib/yesterday/differ.rb
63
82
  - lib/yesterday/historical_value.rb
64
83
  - lib/yesterday/model.rb
@@ -79,29 +98,40 @@ files:
79
98
  - yesterday.gemspec
80
99
  homepage: http://www.altovista.nl
81
100
  licenses: []
101
+
82
102
  post_install_message:
83
103
  rdoc_options: []
84
- require_paths:
104
+
105
+ require_paths:
85
106
  - lib
86
- required_ruby_version: !ruby/object:Gem::Requirement
107
+ required_ruby_version: !ruby/object:Gem::Requirement
87
108
  none: false
88
- requirements:
89
- - - ! '>='
90
- - !ruby/object:Gem::Version
91
- version: '0'
92
- required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ hash: 3
113
+ segments:
114
+ - 0
115
+ version: "0"
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
117
  none: false
94
- requirements:
95
- - - ! '>'
96
- - !ruby/object:Gem::Version
118
+ requirements:
119
+ - - ">"
120
+ - !ruby/object:Gem::Version
121
+ hash: 25
122
+ segments:
123
+ - 1
124
+ - 3
125
+ - 1
97
126
  version: 1.3.1
98
127
  requirements: []
128
+
99
129
  rubyforge_project: yesterday
100
- rubygems_version: 1.8.10
130
+ rubygems_version: 1.8.7
101
131
  signing_key:
102
132
  specification_version: 3
103
133
  summary: Track history in your active record models
104
- test_files:
134
+ test_files:
105
135
  - spec/database.yml
106
136
  - spec/differ_spec.rb
107
137
  - spec/models.rb