audited-activerecord 3.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2 @@
1
+ require 'audited'
2
+ require 'audited/adapters/active_record'
@@ -0,0 +1,15 @@
1
+ require 'active_record'
2
+ require 'audited/auditor'
3
+ require 'audited/adapters/active_record/audit'
4
+
5
+ module Audited::Auditor::ClassMethods
6
+ def default_ignored_attributes
7
+ [self.primary_key, inheritance_column]
8
+ end
9
+ end
10
+
11
+ ::ActiveRecord::Base.send :include, Audited::Auditor
12
+
13
+ Audited.audit_class = Audited::Adapters::ActiveRecord::Audit
14
+
15
+ require 'audited/sweeper'
@@ -0,0 +1,69 @@
1
+ require 'set'
2
+ require 'audited/audit'
3
+
4
+ module Audited
5
+ module Adapters
6
+ module ActiveRecord
7
+ # Audit saves the changes to ActiveRecord models. It has the following attributes:
8
+ #
9
+ # * <tt>auditable</tt>: the ActiveRecord model that was changed
10
+ # * <tt>user</tt>: the user that performed the change; a string or an ActiveRecord model
11
+ # * <tt>action</tt>: one of create, update, or delete
12
+ # * <tt>audited_changes</tt>: a serialized hash of all the changes
13
+ # * <tt>comment</tt>: a comment set with the audit
14
+ # * <tt>created_at</tt>: Time that the change was performed
15
+ #
16
+ class Audit < ::ActiveRecord::Base
17
+ include Audited::Audit
18
+
19
+
20
+ serialize :audited_changes
21
+
22
+ default_scope order(:version)
23
+ scope :descending, reorder("version DESC")
24
+ scope :creates, :conditions => {:action => 'create'}
25
+ scope :updates, :conditions => {:action => 'update'}
26
+ scope :destroys, :conditions => {:action => 'destroy'}
27
+
28
+ scope :up_until, lambda {|date_or_time| where("created_at <= ?", date_or_time) }
29
+ scope :from_version, lambda {|version| where(['version >= ?', version]) }
30
+ scope :to_version, lambda {|version| where(['version <= ?', version]) }
31
+
32
+ # Return all audits older than the current one.
33
+ def ancestors
34
+ self.class.where(['auditable_id = ? and auditable_type = ? and version <= ?',
35
+ auditable_id, auditable_type, version])
36
+ end
37
+
38
+ # Allows user to be set to either a string or an ActiveRecord object
39
+ # @private
40
+ def user_as_string=(user)
41
+ # reset both either way
42
+ self.user_as_model = self.username = nil
43
+ user.is_a?(::ActiveRecord::Base) ?
44
+ self.user_as_model = user :
45
+ self.username = user
46
+ end
47
+ alias_method :user_as_model=, :user=
48
+ alias_method :user=, :user_as_string=
49
+
50
+ # @private
51
+ def user_as_string
52
+ self.user_as_model || self.username
53
+ end
54
+ alias_method :user_as_model, :user
55
+ alias_method :user, :user_as_string
56
+
57
+ private
58
+ def set_version_number
59
+ max = self.class.maximum(:version,
60
+ :conditions => {
61
+ :auditable_id => auditable_id,
62
+ :auditable_type => auditable_type
63
+ }) || 0
64
+ self.version = max + 1
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,28 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+ require 'active_record'
4
+ require 'rails/generators/active_record'
5
+
6
+ module Audited
7
+ module Generators
8
+ class InstallGenerator < Rails::Generators::Base
9
+ include Rails::Generators::Migration
10
+
11
+ source_root File.expand_path("../templates", __FILE__)
12
+
13
+ # Implement the required interface for Rails::Generators::Migration.
14
+ def self.next_migration_number(dirname) #:nodoc:
15
+ next_migration_number = current_migration_number(dirname) + 1
16
+ if ActiveRecord::Base.timestamped_migrations
17
+ [Time.now.utc.strftime("%Y%m%d%H%M%S"), "%.14d" % next_migration_number].max
18
+ else
19
+ "%.3d" % next_migration_number
20
+ end
21
+ end
22
+
23
+ def copy_migration
24
+ migration_template 'install.rb', 'db/migrate/install_audited.rb'
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,11 @@
1
+ class <%= migration_class_name %> < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :audits, :association_id, :integer
4
+ add_column :audits, :association_type, :string
5
+ end
6
+
7
+ def self.down
8
+ remove_column :audits, :association_type
9
+ remove_column :audits, :association_id
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ class <%= migration_class_name %> < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :audits, :comment, :string
4
+ end
5
+
6
+ def self.down
7
+ remove_column :audits, :comment
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ class <%= migration_class_name %> < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :audits, :remote_address, :string
4
+ end
5
+
6
+ def self.down
7
+ remove_column :audits, :remote_address
8
+ end
9
+ end
10
+
@@ -0,0 +1,28 @@
1
+ class <%= migration_class_name %> < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :audits, :force => true do |t|
4
+ t.column :auditable_id, :integer
5
+ t.column :auditable_type, :string
6
+ t.column :associated_id, :integer
7
+ t.column :associated_type, :string
8
+ t.column :user_id, :integer
9
+ t.column :user_type, :string
10
+ t.column :username, :string
11
+ t.column :action, :string
12
+ t.column :audited_changes, :text
13
+ t.column :version, :integer, :default => 0
14
+ t.column :comment, :string
15
+ t.column :remote_address, :string
16
+ t.column :created_at, :datetime
17
+ end
18
+
19
+ add_index :audits, [:auditable_id, :auditable_type], :name => 'auditable_index'
20
+ add_index :audits, [:associated_id, :associated_type], :name => 'associated_index'
21
+ add_index :audits, [:user_id, :user_type], :name => 'user_index'
22
+ add_index :audits, :created_at
23
+ end
24
+
25
+ def self.down
26
+ drop_table :audits
27
+ end
28
+ end
@@ -0,0 +1,23 @@
1
+ class <%= migration_class_name %> < ActiveRecord::Migration
2
+ def self.up
3
+ if index_exists? :audits, [:association_id, :association_type], :name => 'association_index'
4
+ remove_index :audits, :name => 'association_index'
5
+ end
6
+
7
+ rename_column :audits, :association_id, :associated_id
8
+ rename_column :audits, :association_type, :associated_type
9
+
10
+ add_index :audits, [:associated_id, :associated_type], :name => 'associated_index'
11
+ end
12
+
13
+ def self.down
14
+ if index_exists? :audits, [:associated_id, :associated_type], :name => 'associated_index'
15
+ remove_index :audits, :name => 'associated_index'
16
+ end
17
+
18
+ rename_column :audits, :associated_type, :association_type
19
+ rename_column :audits, :associated_id, :association_id
20
+
21
+ add_index :audits, [:association_id, :association_type], :name => 'association_index'
22
+ end
23
+ end
@@ -0,0 +1,9 @@
1
+ class <%= migration_class_name %> < ActiveRecord::Migration
2
+ def self.up
3
+ rename_column :audits, :changes, :audited_changes
4
+ end
5
+
6
+ def self.down
7
+ rename_column :audits, :audited_changes, :changes
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ class <%= migration_class_name %> < ActiveRecord::Migration
2
+ def self.up
3
+ rename_column :audits, :auditable_parent_id, :association_id
4
+ rename_column :audits, :auditable_parent_type, :association_type
5
+ end
6
+
7
+ def self.down
8
+ rename_column :audits, :association_type, :auditable_parent_type
9
+ rename_column :audits, :association_id, :auditable_parent_id
10
+ end
11
+ end
@@ -0,0 +1,63 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+ require 'active_record'
4
+ require 'rails/generators/active_record'
5
+
6
+ module Audited
7
+ module Generators
8
+ class UpgradeGenerator < Rails::Generators::Base
9
+ include Rails::Generators::Migration
10
+
11
+ source_root File.expand_path("../templates", __FILE__)
12
+
13
+ # Implement the required interface for Rails::Generators::Migration.
14
+ def self.next_migration_number(dirname) #:nodoc:
15
+ next_migration_number = current_migration_number(dirname) + 1
16
+ if ActiveRecord::Base.timestamped_migrations
17
+ [Time.now.utc.strftime("%Y%m%d%H%M%S"), "%.14d" % next_migration_number].max
18
+ else
19
+ "%.3d" % next_migration_number
20
+ end
21
+ end
22
+
23
+ def copy_templates
24
+ migrations_to_be_applied do |m|
25
+ migration_template "#{m}.rb", "db/migrate/#{m}.rb"
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def migrations_to_be_applied
32
+ Audited::Adapters::ActiveRecord::Audit.reset_column_information
33
+ columns = Audited::Adapters::ActiveRecord::Audit.columns.map(&:name)
34
+
35
+ unless columns.include?( 'comment' )
36
+ yield :add_comment_to_audits
37
+ end
38
+
39
+ if columns.include?( 'changes' )
40
+ yield :rename_changes_to_audited_changes
41
+ end
42
+
43
+ unless columns.include?( 'remote_address' )
44
+ yield :add_remote_address_to_audits
45
+ end
46
+
47
+ unless columns.include?( 'association_id' )
48
+ if columns.include?('auditable_parent_id')
49
+ yield :rename_parent_to_association
50
+ else
51
+ unless columns.include?( 'associated_id' )
52
+ yield :add_association_to_audits
53
+ end
54
+ end
55
+ end
56
+
57
+ if columns.include?( 'association_id' )
58
+ yield :rename_association_to_associated
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: audited-activerecord
3
+ version: !ruby/object:Gem::Version
4
+ version: 3.0.0.rc1
5
+ prerelease: 6
6
+ platform: ruby
7
+ authors:
8
+ - Brandon Keepers
9
+ - Kenneth Kalmer
10
+ - Daniel Morrison
11
+ - Brian Ryckbost
12
+ - Steve Richert
13
+ - Ryan Glover
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+ date: 2012-04-25 00:00:00.000000000 Z
18
+ dependencies:
19
+ - !ruby/object:Gem::Dependency
20
+ name: audited
21
+ requirement: !ruby/object:Gem::Requirement
22
+ none: false
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.0.rc1
27
+ prerelease: 6
28
+ type: :runtime
29
+ prerelease: false
30
+ version_requirements: !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - '='
34
+ - !ruby/object:Gem::Version
35
+ version: 3.0.0.rc1
36
+ prerelease: 6
37
+ - !ruby/object:Gem::Dependency
38
+ name: activerecord
39
+ requirement: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: '3.0'
45
+ type: :runtime
46
+ prerelease: false
47
+ version_requirements: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ~>
51
+ - !ruby/object:Gem::Version
52
+ version: '3.0'
53
+ description: Log all changes to your ActiveRecord models
54
+ email: info@collectiveidea.com
55
+ executables: []
56
+ extensions: []
57
+ extra_rdoc_files: []
58
+ files:
59
+ - lib/audited-activerecord.rb
60
+ - lib/audited/adapters/active_record.rb
61
+ - lib/audited/adapters/active_record/audit.rb
62
+ - lib/generators/audited/install_generator.rb
63
+ - lib/generators/audited/templates/add_association_to_audits.rb
64
+ - lib/generators/audited/templates/add_comment_to_audits.rb
65
+ - lib/generators/audited/templates/add_remote_address_to_audits.rb
66
+ - lib/generators/audited/templates/install.rb
67
+ - lib/generators/audited/templates/rename_association_to_associated.rb
68
+ - lib/generators/audited/templates/rename_changes_to_audited_changes.rb
69
+ - lib/generators/audited/templates/rename_parent_to_association.rb
70
+ - lib/generators/audited/upgrade_generator.rb
71
+ homepage: https://github.com/collectiveidea/audited
72
+ licenses: []
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ! '>'
87
+ - !ruby/object:Gem::Version
88
+ version: 1.3.1
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 1.8.21
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: Log all changes to your ActiveRecord models
95
+ test_files: []