Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Tarnovan committed May 31, 2010
0 parents commit 34a69d0
Show file tree
Hide file tree
Showing 10 changed files with 195 additions and 0 deletions.
20 changes: 20 additions & 0 deletions MIT-LICENSE
@@ -0,0 +1,20 @@
Copyright (c) 2010 [name of plugin creator]

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
13 changes: 13 additions & 0 deletions README
@@ -0,0 +1,13 @@
SimpleAudit
===========

Introduction goes here.


Example
=======

Example goes here.


Copyright (c) 2010 [name of plugin creator], released under the MIT license
23 changes: 23 additions & 0 deletions Rakefile
@@ -0,0 +1,23 @@
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

desc 'Default: run unit tests.'
task :default => :test

desc 'Test the simple_audit plugin.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.libs << 'test'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end

desc 'Generate documentation for the simple_audit plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'SimpleAudit'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
end
26 changes: 26 additions & 0 deletions lib/app/helpers/simple_audit_helper.rb
@@ -0,0 +1,26 @@
module SimpleAuditHelper

def render_audits(audits)
audits = audits.dup.sort{|a, b| b.created_at <=> a.created_at}
res = ''
audits.each_with_index do |audit, index|
older_audit = audits[index + 1]
res += content_tag(:div, :class => 'audit') do
content_tag(:div, audit.action, :class => "action #{audit.action}") +
content_tag(:div, audit.username, :class => "user") +
content_tag(:div, l(audit.created_at), :class => "timestamp") +
content_tag(:div, :class => 'changes') do
if older_audit.present?
audit.delta(older_audit).collect {|k, v| "\n#{Booking.human_attribute_name(k)}: #{v.last}" }
else
audit.changes.reject{|k, v| v.blank?}.collect {|k, v| "\n#{Booking.human_attribute_name(k)}: #{v}"}
end
end
end
end
res
end

end


39 changes: 39 additions & 0 deletions lib/app/models/audit.rb
@@ -0,0 +1,39 @@
class Audit < ActiveRecord::Base
belongs_to :auditable, :polymorphic => true
belongs_to :user, :polymorphic => true
serialize :changes

#
# returns the differences of the to audits
# result is a hash containing arrays of the form [<value_in_other_audit>, <value_in_this_audit>]
# the values in this audit take precedence
#
def delta(other)

return self.changes if other.nil?

d = {}

(self.changes.keys - other.changes.keys).each do |k|
d[k] = [nil, self.changes[k]]
end

(other.changes.keys - self.changes.keys).each do |k|
d[k] = [other.changes[k], nil]
end

self.changes.keys.each do |k|
if self.changes[k] != other.changes[k]
d[k] = [other.changes[k], self.changes[k]]
end
end

d

end

def changes
self[:changes]
end

end
48 changes: 48 additions & 0 deletions lib/simple_audit.rb
@@ -0,0 +1,48 @@
module Cubus
module SimpleAudit

def self.included(base)
base.send :extend, ClassMethods
end

module ClassMethods

# create a new Audit record
# if no user is specified, try to use sentient_user's User.current

def simple_audit(options = {})
class_eval do

cattr_accessor :username_method
self.username_method = (options[:username_method] || :name).to_s

has_many :audits, :as => :auditable
after_create {|record| audit(record, :create)}
after_update {|record| audit(record, :update)}


end
send :include, InstanceMethods
end

def audit record, action = :update, user = nil
user ||= User.current if User.respond_to?(:current)
record.audits.create(:user => user,
:username => user.try(self.username_method),
:action => action.to_s,
:changes => record.audit_changes
)
end

end

module InstanceMethods
def audit_changes
self.attributes
end
end

end

end

11 changes: 11 additions & 0 deletions rails/init.rb
@@ -0,0 +1,11 @@
require 'simple_audit'

%w{ models helpers }.each do |dir|
path = File.join(File.dirname(__FILE__), '..', 'lib', 'app', dir)
$LOAD_PATH << path
ActiveSupport::Dependencies.load_paths << path
ActiveSupport::Dependencies.load_once_paths.delete(path)
end

ActiveRecord::Base.send :include, Cubus::SimpleAudit
ActionController::Base.send :helper, :simple_audit
4 changes: 4 additions & 0 deletions tasks/simple_audit_tasks.rake
@@ -0,0 +1,4 @@
# desc "Explaining what the task does"
# task :simple_audit do
# # Task goes here
# end
8 changes: 8 additions & 0 deletions test/simple_audit_test.rb
@@ -0,0 +1,8 @@
require 'test_helper'

class SimpleAuditTest < ActiveSupport::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end
3 changes: 3 additions & 0 deletions test/test_helper.rb
@@ -0,0 +1,3 @@
require 'rubygems'
require 'active_support'
require 'active_support/test_case'

0 comments on commit 34a69d0

Please sign in to comment.