simple_audit 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 [name of plugin creator]
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.markdown ADDED
@@ -0,0 +1,18 @@
1
+ # SimpleAudit
2
+
3
+
4
+ Simple auditing solution for ActiveRecord models.
5
+
6
+
7
+ # Installation
8
+
9
+ ## As a plugin
10
+ ./script/plugin install http://github.com/gtarnovan/simple_audit
11
+
12
+ # Example
13
+
14
+
15
+ Example goes here.
16
+
17
+
18
+ Copyright (c) 2010 [name of plugin creator], released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the simple_audit plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for the simple_audit plugin.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'SimpleAudit'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
24
+
25
+ #
26
+ # Gemify
27
+ #
28
+ begin
29
+ require 'jeweler'
30
+
31
+ gem_files = FileList[
32
+ '[a-zA-Z]*',
33
+ 'lib/**/*',
34
+ 'rails/**/*',
35
+ 'tasks/**/*',
36
+ 'test/**/*'
37
+ ]
38
+
39
+ Jeweler::Tasks.new do |gemspec|
40
+ gemspec.name = "simple_audit"
41
+ gemspec.summary = "Simple auditing solution for ActiveRecord models"
42
+ #gemspec.description = ""
43
+ gemspec.email = "gabriel.tarnovan@cubus.ro"
44
+ gemspec.homepage = "http://github.com/gtarnovan/simple_audit"
45
+ gemspec.authors = ["Gabriel Tarnovan"]
46
+ gemspec.version = "0.0.1"
47
+ gemspec.files = gem_files.to_a
48
+
49
+ gemspec.rubyforge_project = 'simple_audit'
50
+ end
51
+ rescue LoadError
52
+ puts "Jeweler not available. Install it with: gem install jeweler"
53
+ end
@@ -0,0 +1,32 @@
1
+ module SimpleAuditHelper
2
+
3
+ def render_audits(audits)
4
+ audits = audits.dup.sort{|a, b| b.created_at <=> a.created_at}
5
+ res = ''
6
+ audits.each_with_index do |audit, index|
7
+ older_audit = audits[index + 1]
8
+ res += content_tag(:div, :class => 'audit') do
9
+ content_tag(:div, audit.action, :class => "action #{audit.action}") +
10
+ content_tag(:div, audit.username, :class => "user") +
11
+ content_tag(:div, l(audit.created_at), :class => "timestamp") +
12
+ content_tag(:div, :class => 'changes') do
13
+ if older_audit.present?
14
+ audit.delta(older_audit).collect do |k, v|
15
+ "\n" +
16
+ Booking.human_attribute_name(k) +
17
+ ":" +
18
+ content_tag(:span, v.last, :class => 'current') +
19
+ content_tag(:span, v.first, :class => 'previous')
20
+ end
21
+ else
22
+ audit.changes.reject{|k, v| v.blank?}.collect {|k, v| "\n#{Booking.human_attribute_name(k)}: #{v}"}
23
+ end
24
+ end
25
+ end
26
+ end
27
+ res
28
+ end
29
+
30
+ end
31
+
32
+
@@ -0,0 +1,39 @@
1
+ class Audit < ActiveRecord::Base
2
+ belongs_to :auditable, :polymorphic => true
3
+ belongs_to :user, :polymorphic => true
4
+ serialize :changes
5
+
6
+ #
7
+ # returns the differences of the to audits
8
+ # result is a hash containing arrays of the form [<value_in_other_audit>, <value_in_this_audit>]
9
+ # the values in this audit take precedence
10
+ #
11
+ def delta(other)
12
+
13
+ return self.changes if other.nil?
14
+
15
+ d = {}
16
+
17
+ (self.changes.keys - other.changes.keys).each do |k|
18
+ d[k] = [nil, self.changes[k]]
19
+ end
20
+
21
+ (other.changes.keys - self.changes.keys).each do |k|
22
+ d[k] = [other.changes[k], nil]
23
+ end
24
+
25
+ self.changes.keys.each do |k|
26
+ if self.changes[k] != other.changes[k]
27
+ d[k] = [other.changes[k], self.changes[k]]
28
+ end
29
+ end
30
+
31
+ d
32
+
33
+ end
34
+
35
+ def changes
36
+ self[:changes]
37
+ end
38
+
39
+ end
@@ -0,0 +1,48 @@
1
+ module Cubus
2
+ module SimpleAudit
3
+
4
+ def self.included(base)
5
+ base.send :extend, ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+
10
+ # create a new Audit record
11
+ # if no user is specified, try to use sentient_user's User.current
12
+
13
+ def simple_audit(options = {})
14
+ class_eval do
15
+
16
+ cattr_accessor :username_method
17
+ self.username_method = (options[:username_method] || :name).to_s
18
+
19
+ has_many :audits, :as => :auditable
20
+ after_create {|record| audit(record, :create)}
21
+ after_update {|record| audit(record, :update)}
22
+
23
+
24
+ end
25
+ send :include, InstanceMethods
26
+ end
27
+
28
+ def audit record, action = :update, user = nil
29
+ user ||= User.current if User.respond_to?(:current)
30
+ record.audits.create(:user => user,
31
+ :username => user.try(self.username_method),
32
+ :action => action.to_s,
33
+ :changes => record.audit_changes
34
+ )
35
+ end
36
+
37
+ end
38
+
39
+ module InstanceMethods
40
+ def audit_changes
41
+ self.attributes
42
+ end
43
+ end
44
+
45
+ end
46
+
47
+ end
48
+
data/rails/init.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'simple_audit'
2
+
3
+ %w{ models helpers }.each do |dir|
4
+ path = File.join(File.dirname(__FILE__), '..', 'lib', 'app', dir)
5
+ $LOAD_PATH << path
6
+ ActiveSupport::Dependencies.load_paths << path
7
+ ActiveSupport::Dependencies.load_once_paths.delete(path)
8
+ end
9
+
10
+ ActiveRecord::Base.send :include, Cubus::SimpleAudit
11
+ ActionController::Base.send :helper, :simple_audit
@@ -0,0 +1,51 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{simple_audit}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Gabriel Tarnovan"]
12
+ s.date = %q{2010-06-02}
13
+ s.email = %q{gabriel.tarnovan@cubus.ro}
14
+ s.extra_rdoc_files = [
15
+ "README.markdown"
16
+ ]
17
+ s.files = [
18
+ "MIT-LICENSE",
19
+ "README.markdown",
20
+ "Rakefile",
21
+ "lib/app/helpers/simple_audit_helper.rb",
22
+ "lib/app/models/audit.rb",
23
+ "lib/simple_audit.rb",
24
+ "rails/init.rb",
25
+ "simple_audit.gemspec",
26
+ "tasks/simple_audit_tasks.rake",
27
+ "test/simple_audit_test.rb",
28
+ "test/test_helper.rb"
29
+ ]
30
+ s.homepage = %q{http://github.com/gtarnovan/simple_audit}
31
+ s.rdoc_options = ["--charset=UTF-8"]
32
+ s.require_paths = ["lib"]
33
+ s.rubyforge_project = %q{simple_audit}
34
+ s.rubygems_version = %q{1.3.6}
35
+ s.summary = %q{Simple auditing solution for ActiveRecord models}
36
+ s.test_files = [
37
+ "test/simple_audit_test.rb",
38
+ "test/test_helper.rb"
39
+ ]
40
+
41
+ if s.respond_to? :specification_version then
42
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
+ s.specification_version = 3
44
+
45
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
46
+ else
47
+ end
48
+ else
49
+ end
50
+ end
51
+
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :simple_audit do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class SimpleAuditTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'active_support/test_case'
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_audit
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Gabriel Tarnovan
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-06-02 00:00:00 +03:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description:
22
+ email: gabriel.tarnovan@cubus.ro
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README.markdown
29
+ files:
30
+ - MIT-LICENSE
31
+ - README.markdown
32
+ - Rakefile
33
+ - lib/app/helpers/simple_audit_helper.rb
34
+ - lib/app/models/audit.rb
35
+ - lib/simple_audit.rb
36
+ - rails/init.rb
37
+ - simple_audit.gemspec
38
+ - tasks/simple_audit_tasks.rake
39
+ - test/simple_audit_test.rb
40
+ - test/test_helper.rb
41
+ has_rdoc: true
42
+ homepage: http://github.com/gtarnovan/simple_audit
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options:
47
+ - --charset=UTF-8
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ requirements: []
65
+
66
+ rubyforge_project: simple_audit
67
+ rubygems_version: 1.3.6
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: Simple auditing solution for ActiveRecord models
71
+ test_files:
72
+ - test/simple_audit_test.rb
73
+ - test/test_helper.rb