cia 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cia (0.3.1)
4
+ cia (0.3.2)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
data/Readme.md CHANGED
@@ -49,6 +49,15 @@ last_passwords = changes.where(:attribute_name => "crypted_password").map(&:new_
49
49
 
50
50
  # exceptions (raised by default)
51
51
  CIA.exception_handler = lambda{|e| raise e unless Rails.env.production? }
52
+
53
+ # conditional auditing
54
+ class User < ActiveRecord::Base
55
+ audited_attributes :email, :if => :interesting?
56
+
57
+ def interesting?
58
+ ...
59
+ end
60
+ end
52
61
  ```
53
62
 
54
63
 
data/lib/cia.rb CHANGED
@@ -23,6 +23,8 @@ module CIA
23
23
 
24
24
  def self.record(action, source)
25
25
  return unless current_transaction
26
+ options = source.class.audited_attribute_options
27
+ return if options and options[:if] and not source.send(options[:if])
26
28
 
27
29
  changes = source.changes.slice(*source.class.audited_attributes)
28
30
  message = source.audit_message if source.respond_to?(:audit_message)
data/lib/cia/auditable.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  module CIA
2
2
  module Auditable
3
3
  def self.included(base)
4
- base.class_attribute :audited_attributes
4
+ base.class_attribute :audited_attributes, :audited_attribute_options
5
5
  base.send :extend, ClassMethods
6
6
  base.after_create {|record| CIA.record(:create, record) }
7
7
  base.after_update {|record| CIA.record(:update, record) }
@@ -10,9 +10,14 @@ module CIA
10
10
 
11
11
  module ClassMethods
12
12
  def audit_attribute(*attributes)
13
+ options = (attributes.last.is_a?(Hash) ? attributes.pop : {})
14
+
13
15
  self.audited_attributes = Set.new unless audited_attributes
14
16
  self.audited_attributes += attributes.map(&:to_s)
15
17
 
18
+ self.audited_attribute_options ||= {}
19
+ self.audited_attribute_options.merge!(options)
20
+
16
21
  has_many :cia_events, :class_name => "CIA::Event", :as => :source
17
22
  has_many :cia_attribute_changes, :class_name => "CIA::AttributeChange", :as => :source
18
23
  end
data/lib/cia/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module CIA
2
- VERSION = '0.3.1'
2
+ VERSION = '0.3.2'
3
3
  end
data/spec/cia_spec.rb CHANGED
@@ -84,6 +84,25 @@ describe CIA do
84
84
  CIA::Event.last.action.should == "update"
85
85
  end
86
86
 
87
+ context ":if" do
88
+ let(:object) { CarWithIf.new }
89
+
90
+ it "tracks if :if is true" do
91
+ expect{
92
+ object.bar = true
93
+ object.save!
94
+ }.to change{ CIA::Event.count }.by(+1)
95
+ CIA::Event.last.action.should == "create"
96
+ end
97
+
98
+ it "does not track if :if is false" do
99
+ expect{
100
+ object.save!
101
+ }.to_not change{ CIA::Event.count }
102
+ CIA::Event.last.should == nil
103
+ end
104
+ end
105
+
87
106
  context "events" do
88
107
  def parse_event_changes(event)
89
108
  event.attribute_changes.map { |c| [c.attribute_name, c.old_value, c.new_value] }
data/spec/spec_helper.rb CHANGED
@@ -49,6 +49,17 @@ class CarWith3Attributes < ActiveRecord::Base
49
49
  audit_attribute :drivers
50
50
  end
51
51
 
52
+ class CarWithIf < ActiveRecord::Base
53
+ self.table_name = "cars"
54
+ include CIA::Auditable
55
+ audit_attribute :wheels, :if => :foo?
56
+ attr_accessor :bar
57
+
58
+ def foo?
59
+ bar
60
+ end
61
+ end
62
+
52
63
  def create_event
53
64
  CIA::Event.create!(:source => Car.create!, :actor => User.create!, :action => "update")
54
65
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cia
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-25 00:00:00.000000000 Z
12
+ date: 2012-06-26 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description:
15
15
  email: michael@grosser.it
@@ -53,7 +53,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
53
53
  version: '0'
54
54
  segments:
55
55
  - 0
56
- hash: -507458673584956374
56
+ hash: 516757827358886270
57
57
  required_rubygems_version: !ruby/object:Gem::Requirement
58
58
  none: false
59
59
  requirements:
@@ -62,7 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
62
62
  version: '0'
63
63
  segments:
64
64
  - 0
65
- hash: -507458673584956374
65
+ hash: 516757827358886270
66
66
  requirements: []
67
67
  rubyforge_project:
68
68
  rubygems_version: 1.8.24