track_changes 0.5.1 → 1.0.0.pre1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +46 -32
- data/TODO +1 -1
- data/VERSION.yml +4 -4
- data/lib/generators/track_changes/templates/initializer.rb +1 -0
- data/lib/generators/track_changes/templates/migration.rb +18 -0
- data/lib/generators/track_changes/templates/stylesheet.css +12 -0
- data/lib/generators/track_changes/track_changes_generator.rb +30 -0
- data/lib/track_changes/action_controller.rb +31 -0
- data/lib/track_changes/active_record.rb +26 -0
- data/lib/track_changes/around_save.rb +9 -0
- data/lib/track_changes/changes.rb +15 -0
- data/lib/track_changes/configuration.rb +3 -32
- data/lib/track_changes/current_user.rb +5 -0
- data/lib/track_changes/current_user_filter.rb +12 -0
- data/lib/track_changes/engine.rb +7 -0
- data/lib/track_changes.rb +11 -10
- data/test/functional/posts_controller_test.rb +39 -10
- data/test/railsapp/Gemfile +12 -0
- data/test/railsapp/Gemfile.lock +84 -0
- data/test/railsapp/Rakefile +4 -0
- data/test/{rails_root → railsapp}/app/controllers/application_controller.rb +1 -0
- data/test/railsapp/app/controllers/posts_controller.rb +40 -0
- data/test/railsapp/app/models/post.rb +3 -0
- data/test/railsapp/app/models/user.rb +3 -0
- data/test/railsapp/app/views/layouts/application.html.erb +12 -0
- data/test/railsapp/app/views/posts/_form.html.erb +13 -0
- data/test/railsapp/app/views/posts/_post.html.erb +3 -0
- data/test/railsapp/app/views/posts/edit.html.erb +5 -0
- data/test/railsapp/app/views/posts/index.html.erb +2 -0
- data/test/railsapp/config/application.rb +12 -0
- data/test/railsapp/config/boot.rb +13 -0
- data/test/railsapp/config/database.yml +5 -0
- data/test/railsapp/config/environment.rb +5 -0
- data/test/railsapp/config/environments/test.rb +10 -0
- data/test/railsapp/config/routes.rb +3 -0
- data/test/railsapp/config.ru +2 -0
- data/test/railsapp/db/test.sqlite3 +0 -0
- data/test/railsapp/log/test.log +6385 -0
- data/test/railsapp/script/rails +6 -0
- data/test/schema.rb +19 -0
- data/test/test_helper.rb +4 -26
- data/test/track_changes_test.rb +34 -0
- metadata +124 -88
- data/lib/track_changes/audit_filter.rb +0 -69
- data/lib/track_changes/base.rb +0 -9
- data/lib/track_changes/class_methods.rb +0 -74
- data/lib/track_changes/filter.rb +0 -43
- data/lib/track_changes/initializer.rb +0 -18
- data/lib/track_changes/instance_methods.rb +0 -5
- data/lib/track_changes/result.rb +0 -36
- data/test/base_test.rb +0 -20
- data/test/class_methods_test.rb +0 -29
- data/test/filter_test.rb +0 -73
- data/test/rails_root/app/controllers/posts_controller.rb +0 -36
- data/test/rails_root/app/models/audit.rb +0 -4
- data/test/rails_root/app/models/post.rb +0 -3
- data/test/rails_root/config/boot.rb +0 -110
- data/test/rails_root/config/database.yml +0 -3
- data/test/rails_root/config/environment.rb +0 -7
- data/test/rails_root/config/environments/test.rb +0 -7
- data/test/rails_root/config/initializers/new_rails_defaults.rb +0 -7
- data/test/rails_root/config/initializers/session_store.rb +0 -4
- data/test/rails_root/config/initializers/track_changes_setup.rb +0 -8
- data/test/rails_root/config/routes.rb +0 -3
- data/test/rails_root/db/migrate/20100115021125_create_audits.rb +0 -16
- data/test/rails_root/db/migrate/20100115021151_create_posts.rb +0 -15
- data/test/rails_root/log/test.log +0 -543
- data/test/rails_root/script/console +0 -3
- data/test/rails_root/script/generate +0 -3
- data/test/result_test.rb +0 -35
- data/test/track_changes/audit_filter_test.rb +0 -154
- data/test/track_changes/configuration_test.rb +0 -36
- data/test/track_changes/initializer_test.rb +0 -37
@@ -0,0 +1,6 @@
|
|
1
|
+
#!/usr/bin/env jruby.exe
|
2
|
+
# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
|
3
|
+
|
4
|
+
APP_PATH = File.expand_path('../../config/application', __FILE__)
|
5
|
+
require File.expand_path('../../config/boot', __FILE__)
|
6
|
+
require 'rails/commands'
|
data/test/schema.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
ActiveRecord::Schema.define(:version => 0) do
|
2
|
+
create_table :audits, :force => true do |t|
|
3
|
+
t.references :audited, :polymorphic => true, :null => false
|
4
|
+
t.references :user
|
5
|
+
t.text :change_set, :null => false
|
6
|
+
|
7
|
+
t.datetime :created_at, :null => false
|
8
|
+
end
|
9
|
+
|
10
|
+
create_table :posts, :force => true do |t|
|
11
|
+
t.string :title
|
12
|
+
t.text :body
|
13
|
+
end
|
14
|
+
|
15
|
+
create_table :users, :force => true do |t|
|
16
|
+
t.string :name
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -1,29 +1,7 @@
|
|
1
|
-
# Copied from the shoulda library
|
2
|
-
require 'fileutils'
|
3
|
-
|
4
|
-
# Load the environment
|
5
1
|
ENV['RAILS_ENV'] = 'test'
|
2
|
+
require File.expand_path("../railsapp/config/environment.rb", __FILE__)
|
3
|
+
require 'rails/test_help'
|
6
4
|
|
7
|
-
|
8
|
-
|
9
|
-
require "#{rails_root}/config/environment.rb"
|
10
|
-
|
11
|
-
# Load the testing framework
|
12
|
-
require 'test_help'
|
13
|
-
silence_warnings { RAILS_ENV = ENV['RAILS_ENV'] }
|
14
|
-
|
15
|
-
# Run the migrations
|
16
|
-
ActiveRecord::Migration.verbose = false
|
17
|
-
ActiveRecord::Migrator.migrate("#{RAILS_ROOT}/db/migrate")
|
18
|
-
|
19
|
-
# Setup the fixtures path
|
20
|
-
ActiveSupport::TestCase.fixture_path =
|
21
|
-
File.join(File.dirname(__FILE__), "fixtures")
|
22
|
-
|
23
|
-
class ActiveSupport::TestCase #:nodoc:
|
24
|
-
self.use_transactional_fixtures = false
|
25
|
-
self.use_instantiated_fixtures = false
|
5
|
+
def load_schema
|
6
|
+
load(File.dirname(__FILE__) + "/schema.rb")
|
26
7
|
end
|
27
|
-
|
28
|
-
require 'track_changes'
|
29
|
-
require 'shoulda'
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class TrackChangesTest < Test::Unit::TestCase
|
4
|
+
load_schema
|
5
|
+
|
6
|
+
def test_schema_has_loaded_correctly
|
7
|
+
assert_equal [], Audit.all
|
8
|
+
assert_equal [], Post.all
|
9
|
+
assert_equal [], User.all
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_should_add_track_changes_method_to_models
|
13
|
+
assert Post.new.respond_to?(:current_user)
|
14
|
+
assert Post.new.respond_to?(:current_user=)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_should_add_association_to_models
|
18
|
+
assert Post.new.respond_to?(:audits)
|
19
|
+
assert_equal [], Post.new.audits.all
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_should_audit_changes
|
23
|
+
user = User.create!(:name => "John Public")
|
24
|
+
post = Post.create!(:title => "Test Post", :body => "Hello, world!")
|
25
|
+
|
26
|
+
post.update_attributes(:title => "Title Changed")
|
27
|
+
|
28
|
+
audit = post.audits.desc.first
|
29
|
+
expected_change = {"title" => ["Test Post", "Title Changed"]}
|
30
|
+
|
31
|
+
assert_equal 2, post.audits.count
|
32
|
+
assert_equal expected_change, audit.change_set
|
33
|
+
end
|
34
|
+
end
|
metadata
CHANGED
@@ -1,24 +1,65 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: track_changes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease: false
|
4
|
+
prerelease: true
|
6
5
|
segments:
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
6
|
+
- 1
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- pre1
|
10
|
+
version: 1.0.0.pre1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
|
-
- Matt Haley
|
13
|
+
- Matt Haley
|
14
14
|
autorequire:
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-
|
18
|
+
date: 2010-08-19 00:00:00 -07:00
|
19
19
|
default_executable:
|
20
|
-
dependencies:
|
21
|
-
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rails
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - "="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
segments:
|
30
|
+
- 3
|
31
|
+
- 0
|
32
|
+
- 0
|
33
|
+
- rc
|
34
|
+
version: 3.0.0.rc
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: activerecord-jdbc-adapter
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
version: "0"
|
48
|
+
type: :development
|
49
|
+
version_requirements: *id002
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
name: activerecord-jdbcsqlite3-adapter
|
52
|
+
prerelease: false
|
53
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
version: "0"
|
61
|
+
type: :development
|
62
|
+
version_requirements: *id003
|
22
63
|
description: Easier auditing of Rails model changes in your controllers.
|
23
64
|
email: matt@smajnr.net
|
24
65
|
executables: []
|
@@ -26,76 +67,80 @@ executables: []
|
|
26
67
|
extensions: []
|
27
68
|
|
28
69
|
extra_rdoc_files:
|
29
|
-
- LICENSE
|
30
|
-
- README.rdoc
|
31
|
-
- TODO
|
70
|
+
- LICENSE
|
71
|
+
- README.rdoc
|
72
|
+
- TODO
|
32
73
|
files:
|
33
|
-
- LICENSE
|
34
|
-
- TODO
|
35
|
-
- VERSION.yml
|
36
|
-
- lib/track_changes.rb
|
37
|
-
- lib/track_changes/
|
38
|
-
- lib/track_changes/
|
39
|
-
- lib/track_changes/
|
40
|
-
- lib/track_changes
|
41
|
-
- lib/track_changes/
|
42
|
-
- lib/track_changes/
|
43
|
-
- lib/track_changes/
|
44
|
-
- lib/track_changes/
|
45
|
-
-
|
46
|
-
-
|
47
|
-
-
|
48
|
-
-
|
49
|
-
- test/
|
50
|
-
- test/
|
51
|
-
- test/
|
52
|
-
- test/
|
53
|
-
- test/
|
54
|
-
- test/
|
55
|
-
- test/
|
56
|
-
- test/
|
57
|
-
- test/
|
58
|
-
- test/
|
59
|
-
- test/
|
60
|
-
- test/
|
61
|
-
- test/
|
62
|
-
- test/
|
63
|
-
- test/
|
64
|
-
- test/
|
65
|
-
- test/
|
66
|
-
- test/
|
67
|
-
- test/
|
68
|
-
- test/
|
69
|
-
- test/
|
70
|
-
- test/
|
71
|
-
-
|
74
|
+
- LICENSE
|
75
|
+
- TODO
|
76
|
+
- VERSION.yml
|
77
|
+
- lib/generators/track_changes/templates/initializer.rb
|
78
|
+
- lib/generators/track_changes/templates/migration.rb
|
79
|
+
- lib/generators/track_changes/templates/stylesheet.css
|
80
|
+
- lib/generators/track_changes/track_changes_generator.rb
|
81
|
+
- lib/track_changes.rb
|
82
|
+
- lib/track_changes/action_controller.rb
|
83
|
+
- lib/track_changes/active_record.rb
|
84
|
+
- lib/track_changes/around_save.rb
|
85
|
+
- lib/track_changes/changes.rb
|
86
|
+
- lib/track_changes/configuration.rb
|
87
|
+
- lib/track_changes/current_user.rb
|
88
|
+
- lib/track_changes/current_user_filter.rb
|
89
|
+
- lib/track_changes/engine.rb
|
90
|
+
- test/functional/posts_controller_test.rb
|
91
|
+
- test/railsapp/Gemfile
|
92
|
+
- test/railsapp/Gemfile.lock
|
93
|
+
- test/railsapp/Rakefile
|
94
|
+
- test/railsapp/app/controllers/application_controller.rb
|
95
|
+
- test/railsapp/app/controllers/posts_controller.rb
|
96
|
+
- test/railsapp/app/models/post.rb
|
97
|
+
- test/railsapp/app/models/user.rb
|
98
|
+
- test/railsapp/app/views/layouts/application.html.erb
|
99
|
+
- test/railsapp/app/views/posts/_form.html.erb
|
100
|
+
- test/railsapp/app/views/posts/_post.html.erb
|
101
|
+
- test/railsapp/app/views/posts/edit.html.erb
|
102
|
+
- test/railsapp/app/views/posts/index.html.erb
|
103
|
+
- test/railsapp/config.ru
|
104
|
+
- test/railsapp/config/application.rb
|
105
|
+
- test/railsapp/config/boot.rb
|
106
|
+
- test/railsapp/config/database.yml
|
107
|
+
- test/railsapp/config/environment.rb
|
108
|
+
- test/railsapp/config/environments/test.rb
|
109
|
+
- test/railsapp/config/routes.rb
|
110
|
+
- test/railsapp/db/test.sqlite3
|
111
|
+
- test/railsapp/log/test.log
|
112
|
+
- test/railsapp/script/rails
|
113
|
+
- test/schema.rb
|
114
|
+
- test/test_helper.rb
|
115
|
+
- test/track_changes_test.rb
|
116
|
+
- README.rdoc
|
72
117
|
has_rdoc: true
|
73
118
|
homepage: http://github.com/mhaley/track_changes
|
74
119
|
licenses: []
|
75
120
|
|
76
121
|
post_install_message:
|
77
122
|
rdoc_options:
|
78
|
-
- --charset=UTF-8
|
123
|
+
- --charset=UTF-8
|
79
124
|
require_paths:
|
80
|
-
- lib
|
125
|
+
- lib
|
81
126
|
required_ruby_version: !ruby/object:Gem::Requirement
|
82
127
|
none: false
|
83
128
|
requirements:
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
version: "0"
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
segments:
|
132
|
+
- 0
|
133
|
+
version: "0"
|
90
134
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
135
|
none: false
|
92
136
|
requirements:
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
137
|
+
- - ">"
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
segments:
|
140
|
+
- 1
|
141
|
+
- 3
|
142
|
+
- 1
|
143
|
+
version: 1.3.1
|
99
144
|
requirements: []
|
100
145
|
|
101
146
|
rubyforge_project: mhaley
|
@@ -104,25 +149,16 @@ signing_key:
|
|
104
149
|
specification_version: 3
|
105
150
|
summary: Easier auditing of Rails model changes in your controllers.
|
106
151
|
test_files:
|
107
|
-
- test/
|
108
|
-
- test/
|
109
|
-
- test/
|
110
|
-
- test/functional/posts_controller_test.rb
|
111
|
-
- test/
|
112
|
-
- test/
|
113
|
-
- test/
|
114
|
-
- test/
|
115
|
-
- test/
|
116
|
-
- test/
|
117
|
-
- test/
|
118
|
-
- test/
|
119
|
-
- test/
|
120
|
-
- test/rails_root/config/initializers/track_changes_setup.rb
|
121
|
-
- test/rails_root/config/routes.rb
|
122
|
-
- test/rails_root/db/migrate/20100115021125_create_audits.rb
|
123
|
-
- test/rails_root/db/migrate/20100115021151_create_posts.rb
|
124
|
-
- test/result_test.rb
|
125
|
-
- test/test_helper.rb
|
126
|
-
- test/track_changes/audit_filter_test.rb
|
127
|
-
- test/track_changes/configuration_test.rb
|
128
|
-
- test/track_changes/initializer_test.rb
|
152
|
+
- test/schema.rb
|
153
|
+
- test/test_helper.rb
|
154
|
+
- test/track_changes_test.rb
|
155
|
+
- test/functional/posts_controller_test.rb
|
156
|
+
- test/railsapp/app/controllers/application_controller.rb
|
157
|
+
- test/railsapp/app/controllers/posts_controller.rb
|
158
|
+
- test/railsapp/app/models/post.rb
|
159
|
+
- test/railsapp/app/models/user.rb
|
160
|
+
- test/railsapp/config/application.rb
|
161
|
+
- test/railsapp/config/boot.rb
|
162
|
+
- test/railsapp/config/environment.rb
|
163
|
+
- test/railsapp/config/routes.rb
|
164
|
+
- test/railsapp/config/environments/test.rb
|
@@ -1,69 +0,0 @@
|
|
1
|
-
module TrackChanges
|
2
|
-
# An around filter that will audit changes to your models.
|
3
|
-
#
|
4
|
-
# Example:
|
5
|
-
# class OrdersController < ApplicationController
|
6
|
-
# before_filter :find_order, :except => [:index, :new, :create]
|
7
|
-
# around_filter AuditFilter.new(:order), :only => :update
|
8
|
-
#
|
9
|
-
# def update
|
10
|
-
# ...
|
11
|
-
# end
|
12
|
-
#
|
13
|
-
# protected
|
14
|
-
#
|
15
|
-
# def find_order
|
16
|
-
# @order = Order.find(params[:id])
|
17
|
-
# end
|
18
|
-
# end
|
19
|
-
class AuditFilter
|
20
|
-
attr_accessor :cached_models, :models
|
21
|
-
|
22
|
-
def initialize(*models)
|
23
|
-
models.flatten!
|
24
|
-
options = models.extract_options!
|
25
|
-
|
26
|
-
self.cached_models = {}
|
27
|
-
self.models = models
|
28
|
-
|
29
|
-
@audit_accessor = options[:audit_accessor] || :audits
|
30
|
-
@current_user = options[:current_user] || :current_user
|
31
|
-
@ignore_nils = if options[:ignore_nil] == false
|
32
|
-
false
|
33
|
-
else
|
34
|
-
true
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
def before(controller)
|
39
|
-
self.models.each do |model|
|
40
|
-
instance_variable_symbol = "@#{model}".to_sym
|
41
|
-
instance = controller.instance_variable_get(instance_variable_symbol)
|
42
|
-
next if instance.nil? && @ignore_nils
|
43
|
-
|
44
|
-
self.cached_models[instance_variable_symbol] = instance.clone
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
def after(controller)
|
49
|
-
self.cached_models.each_pair do |instance_variable_symbol, original_instance|
|
50
|
-
new_instance = controller.instance_variable_get(instance_variable_symbol)
|
51
|
-
next if new_instance.changed? # Dirty objects didn't save
|
52
|
-
|
53
|
-
changes = changes_between(new_instance, original_instance)
|
54
|
-
|
55
|
-
unless changes.empty?
|
56
|
-
audit = new_instance.send(@audit_accessor).create!(:user => controller.send(@current_user), :change_set => changes)
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
private
|
62
|
-
|
63
|
-
def changes_between(new, old)
|
64
|
-
clone = old.clone
|
65
|
-
clone.attributes = new.attributes
|
66
|
-
clone.changes
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
data/lib/track_changes/base.rb
DELETED
@@ -1,74 +0,0 @@
|
|
1
|
-
require 'active_support'
|
2
|
-
require 'track_changes/filter'
|
3
|
-
|
4
|
-
module TrackChanges
|
5
|
-
module ClassMethods
|
6
|
-
# Create an audit of the changes made to a module during a controller action.
|
7
|
-
# Some assumptions are made, primarily that the given models will have a
|
8
|
-
# polymorphic association to an Audit model. An example Audit model:
|
9
|
-
#
|
10
|
-
# class Audit < ActiveRecord::Base
|
11
|
-
# belongs_to :audited, :polymorphic => true
|
12
|
-
# belongs_to :user
|
13
|
-
# serialize :change_set
|
14
|
-
# end
|
15
|
-
#
|
16
|
-
# # Example model that would be audited:
|
17
|
-
# class Order < ActiveRecord::Base
|
18
|
-
# has_many :audits, :as => :audited
|
19
|
-
# end
|
20
|
-
#
|
21
|
-
# ==== Parameters
|
22
|
-
#
|
23
|
-
# * A symbol or many symbols of the name(s) of the instance variable to create audits for.
|
24
|
-
# * An optional hash that will be passed as filter conditions to <tt>around_filter</tt>
|
25
|
-
#
|
26
|
-
# ==== Examples
|
27
|
-
#
|
28
|
-
# # Create an audit of changes made to @order during the <tt>:update</tt> action.
|
29
|
-
# track_changes :order
|
30
|
-
#
|
31
|
-
# # Creates an audit of changes made to @order or @customer during the <tt>:update</tt> action.
|
32
|
-
# track_changes :order, :customer
|
33
|
-
#
|
34
|
-
# # Create an audit of changes made to @order during the <tt>:update</tt> or <tt>:process</tt> actions.
|
35
|
-
# track_changes :order, :only => [:update, :process]
|
36
|
-
def track_changes(*args)
|
37
|
-
options = args.extract_options!
|
38
|
-
models = args.flatten
|
39
|
-
options = {:only => :update} if options.empty?
|
40
|
-
|
41
|
-
configuration = TrackChanges::Initializer.instance.configuration
|
42
|
-
|
43
|
-
audit_filter = AuditFilter.new(models,
|
44
|
-
:audit_accessor => configuration.audit_association,
|
45
|
-
:current_user => configuration.current_user,
|
46
|
-
:ignore_nil => configuration.ignore_nil)
|
47
|
-
around_filter(audit_filter, options)
|
48
|
-
end
|
49
|
-
|
50
|
-
# Uses a combination of <tt>before_filter</tt> and <tt>after_filter</tt>
|
51
|
-
# to act on changes made to a module during a controllers <tt>update</tt>
|
52
|
-
# action.
|
53
|
-
#
|
54
|
-
# Parameters:
|
55
|
-
#
|
56
|
-
# * <tt>model</tt>: A symbol of the model instance name to track changes to
|
57
|
-
# * <tt>only_if_changed</tt>: If true, will only yield block if changes is not empty. Defaults to <tt>true</tt>.
|
58
|
-
# * <tt>options</tt>: An options hash the will be supplied to <tt>before_filter</tt> and <tt>after_filter</tt>. Defaults to <tt>:only => :update</tt>.
|
59
|
-
# * <tt>&block</tt>: The supplied block is called with an instance of Result as its parameter.
|
60
|
-
def track_changes_to(models, only_if_changed = true, options = {:only => :update}, &block)
|
61
|
-
warn "[DEPRECATED] track_changes_to is deprecated. Use track_changes instead"
|
62
|
-
append_before_filter(options) do |controller|
|
63
|
-
track_filter = Filter.new(models, only_if_changed, block)
|
64
|
-
controller.track_changes_filter = track_filter
|
65
|
-
|
66
|
-
track_filter.before(controller)
|
67
|
-
end
|
68
|
-
|
69
|
-
append_after_filter(options)do |controller|
|
70
|
-
controller.track_changes_filter.after(controller)
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|
data/lib/track_changes/filter.rb
DELETED
@@ -1,43 +0,0 @@
|
|
1
|
-
require 'track_changes/result'
|
2
|
-
|
3
|
-
module TrackChanges
|
4
|
-
class Filter
|
5
|
-
attr_accessor :call_proc, :changes, :model, :original
|
6
|
-
|
7
|
-
def initialize(model, yield_only_changed, call_proc)
|
8
|
-
@call_proc = call_proc
|
9
|
-
@model = model
|
10
|
-
@yield_only_changed = yield_only_changed
|
11
|
-
@changes = []
|
12
|
-
@original = []
|
13
|
-
end
|
14
|
-
|
15
|
-
def before(controller)
|
16
|
-
@original = clone_model(controller)
|
17
|
-
end
|
18
|
-
|
19
|
-
def after(controller)
|
20
|
-
cur_model = controller.instance_variable_get("@#{@model}")
|
21
|
-
# If changed? returns true, then the update didn't succeed.
|
22
|
-
unless cur_model.changed?
|
23
|
-
@changes = changes_between(@original, cur_model)
|
24
|
-
unless @yield_only_changed && @changes.empty?
|
25
|
-
@call_proc.call(Result.new(@model, controller, @changes))
|
26
|
-
end
|
27
|
-
end
|
28
|
-
nil
|
29
|
-
end
|
30
|
-
|
31
|
-
private
|
32
|
-
|
33
|
-
def changes_between(old, new)
|
34
|
-
old.attributes = new.attributes
|
35
|
-
old.changes
|
36
|
-
end
|
37
|
-
|
38
|
-
def clone_model(controller)
|
39
|
-
ivar = controller.instance_variable_get("@#{@model}")
|
40
|
-
ivar.clone if ivar.respond_to?(:clone)
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
@@ -1,18 +0,0 @@
|
|
1
|
-
module TrackChanges
|
2
|
-
class Initializer
|
3
|
-
attr_accessor :configuration
|
4
|
-
|
5
|
-
include Singleton
|
6
|
-
|
7
|
-
class << self
|
8
|
-
alias_method :__tr_instance, :instance
|
9
|
-
|
10
|
-
def instance(&block)
|
11
|
-
obj = __tr_instance
|
12
|
-
obj.configuration ||= Configuration.new
|
13
|
-
yield obj.configuration if block_given?
|
14
|
-
obj
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
data/lib/track_changes/result.rb
DELETED
@@ -1,36 +0,0 @@
|
|
1
|
-
module TrackChanges
|
2
|
-
# A Result instance is yielded by ClassMethods#track_changes_to.
|
3
|
-
#
|
4
|
-
# Example:
|
5
|
-
#
|
6
|
-
# track_changes_to :post do |result|
|
7
|
-
# result.post # => The instance variable in the current_controler
|
8
|
-
# # given by the :post option.
|
9
|
-
# result.controller # => The instance of the current controller
|
10
|
-
# result.current_user # => The current controllers @current_user
|
11
|
-
# result.some_method # => Call some_method on the current controller instance
|
12
|
-
# end
|
13
|
-
class Result
|
14
|
-
attr_accessor :changes, :controller
|
15
|
-
|
16
|
-
def initialize(model_name, controller, changes)
|
17
|
-
@changes = changes
|
18
|
-
@controller = controller
|
19
|
-
|
20
|
-
self.class.send(:define_method, model_name) do
|
21
|
-
@controller.instance_variable_get("@#{model_name}")
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
# Pass all other methods to the controller.
|
26
|
-
def method_missing(method_sym, *args)
|
27
|
-
controller.send(method_sym, *args)
|
28
|
-
end
|
29
|
-
|
30
|
-
# A convienence method that returns the <tt>@current_user</tt>
|
31
|
-
# in the current controller.
|
32
|
-
def current_user
|
33
|
-
user = controller.instance_variable_get("@current_user")
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
data/test/base_test.rb
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class BaseTest < Test::Unit::TestCase
|
4
|
-
context "when TrackChanges is included in a Class" do
|
5
|
-
setup do
|
6
|
-
class A
|
7
|
-
include TrackChanges
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
should "include InstanceMethods" do
|
12
|
-
assert A.included_modules.include?(TrackChanges::InstanceMethods)
|
13
|
-
end
|
14
|
-
|
15
|
-
should "extend class with ClassMethods" do
|
16
|
-
extended_modules = (class << A; self; end).included_modules
|
17
|
-
assert extended_modules.include?(TrackChanges::ClassMethods)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
data/test/class_methods_test.rb
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class ClassMethodsTest < Test::Unit::TestCase
|
4
|
-
include TrackChanges::ClassMethods
|
5
|
-
|
6
|
-
context "a ClassMethods module" do
|
7
|
-
context "when track_changes_to method called" do
|
8
|
-
setup { track_changes_to(:something) {} }
|
9
|
-
|
10
|
-
should "call append_before_filter" do
|
11
|
-
assert @before_filter
|
12
|
-
end
|
13
|
-
|
14
|
-
should "call append_after_filter" do
|
15
|
-
assert @after_filter
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
protected
|
21
|
-
|
22
|
-
def append_before_filter(options = {}, &block)
|
23
|
-
@before_filter = block
|
24
|
-
end
|
25
|
-
|
26
|
-
def append_after_filter(options = {}, &block)
|
27
|
-
@after_filter = block
|
28
|
-
end
|
29
|
-
end
|