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.
Files changed (73) hide show
  1. data/README.rdoc +46 -32
  2. data/TODO +1 -1
  3. data/VERSION.yml +4 -4
  4. data/lib/generators/track_changes/templates/initializer.rb +1 -0
  5. data/lib/generators/track_changes/templates/migration.rb +18 -0
  6. data/lib/generators/track_changes/templates/stylesheet.css +12 -0
  7. data/lib/generators/track_changes/track_changes_generator.rb +30 -0
  8. data/lib/track_changes/action_controller.rb +31 -0
  9. data/lib/track_changes/active_record.rb +26 -0
  10. data/lib/track_changes/around_save.rb +9 -0
  11. data/lib/track_changes/changes.rb +15 -0
  12. data/lib/track_changes/configuration.rb +3 -32
  13. data/lib/track_changes/current_user.rb +5 -0
  14. data/lib/track_changes/current_user_filter.rb +12 -0
  15. data/lib/track_changes/engine.rb +7 -0
  16. data/lib/track_changes.rb +11 -10
  17. data/test/functional/posts_controller_test.rb +39 -10
  18. data/test/railsapp/Gemfile +12 -0
  19. data/test/railsapp/Gemfile.lock +84 -0
  20. data/test/railsapp/Rakefile +4 -0
  21. data/test/{rails_root → railsapp}/app/controllers/application_controller.rb +1 -0
  22. data/test/railsapp/app/controllers/posts_controller.rb +40 -0
  23. data/test/railsapp/app/models/post.rb +3 -0
  24. data/test/railsapp/app/models/user.rb +3 -0
  25. data/test/railsapp/app/views/layouts/application.html.erb +12 -0
  26. data/test/railsapp/app/views/posts/_form.html.erb +13 -0
  27. data/test/railsapp/app/views/posts/_post.html.erb +3 -0
  28. data/test/railsapp/app/views/posts/edit.html.erb +5 -0
  29. data/test/railsapp/app/views/posts/index.html.erb +2 -0
  30. data/test/railsapp/config/application.rb +12 -0
  31. data/test/railsapp/config/boot.rb +13 -0
  32. data/test/railsapp/config/database.yml +5 -0
  33. data/test/railsapp/config/environment.rb +5 -0
  34. data/test/railsapp/config/environments/test.rb +10 -0
  35. data/test/railsapp/config/routes.rb +3 -0
  36. data/test/railsapp/config.ru +2 -0
  37. data/test/railsapp/db/test.sqlite3 +0 -0
  38. data/test/railsapp/log/test.log +6385 -0
  39. data/test/railsapp/script/rails +6 -0
  40. data/test/schema.rb +19 -0
  41. data/test/test_helper.rb +4 -26
  42. data/test/track_changes_test.rb +34 -0
  43. metadata +124 -88
  44. data/lib/track_changes/audit_filter.rb +0 -69
  45. data/lib/track_changes/base.rb +0 -9
  46. data/lib/track_changes/class_methods.rb +0 -74
  47. data/lib/track_changes/filter.rb +0 -43
  48. data/lib/track_changes/initializer.rb +0 -18
  49. data/lib/track_changes/instance_methods.rb +0 -5
  50. data/lib/track_changes/result.rb +0 -36
  51. data/test/base_test.rb +0 -20
  52. data/test/class_methods_test.rb +0 -29
  53. data/test/filter_test.rb +0 -73
  54. data/test/rails_root/app/controllers/posts_controller.rb +0 -36
  55. data/test/rails_root/app/models/audit.rb +0 -4
  56. data/test/rails_root/app/models/post.rb +0 -3
  57. data/test/rails_root/config/boot.rb +0 -110
  58. data/test/rails_root/config/database.yml +0 -3
  59. data/test/rails_root/config/environment.rb +0 -7
  60. data/test/rails_root/config/environments/test.rb +0 -7
  61. data/test/rails_root/config/initializers/new_rails_defaults.rb +0 -7
  62. data/test/rails_root/config/initializers/session_store.rb +0 -4
  63. data/test/rails_root/config/initializers/track_changes_setup.rb +0 -8
  64. data/test/rails_root/config/routes.rb +0 -3
  65. data/test/rails_root/db/migrate/20100115021125_create_audits.rb +0 -16
  66. data/test/rails_root/db/migrate/20100115021151_create_posts.rb +0 -15
  67. data/test/rails_root/log/test.log +0 -543
  68. data/test/rails_root/script/console +0 -3
  69. data/test/rails_root/script/generate +0 -3
  70. data/test/result_test.rb +0 -35
  71. data/test/track_changes/audit_filter_test.rb +0 -154
  72. data/test/track_changes/configuration_test.rb +0 -36
  73. data/test/track_changes/initializer_test.rb +0 -37
data/test/filter_test.rb DELETED
@@ -1,73 +0,0 @@
1
- require 'test_helper'
2
-
3
- class FilterTest < Test::Unit::TestCase
4
- context "a Filter instance" do
5
- setup do
6
- @controller = mock('@controller')
7
- @model = mock('@model')
8
- @proc_result = nil
9
- @proc = Proc.new { |value| @proc_result = value }
10
-
11
- @filter = TrackChanges::Filter.new(:model, true, @proc)
12
- end
13
-
14
- should "set @original when method before called" do
15
- @model.expects(:clone).returns(mock('@model cloned', :my_mock? => true))
16
- @controller.expects(:instance_variable_get).returns(@model)
17
- @filter.before(@controller)
18
-
19
- assert @filter.original.my_mock?
20
- end
21
-
22
- should "return a Result when after called" do
23
- @filter.original = mock('original', :attributes= => {"attr" => "new"},
24
- :changes => {"attr" => ["old", "new"]})
25
- @new_model = mock('new_model', :attributes => {"attr" => "new"},
26
- :changed? => false)
27
-
28
- @controller.expects(:instance_variable_get).once.returns(@new_model)
29
-
30
- @filter.after(@controller)
31
- assert @proc_result.is_a?(TrackChanges::Result)
32
- assert_equal({"attr" => ["old", "new"]}, @proc_result.changes)
33
- end
34
-
35
- context "when yield_only_changed is true" do
36
- setup do
37
- @proc_result = nil
38
- @filter = TrackChanges::Filter.new(:model, true, @proc)
39
- end
40
-
41
- should "not call block when method after called and changes is empty" do
42
- @filter.original = mock('original', :attributes= => {"attr" => "new"},
43
- :changes => {})
44
- @new_model = mock('new_model', :attributes => {"attr" => "new"},
45
- :changed? => false)
46
-
47
- @controller.expects(:instance_variable_get).once.returns(@new_model)
48
-
49
- @filter.after(@controller)
50
- assert_nil @proc_result
51
- end
52
- end
53
-
54
- context "when yield_only_changed is false" do
55
- setup do
56
- @proc_result = nil
57
- @filter = TrackChanges::Filter.new(:model, false, @proc)
58
- end
59
-
60
- should "call block when method after called and changes is empty" do
61
- @filter.original = mock('original', :attributes= => {"attr" => "new"},
62
- :changes => {})
63
- @new_model = mock('new_model', :attributes => {"attr" => "new"},
64
- :changed? => false)
65
-
66
- @controller.expects(:instance_variable_get).once.returns(@new_model)
67
-
68
- @filter.after(@controller)
69
- assert_not_nil @proc_result
70
- end
71
- end
72
- end
73
- end
@@ -1,36 +0,0 @@
1
- require 'track_changes'
2
-
3
- class PostsController < ApplicationController
4
- include TrackChanges
5
- before_filter :find_post
6
- track_changes :post, :only => :update
7
-
8
- def show
9
- render :text => @post.inspect
10
- end
11
-
12
- # PUT /posts/1
13
- # PUT /posts/1.xml
14
- def update
15
- respond_to do |format|
16
- if @post.update_attributes(params[:post])
17
- flash[:notice] = 'Post was successfully updated.'
18
- format.html { redirect_to(@post) }
19
- format.xml { head :ok }
20
- else
21
- format.html { render :action => "edit" }
22
- format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
23
- end
24
- end
25
- end
26
-
27
- protected
28
-
29
- def current_user
30
- "John Smith"
31
- end
32
-
33
- def find_post
34
- @post = Post.find(params[:id])
35
- end
36
- end
@@ -1,4 +0,0 @@
1
- class Audit < ActiveRecord::Base
2
- belongs_to :audited, :polymorphic => true
3
- serialize :change_set
4
- end
@@ -1,3 +0,0 @@
1
- class Post < ActiveRecord::Base
2
- has_many :audits, :as => :audited
3
- end
@@ -1,110 +0,0 @@
1
- # Don't change this file!
2
- # Configure your app in config/environment.rb and config/environments/*.rb
3
-
4
- RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
5
-
6
- module Rails
7
- class << self
8
- def boot!
9
- unless booted?
10
- preinitialize
11
- pick_boot.run
12
- end
13
- end
14
-
15
- def booted?
16
- defined? Rails::Initializer
17
- end
18
-
19
- def pick_boot
20
- (vendor_rails? ? VendorBoot : GemBoot).new
21
- end
22
-
23
- def vendor_rails?
24
- File.exist?("#{RAILS_ROOT}/vendor/rails")
25
- end
26
-
27
- def preinitialize
28
- load(preinitializer_path) if File.exist?(preinitializer_path)
29
- end
30
-
31
- def preinitializer_path
32
- "#{RAILS_ROOT}/config/preinitializer.rb"
33
- end
34
- end
35
-
36
- class Boot
37
- def run
38
- load_initializer
39
- Rails::Initializer.run(:set_load_path)
40
- end
41
- end
42
-
43
- class VendorBoot < Boot
44
- def load_initializer
45
- require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
46
- Rails::Initializer.run(:install_gem_spec_stubs)
47
- Rails::GemDependency.add_frozen_gem_path
48
- end
49
- end
50
-
51
- class GemBoot < Boot
52
- def load_initializer
53
- self.class.load_rubygems
54
- load_rails_gem
55
- require 'initializer'
56
- end
57
-
58
- def load_rails_gem
59
- if version = self.class.gem_version
60
- gem 'rails', version
61
- else
62
- gem 'rails'
63
- end
64
- rescue Gem::LoadError => load_error
65
- $stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
66
- exit 1
67
- end
68
-
69
- class << self
70
- def rubygems_version
71
- Gem::RubyGemsVersion rescue nil
72
- end
73
-
74
- def gem_version
75
- if defined? RAILS_GEM_VERSION
76
- RAILS_GEM_VERSION
77
- elsif ENV.include?('RAILS_GEM_VERSION')
78
- ENV['RAILS_GEM_VERSION']
79
- else
80
- parse_gem_version(read_environment_rb)
81
- end
82
- end
83
-
84
- def load_rubygems
85
- min_version = '1.3.2'
86
- require 'rubygems'
87
- unless rubygems_version >= min_version
88
- $stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.)
89
- exit 1
90
- end
91
-
92
- rescue LoadError
93
- $stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org)
94
- exit 1
95
- end
96
-
97
- def parse_gem_version(text)
98
- $1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
99
- end
100
-
101
- private
102
- def read_environment_rb
103
- File.read("#{RAILS_ROOT}/config/environment.rb")
104
- end
105
- end
106
- end
107
- end
108
-
109
- # All that for this:
110
- Rails.boot!
@@ -1,3 +0,0 @@
1
- test:
2
- :adapter: sqlite3
3
- :database: ":memory:"
@@ -1,7 +0,0 @@
1
- RAILS_GEM_VERSION = '2.3.5' unless defined? RAILS_GEM_VERSION
2
-
3
- require File.join(File.dirname(__FILE__), 'boot')
4
-
5
- Rails::Initializer.run do |config|
6
- config.time_zone = 'UTC'
7
- end
@@ -1,7 +0,0 @@
1
- config.cache_classes = true
2
- config.whiny_nils = true
3
- config.action_controller.consider_all_requests_local = true
4
- config.action_controller.perform_caching = false
5
- config.action_view.cache_template_loading = true
6
- config.action_controller.allow_forgery_protection = false
7
- config.action_mailer.delivery_method = :test
@@ -1,7 +0,0 @@
1
- if defined?(ActiveRecord)
2
- ActiveRecord::Base.include_root_in_json = true
3
- ActiveRecord::Base.store_full_sti_class = true
4
- end
5
- ActionController::Routing.generate_best_match = false
6
- ActiveSupport.use_standard_json_time_format = true
7
- ActiveSupport.escape_html_entities_in_json = false
@@ -1,4 +0,0 @@
1
- ActionController::Base.session = {
2
- :key => '_rails_root_session',
3
- :secret => '54bf4d986f9e5cf80f28c31e03278d3689cb30e84aceb32893bfa617ad7a0fe0bfc4e7012f6b6e1bb9787f2c02cdff1fef4bc928f3ad060d5d6687dd2f813a54'
4
- }
@@ -1,8 +0,0 @@
1
- require 'track_changes'
2
-
3
- TrackChanges::Initializer.instance do |config|
4
- config.audit_association = :audits
5
- config.current_user = :current_user
6
- config.ignore_nil = true
7
- end
8
-
@@ -1,3 +0,0 @@
1
- ActionController::Routing::Routes.draw do |map|
2
- map.resources :posts
3
- end
@@ -1,16 +0,0 @@
1
- class CreateAudits < ActiveRecord::Migration
2
- def self.up
3
- create_table :audits do |t|
4
- t.string :audited_type
5
- t.integer :audited_id
6
- t.string :user
7
- t.text :change_set
8
-
9
- t.timestamps
10
- end
11
- end
12
-
13
- def self.down
14
- drop_table :audits
15
- end
16
- end
@@ -1,15 +0,0 @@
1
- class CreatePosts < ActiveRecord::Migration
2
- def self.up
3
- create_table :posts do |t|
4
- t.string :title
5
- t.text :body
6
- t.string :author
7
-
8
- t.timestamps
9
- end
10
- end
11
-
12
- def self.down
13
- drop_table :posts
14
- end
15
- end