acts_as_audited_on_steroids 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +36 -0
- data/Rakefile +29 -0
- data/VERSION +1 -0
- data/acts_as_audited_on_steroids.gemspec +60 -0
- data/init.rb +1 -0
- data/install.rb +1 -0
- data/lib/audit_actions.rb +10 -0
- data/lib/audit_builder.rb +44 -0
- data/lib/audit_events.rb +10 -0
- data/rails/init.rb +5 -0
- data/spec/audit_actions_spec.rb +32 -0
- data/spec/audit_events_spec.rb +34 -0
- data/spec/debug.log +1 -0
- data/spec/schema.rb +22 -0
- data/spec/spec.opts +4 -0
- data/spec/spec_helper.rb +10 -0
- data/tasks/acts_as_audited_on_steroids_tasks.rake +4 -0
- data/uninstall.rb +1 -0
- metadata +83 -0
data/README
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
ActsAsAuditedOnSteroids
|
2
|
+
=======================
|
3
|
+
|
4
|
+
Simple solution to improve existing acts_as_audited functionality.
|
5
|
+
|
6
|
+
Example
|
7
|
+
=======
|
8
|
+
|
9
|
+
class User < ActiveRecord::Base
|
10
|
+
def cuak
|
11
|
+
"I love to cuak"
|
12
|
+
end
|
13
|
+
audit_event :cuak, :with => :changes
|
14
|
+
end
|
15
|
+
|
16
|
+
class ProductsController < ApplicationController
|
17
|
+
audit_action :create, :as => :attempt, :with => Proc.new { |c| c.send :params }
|
18
|
+
|
19
|
+
def create
|
20
|
+
@product = Product.new(params[:product])
|
21
|
+
|
22
|
+
respond_to do |format|
|
23
|
+
if @product.save
|
24
|
+
flash[:notice] = 'Product was successfully created.'
|
25
|
+
format.html { redirect_to(@product) }
|
26
|
+
format.xml { render :xml => @product, :status => :created, :location => @product }
|
27
|
+
else
|
28
|
+
format.html { render :action => "new" }
|
29
|
+
format.xml { render :xml => @product.errors, :status => :unprocessable_entity }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
(c) Copyright 2010 Juan Manuel Barreneche and Lucas Florio. MIT. Do whatever you want.
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Rakefile
|
2
|
+
require 'rubygems'
|
3
|
+
require 'rake'
|
4
|
+
|
5
|
+
begin
|
6
|
+
require 'jeweler'
|
7
|
+
Jeweler::Tasks.new do |gemspec|
|
8
|
+
gemspec.name = "acts_as_audited_on_steroids"
|
9
|
+
gemspec.summary = "Simple solution to improve existing acts_as_audited functionality."
|
10
|
+
gemspec.description = ""
|
11
|
+
gemspec.homepage = "http://github.com/lucasefe/acts_as_audited_on_steroids"
|
12
|
+
gemspec.authors = ["Juan Manuel Barreneche", "Lucas Florio"]
|
13
|
+
gemspec.email = "snipperme@gmail.com"
|
14
|
+
# This doesn't belong here
|
15
|
+
# gemspec.add_dependency('searchlogic', '>= 2.4.19')
|
16
|
+
# gemspec.add_dependency('inherited_resources', '= 1.0.6')
|
17
|
+
# gemspec.add_dependency('formtastic','= 0.9.8')
|
18
|
+
# gemspec.add_dependency('validation_reflection','= 0.3.6')
|
19
|
+
# gemspec.add_dependency('show_for','= 0.1.3')
|
20
|
+
# gemspec.add_dependency('will_paginate','= 2.3.12')
|
21
|
+
# gemspec.add_dependency('haml','3.0.0.rc.2')
|
22
|
+
end
|
23
|
+
Jeweler::GemcutterTasks.new
|
24
|
+
rescue LoadError
|
25
|
+
puts "Jeweler not available. Install it with: sudo gem install jeweler -s http://gemcutter.org"
|
26
|
+
end
|
27
|
+
|
28
|
+
Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
|
29
|
+
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,60 @@
|
|
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{acts_as_audited_on_steroids}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Juan Manuel Barreneche", "Lucas Florio"]
|
12
|
+
s.date = %q{2010-05-11}
|
13
|
+
s.description = %q{}
|
14
|
+
s.email = %q{snipperme@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"README"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
"README",
|
20
|
+
"Rakefile",
|
21
|
+
"VERSION",
|
22
|
+
"acts_as_audited_on_steroids.gemspec",
|
23
|
+
"init.rb",
|
24
|
+
"install.rb",
|
25
|
+
"lib/audit_actions.rb",
|
26
|
+
"lib/audit_builder.rb",
|
27
|
+
"lib/audit_events.rb",
|
28
|
+
"rails/init.rb",
|
29
|
+
"spec/audit_actions_spec.rb",
|
30
|
+
"spec/audit_events_spec.rb",
|
31
|
+
"spec/debug.log",
|
32
|
+
"spec/schema.rb",
|
33
|
+
"spec/spec.opts",
|
34
|
+
"spec/spec_helper.rb",
|
35
|
+
"tasks/acts_as_audited_on_steroids_tasks.rake",
|
36
|
+
"uninstall.rb"
|
37
|
+
]
|
38
|
+
s.homepage = %q{http://github.com/lucasefe/acts_as_audited_on_steroids}
|
39
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
40
|
+
s.require_paths = ["lib"]
|
41
|
+
s.rubygems_version = %q{1.3.6}
|
42
|
+
s.summary = %q{Simple solution to improve existing acts_as_audited functionality.}
|
43
|
+
s.test_files = [
|
44
|
+
"spec/audit_actions_spec.rb",
|
45
|
+
"spec/audit_events_spec.rb",
|
46
|
+
"spec/schema.rb",
|
47
|
+
"spec/spec_helper.rb"
|
48
|
+
]
|
49
|
+
|
50
|
+
if s.respond_to? :specification_version then
|
51
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
52
|
+
s.specification_version = 3
|
53
|
+
|
54
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
55
|
+
else
|
56
|
+
end
|
57
|
+
else
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Include hook code here
|
data/install.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Install hook code here
|
@@ -0,0 +1,10 @@
|
|
1
|
+
module AuditActions
|
2
|
+
def audit_action(action_name, options = {})
|
3
|
+
filter_name = "audit_action_#{action_name}"
|
4
|
+
options[:id] ||= nil
|
5
|
+
before_filter filter_name, :only => action_name
|
6
|
+
define_method filter_name do
|
7
|
+
AuditBuilder.new(self, (options[:as] || action_name), options).create_audit
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
class AuditBuilder
|
2
|
+
FALLBACKS_FOR_WITH = [:attributes, :inspect]
|
3
|
+
FALLBACKS_FOR_ID = [:to_param, :id, :object_id]
|
4
|
+
|
5
|
+
attr_accessor :object, :audit_name, :options
|
6
|
+
def initialize(object, audit_name, options = {})
|
7
|
+
@object = object
|
8
|
+
@audit_name = audit_name
|
9
|
+
@options = options
|
10
|
+
end
|
11
|
+
def create_audit
|
12
|
+
Audit.create :auditable_type => extract_auditable_type,
|
13
|
+
:auditable_id => extract_auditable_id,
|
14
|
+
:changes => extract_changes,
|
15
|
+
:action => @audit_name.to_s
|
16
|
+
end
|
17
|
+
private
|
18
|
+
def extract_auditable_id
|
19
|
+
value_for attribute(:id, FALLBACKS_FOR_ID)
|
20
|
+
end
|
21
|
+
def extract_changes
|
22
|
+
value_for attribute(:with, FALLBACKS_FOR_WITH)
|
23
|
+
end
|
24
|
+
def attribute(option_key, fallbacks)
|
25
|
+
if @options.has_key?(option_key)
|
26
|
+
@options[option_key]
|
27
|
+
else
|
28
|
+
fallbacks.detect {|meth| @object.respond_to?meth}
|
29
|
+
end
|
30
|
+
end
|
31
|
+
def extract_auditable_type
|
32
|
+
@options[:type] || @object.class.name
|
33
|
+
end
|
34
|
+
def value_for(attribute)
|
35
|
+
case attribute
|
36
|
+
when Symbol
|
37
|
+
@object.send attribute
|
38
|
+
when Proc
|
39
|
+
attribute.call(@object)
|
40
|
+
else
|
41
|
+
attribute
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/lib/audit_events.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
module AuditEvents
|
2
|
+
def audit_event(event_name, options = {})
|
3
|
+
define_method("#{event_name}_with_audited") do |*args|
|
4
|
+
result = self.send "#{event_name}_without_audited", *args
|
5
|
+
AuditBuilder.new(self, (options[:as] || event_name), options).create_audit
|
6
|
+
result
|
7
|
+
end
|
8
|
+
alias_method_chain event_name.to_sym, :audited
|
9
|
+
end
|
10
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
class CosoController < ActionController::Base
|
4
|
+
extend AuditActions
|
5
|
+
def run
|
6
|
+
render :text => "Great!"# , :with => Proc.new {|c| params }
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
ActionController::Routing::Routes.draw do |map|
|
11
|
+
map.run '/run', :controller => 'coso', :action => 'run'
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
describe CosoController, "using AuditAction", :type => :controller do
|
16
|
+
it 'should know how to declare an action to be audited' do
|
17
|
+
CosoController.should respond_to :audit_action
|
18
|
+
end
|
19
|
+
describe 'while auditing an action' do
|
20
|
+
before do
|
21
|
+
CosoController.audit_action :run, :as => 'exec', :with => { :data => 'data' }
|
22
|
+
end
|
23
|
+
it "should create a new Audit when the method is executed" do
|
24
|
+
Audit.should_receive(:create).with(:action => 'exec',
|
25
|
+
:auditable_type => "CosoController",
|
26
|
+
:changes => { :data => 'data' },
|
27
|
+
:auditable_id => nil)
|
28
|
+
get :run
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe "An object using AuditEvent " do
|
4
|
+
before do
|
5
|
+
@class = Class.new do
|
6
|
+
extend AuditEvents
|
7
|
+
attr_accessor :data
|
8
|
+
def run
|
9
|
+
self.data ||= 0
|
10
|
+
self.data += 1
|
11
|
+
end
|
12
|
+
def attributes
|
13
|
+
{:data => data}
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
it 'should know how to declare an event to be audited' do
|
18
|
+
@class.should respond_to :audit_event
|
19
|
+
end
|
20
|
+
describe 'while auditing a method' do
|
21
|
+
before do
|
22
|
+
@class.audit_event :run, :with => :attributes
|
23
|
+
@object = @class.new
|
24
|
+
end
|
25
|
+
it "should create a new Audit when the method is executed" do
|
26
|
+
Audit.should_receive(:create).with(:action => 'run',
|
27
|
+
:auditable_type => @class.name,
|
28
|
+
:changes => {:data => 1},
|
29
|
+
:auditable_id => @object.to_param)
|
30
|
+
@object.run
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
data/spec/debug.log
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Logfile created on Tue May 11 12:10:47 -0300 2010 by logger.rb/22285
|
data/spec/schema.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
ActiveRecord::Schema.define(:version => 1) do
|
2
|
+
|
3
|
+
create_table "audits", :force => true do |t|
|
4
|
+
t.integer "auditable_id"
|
5
|
+
t.string "auditable_type"
|
6
|
+
t.integer "user_id"
|
7
|
+
t.string "user_type"
|
8
|
+
t.string "username"
|
9
|
+
t.string "action"
|
10
|
+
t.text "changes"
|
11
|
+
t.integer "version", :default => 0
|
12
|
+
t.datetime "created_at"
|
13
|
+
end
|
14
|
+
create_table "products", :force => true do |t|
|
15
|
+
t.string "title"
|
16
|
+
t.decimal "price"
|
17
|
+
t.text "description"
|
18
|
+
t.datetime "created_at"
|
19
|
+
t.datetime "updated_at"
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
begin
|
2
|
+
require File.dirname(__FILE__) + '/../../../../spec/spec_helper'
|
3
|
+
rescue LoadError
|
4
|
+
puts "You need to install rspec in your base app"
|
5
|
+
exit
|
6
|
+
end
|
7
|
+
|
8
|
+
plugin_spec_dir = File.dirname(__FILE__)
|
9
|
+
ActiveRecord::Base.logger = Logger.new(plugin_spec_dir + "/debug.log")
|
10
|
+
|
data/uninstall.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Uninstall hook code here
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: acts_as_audited_on_steroids
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Juan Manuel Barreneche
|
13
|
+
- Lucas Florio
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-05-11 00:00:00 -03:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: ""
|
23
|
+
email: snipperme@gmail.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files:
|
29
|
+
- README
|
30
|
+
files:
|
31
|
+
- README
|
32
|
+
- Rakefile
|
33
|
+
- VERSION
|
34
|
+
- acts_as_audited_on_steroids.gemspec
|
35
|
+
- init.rb
|
36
|
+
- install.rb
|
37
|
+
- lib/audit_actions.rb
|
38
|
+
- lib/audit_builder.rb
|
39
|
+
- lib/audit_events.rb
|
40
|
+
- rails/init.rb
|
41
|
+
- spec/audit_actions_spec.rb
|
42
|
+
- spec/audit_events_spec.rb
|
43
|
+
- spec/debug.log
|
44
|
+
- spec/schema.rb
|
45
|
+
- spec/spec.opts
|
46
|
+
- spec/spec_helper.rb
|
47
|
+
- tasks/acts_as_audited_on_steroids_tasks.rake
|
48
|
+
- uninstall.rb
|
49
|
+
has_rdoc: true
|
50
|
+
homepage: http://github.com/lucasefe/acts_as_audited_on_steroids
|
51
|
+
licenses: []
|
52
|
+
|
53
|
+
post_install_message:
|
54
|
+
rdoc_options:
|
55
|
+
- --charset=UTF-8
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
version: "0"
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
segments:
|
70
|
+
- 0
|
71
|
+
version: "0"
|
72
|
+
requirements: []
|
73
|
+
|
74
|
+
rubyforge_project:
|
75
|
+
rubygems_version: 1.3.6
|
76
|
+
signing_key:
|
77
|
+
specification_version: 3
|
78
|
+
summary: Simple solution to improve existing acts_as_audited functionality.
|
79
|
+
test_files:
|
80
|
+
- spec/audit_actions_spec.rb
|
81
|
+
- spec/audit_events_spec.rb
|
82
|
+
- spec/schema.rb
|
83
|
+
- spec/spec_helper.rb
|