active_model_listener 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1,3 @@
1
+ 2009-10-26
2
+ - you no longer have to define all 3 callback methods in callback classes
3
+ - callback methods are execute without listeners, to prevent recursion
data/README.md CHANGED
@@ -2,6 +2,17 @@
2
2
 
3
3
  Simple, global ActiveRecord event observers, using a middleware architecture, that can easily be turned on and off. Designed for audit trails, activity feeds and other application-level event handlers.
4
4
 
5
+ ## Installation
6
+
7
+ The gem is hosted at gemcutter. You can either add gemcutter as a source, or you can make gemcutter your default source (recommended):
8
+
9
+ gem install gemcutter
10
+ gem tumble
11
+
12
+ Then, install it like any other gem:
13
+
14
+ gem install active_model_listener
15
+
5
16
  ## Usage
6
17
 
7
18
  First, require Active Model Listener above your rails initializer:
@@ -46,6 +57,8 @@ Then, create a listener class that defines methods for after_create, after_updat
46
57
  end
47
58
  end
48
59
 
60
+ Inside the after_create, after_destroy and after_update methods all calls will be called _without_ listeners, to prevent recursion. If you need listeners to be turned on during those blocks, you'll have to set them yourself within the blocks.
61
+
49
62
  ## Turning off listeners in specs
50
63
 
51
64
  When unit testing if your listeners are all firing your unit tests become integration tests. To avoid this, you can easily turn off listeners for all specs all the time:
data/Rakefile CHANGED
@@ -16,6 +16,8 @@ rescue LoadError
16
16
  puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
17
17
  end
18
18
 
19
+ Jeweler::GemcutterTasks.new
20
+
19
21
  require 'spec/rake/spectask'
20
22
  Spec::Rake::SpecTask.new(:spec) do |spec|
21
23
  spec.libs << 'lib' << 'spec'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.2.2
@@ -1,12 +1,15 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
1
4
  # -*- encoding: utf-8 -*-
2
5
 
3
6
  Gem::Specification.new do |s|
4
7
  s.name = %q{active_model_listener}
5
- s.version = "0.2.1"
8
+ s.version = "0.2.2"
6
9
 
7
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
11
  s.authors = ["Jeff Dean"]
9
- s.date = %q{2009-10-03}
12
+ s.date = %q{2009-10-26}
10
13
  s.email = %q{jeff@zilkey.com}
11
14
  s.extra_rdoc_files = [
12
15
  "LICENSE",
@@ -15,6 +18,7 @@ Gem::Specification.new do |s|
15
18
  s.files = [
16
19
  ".document",
17
20
  ".gitignore",
21
+ "CHANGELOG",
18
22
  "LICENSE",
19
23
  "README.md",
20
24
  "Rakefile",
@@ -45,3 +49,4 @@ Gem::Specification.new do |s|
45
49
  else
46
50
  end
47
51
  end
52
+
@@ -10,8 +10,11 @@ class ActiveModelListener
10
10
  end
11
11
 
12
12
  def dispatch(object, action)
13
+ method_name = "after_#{action}"
13
14
  self.listeners.each do |listener|
14
- listener.send "after_#{action}", object
15
+ without_listeners do
16
+ listener.send method_name, object if listener.respond_to?(method_name)
17
+ end
15
18
  end
16
19
  end
17
20
 
@@ -2,6 +2,7 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  class GenericListener
4
4
  def self.after_create(object)
5
+ object.class.create
5
6
  end
6
7
  def self.after_update(object)
7
8
  end
@@ -9,6 +10,9 @@ class GenericListener
9
10
  end
10
11
  end
11
12
 
13
+ class SimpleListener
14
+ end
15
+
12
16
  class FooListener < GenericListener
13
17
  end
14
18
 
@@ -61,6 +65,13 @@ describe ActiveModelListener do
61
65
  end
62
66
  end
63
67
 
68
+ describe "callback methods" do
69
+ it "should turn off other listeners" do
70
+ FooListener.should_receive(:after_create).once
71
+ Article.create! :title => "foo"
72
+ end
73
+ end
74
+
64
75
  describe "after create" do
65
76
  it "should fire off all wired up events" do
66
77
  FooListener.should_receive(:after_create)
@@ -87,4 +98,18 @@ describe ActiveModelListener do
87
98
  end
88
99
  end
89
100
 
101
+ describe "a listener with missing methods" do
102
+ before do
103
+ ActiveModelListener.listeners.clear
104
+ ActiveModelListener.listeners << SimpleListener
105
+ end
106
+
107
+ it "should not fire off the missing methods" do
108
+ article = Article.create
109
+ proc do
110
+ article.destroy
111
+ end.should_not raise_error
112
+ end
113
+ end
114
+
90
115
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_model_listener
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Dean
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-03 00:00:00 -04:00
12
+ date: 2009-10-26 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -25,6 +25,7 @@ extra_rdoc_files:
25
25
  files:
26
26
  - .document
27
27
  - .gitignore
28
+ - CHANGELOG
28
29
  - LICENSE
29
30
  - README.md
30
31
  - Rakefile