smajn-track_changes 0.2.0

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/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Matt Haley
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,46 @@
1
+ = track_changes
2
+
3
+ TrackChanges is a Rails plugin to facilitate tracking
4
+ changes made to an ActiveRecord model in your
5
+ Controller#update actions.
6
+
7
+ A TrackChanges::Filter is instantiated on each run of the <tt>before_filter</tt>
8
+ so it should be thread-safe.
9
+
10
+ Consult the generated rdoc for more information.
11
+
12
+ == Example
13
+
14
+ class PostController < ApplicationController
15
+ include TrackChanges
16
+
17
+ before_filter :get_post, :except => [:index, :new]
18
+
19
+ track_changes_to :post do |r|
20
+ # r.changes => Model#changes made during update action
21
+ RAILS_DEFAULT_LOGGER.debug(r.changes.inspect)
22
+ end
23
+
24
+
25
+ def update
26
+ if @post.update_attributes(params[:post])
27
+ flash[:notice] = "Success."
28
+ else
29
+ render :action => "edit"
30
+ end
31
+ end
32
+
33
+ # Normal controller actions
34
+ # ...
35
+
36
+ protected
37
+
38
+ def get_post
39
+ @post = Post.find(params[:id])
40
+ end
41
+ end
42
+
43
+
44
+ == COPYRIGHT
45
+
46
+ Copyright (c) 2008 Matt Haley. See link:files/LICENSE.html for details.
data/TODO ADDED
@@ -0,0 +1,5 @@
1
+ Track Changes TODO List
2
+ =======================
3
+
4
+ * Tests
5
+ * Check for ivars
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :minor: 2
3
+ :patch: 0
4
+ :major: 0
@@ -0,0 +1,9 @@
1
+ require 'track_changes/instance_methods'
2
+ require 'track_changes/class_methods'
3
+
4
+ module TrackChanges
5
+ def self.included(controller)
6
+ controller.send(:include, InstanceMethods)
7
+ controller.extend(ClassMethods)
8
+ end
9
+ end
@@ -0,0 +1,27 @@
1
+ require 'track_changes/filter'
2
+
3
+ module TrackChanges
4
+ module ClassMethods
5
+ # Uses a combination of <tt>before_filter</tt> and <tt>after_filter</tt>
6
+ # to act on changes made to a module during a controllers <tt>update</tt>
7
+ # action.
8
+ #
9
+ # Parameters:
10
+ #
11
+ # * <tt>model</tt>: A symbol of the model instance name to track changes to
12
+ # * <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>.
13
+ # * <tt>&block</tt>: The supplied block is called with an instance of Result as its parameter.
14
+ def track_changes_to(models, options = {:only => :update}, &block)
15
+ append_before_filter(options) do |controller|
16
+ track_filter = Filter.new(models, block)
17
+ controller.track_changes_filter = track_filter
18
+
19
+ track_filter.before(controller)
20
+ end
21
+
22
+ append_after_filter(options)do |controller|
23
+ controller.track_changes_filter.after(controller)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,39 @@
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, call_proc)
8
+ @call_proc = call_proc
9
+ @model = model
10
+ @changes = []
11
+ @original = []
12
+ end
13
+
14
+ def before(controller)
15
+ @original = clone_model(controller)
16
+ end
17
+
18
+ def after(controller)
19
+ cur_model = clone_model(controller)
20
+ # If changed? returns true, then the update didn't success.
21
+ unless cur_model.changed?
22
+ @changes = changes_between(@original, cur_model)
23
+ @call_proc.call(Result.new(@model, controller, @changes))
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def changes_between(old, new)
30
+ old.attributes = new.attributes
31
+ old.changes
32
+ end
33
+
34
+ def clone_model(controller)
35
+ ivar = controller.instance_variable_get("@#{@model}")
36
+ ivar.clone if ivar.respond_to?(:clone)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,5 @@
1
+ module TrackChanges
2
+ module InstanceMethods
3
+ attr_accessor :track_changes_filter
4
+ end
5
+ end
@@ -0,0 +1,36 @@
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
@@ -0,0 +1,9 @@
1
+ require 'track_changes/result'
2
+ require 'track_changes/filter'
3
+ require 'track_changes/instance_methods'
4
+ require 'track_changes/class_methods'
5
+ require 'track_changes/base'
6
+
7
+ module TrackChanges
8
+
9
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'mocha'
5
+
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'track_changes'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,7 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class TrackChangesTest < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smajn-track_changes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Matt Haley
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-29 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Easier auditing of Rails model changes in your controllers.
17
+ email: matt@smajn.net
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README
26
+ - TODO
27
+ - LICENSE
28
+ - VERSION.yml
29
+ - lib/track_changes
30
+ - lib/track_changes/instance_methods.rb
31
+ - lib/track_changes/result.rb
32
+ - lib/track_changes/base.rb
33
+ - lib/track_changes/class_methods.rb
34
+ - lib/track_changes/filter.rb
35
+ - lib/track_changes.rb
36
+ - test/track_changes_test.rb
37
+ - test/test_helper.rb
38
+ has_rdoc: true
39
+ homepage: http://github.com/smajn/track_changes
40
+ post_install_message:
41
+ rdoc_options:
42
+ - --inline-source
43
+ - --charset=UTF-8
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.2.0
62
+ signing_key:
63
+ specification_version: 2
64
+ summary: Easier auditing of Rails model changes in your controllers.
65
+ test_files: []
66
+