Permalink
Browse files

Convert to Rails 3 idioms.

  • Loading branch information...
1 parent 0d06d81 commit 36a4b9b3231717281aa2eb4faa40b15db648545f @airblade committed Feb 8, 2011
Showing with 9,989 additions and 282 deletions.
  1. +56 −7 Gemfile.lock
  2. +1 −1 Rakefile
  3. +9 −4 lib/paper_trail.rb
  4. +0 −2 lib/paper_trail/controller.rb
  5. +2 −3 lib/paper_trail/has_paper_trail.rb
  6. +17 −24 paper_trail.gemspec
  7. +7 −0 test/dummy/Rakefile
  8. +16 −0 test/dummy/app/controllers/application_controller.rb
  9. +5 −0 test/dummy/app/controllers/test_controller.rb
  10. +18 −0 test/dummy/app/controllers/widgets_controller.rb
  11. +2 −0 test/dummy/app/helpers/application_helper.rb
  12. +11 −0 test/dummy/app/models/article.rb
  13. +5 −0 test/dummy/app/models/authorship.rb
  14. +5 −0 test/dummy/app/models/book.rb
  15. +3 −0 test/dummy/app/models/fluxor.rb
  16. +2 −0 test/dummy/app/models/foo_widget.rb
  17. +5 −0 test/dummy/app/models/person.rb
  18. +12 −0 test/dummy/app/models/song.rb
  19. +5 −0 test/dummy/app/models/widget.rb
  20. +4 −0 test/dummy/app/models/wotsit.rb
  21. +14 −0 test/dummy/app/views/layouts/application.html.erb
  22. +4 −0 test/dummy/config.ru
  23. +45 −0 test/dummy/config/application.rb
  24. +10 −0 test/dummy/config/boot.rb
  25. +22 −0 test/dummy/config/database.yml
  26. +5 −0 test/dummy/config/environment.rb
  27. +26 −0 test/dummy/config/environments/development.rb
  28. +49 −0 test/dummy/config/environments/production.rb
  29. +35 −0 test/dummy/config/environments/test.rb
  30. +7 −0 test/dummy/config/initializers/backtrace_silencers.rb
  31. +10 −0 test/dummy/config/initializers/inflections.rb
  32. +5 −0 test/dummy/config/initializers/mime_types.rb
  33. +7 −0 test/dummy/config/initializers/secret_token.rb
  34. +8 −0 test/dummy/config/initializers/session_store.rb
  35. +5 −0 test/dummy/config/locales/en.yml
  36. +3 −0 test/dummy/config/routes.rb
  37. BIN test/dummy/db/development.sqlite3
  38. +84 −0 test/dummy/db/migrate/20110208155312_set_up_test_tables.rb
  39. +82 −0 test/dummy/db/schema.rb
  40. BIN test/dummy/db/test.sqlite3
  41. +26 −0 test/dummy/public/404.html
  42. +26 −0 test/dummy/public/422.html
  43. +26 −0 test/dummy/public/500.html
  44. 0 test/dummy/public/favicon.ico
  45. +2 −0 test/dummy/public/javascripts/application.js
  46. +965 −0 test/dummy/public/javascripts/controls.js
  47. +974 −0 test/dummy/public/javascripts/dragdrop.js
  48. +1,123 −0 test/dummy/public/javascripts/effects.js
  49. +6,001 −0 test/dummy/public/javascripts/prototype.js
  50. +175 −0 test/dummy/public/javascripts/rails.js
  51. 0 test/dummy/public/stylesheets/.gitkeep
  52. +6 −0 test/dummy/script/rails
  53. +7 −44 test/{paper_trail_controller_test.rb → functional/controller_test.rb}
  54. +1 −7 test/{thread_safe_test.rb → functional/thread_safety_test.rb}
  55. +7 −0 test/integration/navigation_test.rb
  56. +0 −15 test/paper_trail_schema_test.rb
  57. +7 −0 test/paper_trail_test.rb
  58. +0 −71 test/schema.rb
  59. +0 −3 test/schema_change.rb
  60. +5 −0 test/support/integration_case.rb
  61. +27 −33 test/test_helper.rb
  62. +5 −68 test/{paper_trail_model_test.rb → unit/model_test.rb}
View
@@ -2,13 +2,15 @@ PATH
remote: .
specs:
paper_trail (2.0.0)
- actionpack (~> 3)
- activerecord (~> 3)
+ rails (~> 3)
GEM
remote: http://rubygems.org/
specs:
abstract (1.0.0)
+ actionmailer (3.0.3)
+ actionpack (= 3.0.3)
+ mail (~> 2.2.9)
actionpack (3.0.3)
activemodel (= 3.0.3)
activesupport (= 3.0.3)
@@ -28,31 +30,78 @@ GEM
activesupport (= 3.0.3)
arel (~> 2.0.2)
tzinfo (~> 0.3.23)
+ activeresource (3.0.3)
+ activemodel (= 3.0.3)
+ activesupport (= 3.0.3)
activesupport (3.0.3)
arel (2.0.7)
builder (2.1.2)
+ capybara (0.4.1.1)
+ celerity (>= 0.7.9)
+ culerity (>= 0.2.4)
+ mime-types (>= 1.16)
+ nokogiri (>= 1.3.3)
+ rack (>= 1.0.0)
+ rack-test (>= 0.5.4)
+ selenium-webdriver (>= 0.0.27)
+ xpath (~> 0.1.3)
+ celerity (0.8.7)
+ childprocess (0.1.6)
+ ffi (~> 0.6.3)
+ culerity (0.2.15)
erubis (2.6.6)
abstract (>= 1.0.0)
+ ffi (0.6.3)
+ rake (>= 0.8.7)
i18n (0.5.0)
+ json_pure (1.5.1)
+ mail (2.2.15)
+ activesupport (>= 2.3.6)
+ i18n (>= 0.4.0)
+ mime-types (~> 1.16)
+ treetop (~> 1.4.8)
+ mime-types (1.16)
+ nokogiri (1.4.4)
+ polyglot (0.3.1)
rack (1.2.1)
rack-mount (0.6.13)
rack (>= 1.0.0)
rack-test (0.5.7)
rack (>= 1.0)
+ rails (3.0.3)
+ actionmailer (= 3.0.3)
+ actionpack (= 3.0.3)
+ activerecord (= 3.0.3)
+ activeresource (= 3.0.3)
+ activesupport (= 3.0.3)
+ bundler (~> 1.0)
+ railties (= 3.0.3)
+ railties (3.0.3)
+ actionpack (= 3.0.3)
+ activesupport (= 3.0.3)
+ rake (>= 0.8.7)
+ thor (~> 0.14.4)
rake (0.8.7)
+ rubyzip (0.9.4)
+ selenium-webdriver (0.1.2)
+ childprocess (~> 0.1.5)
+ ffi (~> 0.6.3)
+ json_pure
+ rubyzip
shoulda (2.10.3)
sqlite3-ruby (1.3.1)
+ thor (0.14.6)
+ treetop (1.4.9)
+ polyglot (>= 0.3.1)
tzinfo (0.3.24)
+ xpath (0.1.3)
+ nokogiri (~> 1.3)
PLATFORMS
ruby
DEPENDENCIES
- actionpack (~> 3)
- activerecord (~> 3)
- activesupport (~> 3)
- bundler (~> 1.0)
+ capybara (>= 0.4.0)
paper_trail!
- rake (= 0.8.7)
shoulda (= 2.10.3)
sqlite3-ruby (~> 1.2)
View
@@ -8,7 +8,7 @@ Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.libs << 'test'
t.pattern = 'test/**/*_test.rb'
- t.verbose = true
+ t.verbose = false
end
desc 'Default: run unit tests.'
View
@@ -1,15 +1,11 @@
require 'singleton'
require 'yaml'
-require 'action_controller'
-require 'active_record'
-
require 'paper_trail/config'
require 'paper_trail/controller'
require 'paper_trail/has_paper_trail'
require 'paper_trail/version'
-
# PaperTrail's module methods can be called in both models and controllers.
module PaperTrail
@@ -66,3 +62,12 @@ def self.config
end
end
+
+
+ActiveSupport.on_load(:active_record) do
+ include PaperTrail::Model
+end
+
+ActiveSupport.on_load(:action_controller) do
+ include PaperTrail::Controller
+end
@@ -59,5 +59,3 @@ def set_paper_trail_controller_info
end
end
-
-ActionController::Base.send :include, PaperTrail::Controller
@@ -64,7 +64,7 @@ def live?
# Returns who put the object into its current state.
def originator
- versions.last.try :whodunnit
+ Version.with_item_keys(self.class.name, id).last.try :whodunnit
end
# Returns the object (not a Version) as it was at the given timestamp.
@@ -112,6 +112,7 @@ def record_destroy
:object => object_to_string(item_before_change),
:whodunnit => PaperTrail.whodunnit)
end
+ versions.send :load_target
end
def merge_metadata(data)
@@ -158,5 +159,3 @@ def switched_on?
end
end
-
-ActiveRecord::Base.send :include, PaperTrail::Model
View
@@ -1,30 +1,23 @@
-$LOAD_PATH.unshift 'lib'
+$LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
require 'paper_trail/version_number'
Gem::Specification.new do |s|
- s.name = 'paper_trail'
- s.version = PaperTrail::VERSION
- s.summary = "Track changes to your models' data. Good for auditing or versioning."
- s.description = s.summary
- s.homepage = 'http://github.com/airblade/paper_trail'
- s.authors = ['Andy Stewart']
- s.email = 'boss@airbladesoftware.com'
- s.files = %w[ README.md Rakefile paper_trail.gemspec ]
- s.files += %w[ init.rb install.rb ]
- s.files += Dir.glob("lib/**/*")
- s.files += Dir.glob("generators/**/*")
- s.require_path = 'lib'
- s.test_files = Dir.glob("test/**/*")
+ s.name = 'paper_trail'
+ s.version = PaperTrail::VERSION
+ s.summary = "Track changes to your models' data. Good for auditing or versioning."
+ s.description = s.summary
+ s.homepage = 'http://github.com/airblade/paper_trail'
+ s.authors = ['Andy Stewart']
+ s.email = 'boss@airbladesoftware.com'
- s.add_development_dependency 'bundler', '~> 1.0'
- s.add_development_dependency 'rake', '0.8.7' # TODO: why do we need to list rake?
- s.add_development_dependency 'shoulda', '2.10.3'
- s.add_development_dependency 'activesupport', '~> 3'
- s.add_development_dependency 'sqlite3-ruby', '~> 1.2'
- # Repeated here to make bundler happy
- s.add_development_dependency 'activerecord', '~> 3'
- s.add_development_dependency 'actionpack', '~> 3'
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ['lib']
- s.add_dependency 'activerecord', '~> 3'
- s.add_dependency 'actionpack', '~> 3'
+ s.add_dependency 'rails', '~> 3'
+
+ s.add_development_dependency 'shoulda', '2.10.3'
+ s.add_development_dependency 'sqlite3-ruby', '~> 1.2'
+ s.add_development_dependency 'capybara', '>= 0.4.0'
end
View
@@ -0,0 +1,7 @@
+# Add your own tasks in files placed in lib/tasks ending in .rake,
+# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
+
+require File.expand_path('../config/application', __FILE__)
+require 'rake'
+
+Dummy::Application.load_tasks
@@ -0,0 +1,16 @@
+class ApplicationController < ActionController::Base
+ protect_from_forgery
+
+ def rescue_action(e)
+ raise e
+ end
+
+ # Returns id of hypothetical current user
+ def current_user
+ 153
+ end
+
+ def info_for_paper_trail
+ {:ip => request.remote_ip, :user_agent => request.user_agent}
+ end
+end
@@ -0,0 +1,5 @@
+class TestController < ActionController::Base
+ def current_user
+ Thread.current.object_id
+ end
+end
@@ -0,0 +1,18 @@
+class WidgetsController < ApplicationController
+ def create
+ @widget = Widget.create params[:widget]
+ head :ok
+ end
+
+ def update
+ @widget = Widget.find params[:id]
+ @widget.update_attributes params[:widget]
+ head :ok
+ end
+
+ def destroy
+ @widget = Widget.find params[:id]
+ @widget.destroy
+ head :ok
+ end
+end
@@ -0,0 +1,2 @@
+module ApplicationHelper
+end
@@ -0,0 +1,11 @@
+class Article < ActiveRecord::Base
+ has_paper_trail :ignore => [:title],
+ :meta => {:answer => 42,
+ :action => :action_data_provider_method,
+ :question => Proc.new { "31 + 11 = #{31 + 11}" },
+ :article_id => Proc.new { |article| article.id } }
+
+ def action_data_provider_method
+ self.object_id.to_s
+ end
+end
@@ -0,0 +1,5 @@
+class Authorship < ActiveRecord::Base
+ belongs_to :book
+ belongs_to :person
+ has_paper_trail
+end
@@ -0,0 +1,5 @@
+class Book < ActiveRecord::Base
+ has_many :authorships, :dependent => :destroy
+ has_many :authors, :through => :authorships, :source => :person
+ has_paper_trail
+end
@@ -0,0 +1,3 @@
+class Fluxor < ActiveRecord::Base
+ belongs_to :widget
+end
@@ -0,0 +1,2 @@
+class FooWidget < Widget
+end
@@ -0,0 +1,5 @@
+class Person < ActiveRecord::Base
+ has_many :authorships, :dependent => :destroy
+ has_many :books, :through => :authorships
+ has_paper_trail
+end
@@ -0,0 +1,12 @@
+# Example from 'Overwriting default accessors' in ActiveRecord::Base.
+class Song < ActiveRecord::Base
+ has_paper_trail
+
+ # Uses an integer of seconds to hold the length of the song
+ def length=(minutes)
+ write_attribute(:length, minutes.to_i * 60)
+ end
+ def length
+ read_attribute(:length) / 60
+ end
+end
@@ -0,0 +1,5 @@
+class Widget < ActiveRecord::Base
+ has_paper_trail
+ has_one :wotsit
+ has_many :fluxors, :order => :name
+end
@@ -0,0 +1,4 @@
+class Wotsit < ActiveRecord::Base
+ has_paper_trail
+ belongs_to :widget
+end
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <title>Dummy</title>
+ <%= stylesheet_link_tag :all %>
+ <%= javascript_include_tag :defaults %>
+ <%= csrf_meta_tag %>
+</head>
+<body>
+
+<%= yield %>
+
+</body>
+</html>
@@ -0,0 +1,4 @@
+# This file is used by Rack-based servers to start the application.
+
+require ::File.expand_path('../config/environment', __FILE__)
+run Dummy::Application
@@ -0,0 +1,45 @@
+require File.expand_path('../boot', __FILE__)
+
+require "active_model/railtie"
+require "active_record/railtie"
+require "action_controller/railtie"
+require "action_view/railtie"
+require "action_mailer/railtie"
+
+Bundler.require
+require 'paper_trail'
+
+module Dummy
+ class Application < Rails::Application
+ # Settings in config/environments/* take precedence over those specified here.
+ # Application configuration should go into files in config/initializers
+ # -- all .rb files in that directory are automatically loaded.
+
+ # Custom directories with classes and modules you want to be autoloadable.
+ # config.autoload_paths += %W(#{config.root}/extras)
+
+ # Only load the plugins named here, in the order given (default is alphabetical).
+ # :all can be used as a placeholder for all plugins not explicitly named.
+ # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
+
+ # Activate observers that should always be running.
+ # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
+
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
+ # config.time_zone = 'Central Time (US & Canada)'
+
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
+ # config.i18n.default_locale = :de
+
+ # JavaScript files you want as :defaults (application.js is always included).
+ # config.action_view.javascript_expansions[:defaults] = %w(jquery rails)
+
+ # Configure the default encoding used in templates for Ruby 1.9.
+ config.encoding = "utf-8"
+
+ # Configure sensitive parameters which will be filtered from the log file.
+ config.filter_parameters += [:password]
+ end
+end
Oops, something went wrong. Retry.

2 comments on commit 36a4b9b

@emmanuel

Wow, awesome! Thanks for integrating those ideas, and so quickly!

BTW, I switched from another auditing plugin to paper_trail because the implementation is so nice and simple. It's readable, clear and extendible (and it reminds me of git (store the complete state at every version/revision)). Thanks for Paper Trail.

Off to go drop my fork...

@airblade
Owner

Thank you!

Please sign in to comment.