cia 0.5.2 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cia (0.5.2)
4
+ cia (0.5.3)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/mgrosser/code/tools/cia
3
3
  specs:
4
- cia (0.5.1)
4
+ cia (0.5.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/mgrosser/code/tools/cia
3
3
  specs:
4
- cia (0.5.1)
4
+ cia (0.5.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/lib/cia.rb CHANGED
@@ -5,6 +5,7 @@ require 'cia/auditable'
5
5
  module CIA
6
6
  autoload 'Event', 'cia/event'
7
7
  autoload 'AttributeChange', 'cia/attribute_change'
8
+ autoload 'SourceValidation', 'cia/source_validation'
8
9
 
9
10
  class << self
10
11
  attr_accessor :exception_handler
@@ -1,11 +1,12 @@
1
1
  module CIA
2
2
  class AttributeChange < ActiveRecord::Base
3
+ include SourceValidation
3
4
  self.table_name = "cia_attribute_changes"
4
5
 
5
6
  belongs_to :event, :foreign_key => "cia_event_id"
6
7
  belongs_to :source, :polymorphic => true
7
8
 
8
- validates_presence_of :event, :attribute_name, :source
9
+ validates_presence_of :event, :attribute_name
9
10
 
10
11
  if ActiveRecord::VERSION::MAJOR > 2
11
12
  scope :previous, :order => "id desc"
@@ -18,5 +19,11 @@ module CIA
18
19
  def self.on_attribute(attribute)
19
20
  scoped(:conditions => {:attribute_name => attribute})
20
21
  end
22
+
23
+ private
24
+
25
+ def source_must_be_present?
26
+ event.present? && event.source_must_be_present?
27
+ end
21
28
  end
22
29
  end
@@ -1,5 +1,6 @@
1
1
  module CIA
2
2
  class Event < ActiveRecord::Base
3
+ include SourceValidation
3
4
  self.table_name = "cia_events"
4
5
 
5
6
  belongs_to :actor, :polymorphic => true
@@ -7,8 +8,6 @@ module CIA
7
8
  has_many :attribute_changes, :foreign_key => :cia_event_id
8
9
 
9
10
  validates_presence_of :action
10
- validates_presence_of :source, :if => :source_must_be_exist?
11
- validates_presence_of :source_id, :source_type, :unless => :source_must_be_exist?
12
11
 
13
12
  def self.previous
14
13
  scoped(:order => "created_at desc")
@@ -33,9 +32,7 @@ module CIA
33
32
  end
34
33
  end
35
34
 
36
- private
37
-
38
- def source_must_be_exist?
35
+ def source_must_be_present?
39
36
  new_record? and action != "destroy" and (!attributes.key?("source_display_name") or source_display_name.blank?)
40
37
  end
41
38
  end
@@ -0,0 +1,8 @@
1
+ module CIA
2
+ module SourceValidation
3
+ def self.included(base)
4
+ base.validates_presence_of :source_id, :source_type, :unless => :source_must_be_present?
5
+ base.validates_presence_of :source, :if => :source_must_be_present?
6
+ end
7
+ end
8
+ end
@@ -1,3 +1,3 @@
1
1
  module CIA
2
- VERSION = '0.5.2'
2
+ VERSION = '0.5.3'
3
3
  end
@@ -42,4 +42,24 @@ describe CIA::AttributeChange do
42
42
  CIA::AttributeChange.on_attribute(:xxx).all.should == [a]
43
43
  end
44
44
  end
45
+
46
+ describe "enforcing presence of source" do
47
+ it "requires a source when associated event requires a source" do
48
+ event = CIA::Event.new { |event| event.id = 1 }
49
+ event.stub(:source_must_be_present? => true)
50
+ change = CIA::AttributeChange.new(:event => event, :attribute_name => 'awesomeness')
51
+
52
+ change.valid?.should be_false
53
+ change.errors.full_messages.should =~ ["Source can't be blank"]
54
+ end
55
+
56
+ it "does not require a source when associated event does not" do
57
+ event = CIA::Event.new { |event| event.id = 1 }
58
+ event.stub(:source_must_be_present? => false)
59
+ change = CIA::AttributeChange.new(:event => event, :attribute_name => 'awesomeness',
60
+ :source_type => 'ObscureType', :source_id => 101)
61
+
62
+ change.valid?.should be_true
63
+ end
64
+ end
45
65
  end
@@ -309,7 +309,7 @@ describe CIA do
309
309
  CIA.exception_handler = lambda{|e| ex = e }
310
310
  yield
311
311
  ex
312
- rescue
312
+ ensure
313
313
  CIA.exception_handler = old
314
314
  end
315
315
  end
@@ -1,4 +1,3 @@
1
- $LOAD_PATH.unshift 'lib'
2
1
  require 'cia'
3
2
  require 'after_commit' if ActiveRecord::VERSION::MAJOR == 2
4
3
 
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.5.2
4
+ version: 0.5.3
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: 2013-04-11 00:00:00.000000000 Z
12
+ date: 2013-05-23 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description:
15
15
  email: michael@grosser.it
@@ -33,6 +33,7 @@ files:
33
33
  - lib/cia/attribute_change.rb
34
34
  - lib/cia/auditable.rb
35
35
  - lib/cia/event.rb
36
+ - lib/cia/source_validation.rb
36
37
  - lib/cia/version.rb
37
38
  - spec/cia/attribute_change_spec.rb
38
39
  - spec/cia/event_spec.rb
@@ -53,7 +54,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
53
54
  version: '0'
54
55
  segments:
55
56
  - 0
56
- hash: 3219662641279385344
57
+ hash: 2131541393242182873
57
58
  required_rubygems_version: !ruby/object:Gem::Requirement
58
59
  none: false
59
60
  requirements:
@@ -62,7 +63,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
62
63
  version: '0'
63
64
  segments:
64
65
  - 0
65
- hash: 3219662641279385344
66
+ hash: 2131541393242182873
66
67
  requirements: []
67
68
  rubyforge_project:
68
69
  rubygems_version: 1.8.25