rails-observers 0.1.2 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4ebf1dea2a2c82e54123428320e6685c5765da02
4
- data.tar.gz: 52ef78d1ccb52ed81cd43e91d582b18976f18123
3
+ metadata.gz: 99ca65394dfce3f1dd3e17d7b8ed228c3794ddb4
4
+ data.tar.gz: 2794ed3480d4b5f31dac6d0d0dc0f0597ac71877
5
5
  SHA512:
6
- metadata.gz: c2465a6f154cd1de2b3439333d2abc2719db0dd0a3294779e09df4caf31f37b66fe61a4ae0622a558011bd6e96a383a243814ee7afee059649aac335cbd24f7f
7
- data.tar.gz: ee0dd0aca9d35994d059e92341861319ca4aab3aa00ae104b19e28d94a646798163fb926f00fbe5e5eb177bf1598d331e4e3ceb19eda161323fc0b151c1a4a02
6
+ metadata.gz: 7deaf20f19f602726f704ce2ce3aee04961b32cd9b76db9e2c2ed1d7d9eceea20901b82c5fedd9339d3324a65ea8773c983c19e34db06c3e18a2e463c20cb0a8
7
+ data.tar.gz: bd2e858515a2f21c68a73cbcc64eee28f6192b170de9005dc9f7a36b1497b09b92f8ebd037c87dd8bd3c782632f9d5f3c2e4f45ee3f5769daecb5f1590a5bee0
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2012 Steve Klabnik, Rafael Mendonça França
1
+ Copyright (c) 2012-2016 Steve Klabnik, Rafael Mendonça França
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -6,9 +6,9 @@ Rails Observers (removed from core in Rails 4.0)
6
6
  ## Installation
7
7
 
8
8
  Add this line to your application's Gemfile:
9
-
10
- gem 'rails-observers'
11
-
9
+ ```ruby
10
+ gem 'rails-observers'
11
+ ```
12
12
  And then execute:
13
13
 
14
14
  $ bundle
@@ -30,7 +30,8 @@ Observer classes respond to life cycle callbacks to implement trigger-like
30
30
  behavior outside the original class. This is a great way to reduce the
31
31
  clutter that normally comes when the model class is burdened with
32
32
  functionality that doesn't pertain to the core responsibility of the
33
- class. Example:
33
+ class. Observers are put in `app/models` (e.g.
34
+ `app/models/comment_observer.rb`). Example:
34
35
 
35
36
  ```ruby
36
37
  class CommentObserver < ActiveRecord::Observer
@@ -56,16 +57,31 @@ end
56
57
 
57
58
  This Observer uses logger to log when specific callbacks are triggered.
58
59
 
59
- Please note that observers are called in the order that they are defined. That means that callbacks in an observer
60
+ The convention is to name observers after the class they observe. If you
61
+ absolutely need to override this, or want to use one observer for several
62
+ classes, use `observe`:
63
+
64
+ ```ruby
65
+ class NotificationsObserver < ActiveRecord::Observer
66
+ observe :comment, :like
67
+
68
+ def after_create(record)
69
+ # notifiy users of new comment or like
70
+ end
71
+
72
+ end
73
+ ```
74
+
75
+ Please note that observers are called in the order that they are defined. This means that callbacks in an observer
60
76
  will always be called *after* callbacks defined in the model itself. Likewise, `has_one` and `has_many`
61
- use callbacks to enforce `:dependent => :destroy`. Therefore, associated records will be destroyed before
62
- the observer's `before_destory` is called.
77
+ use callbacks to enforce `dependent: :destroy`. Therefore, associated records will be destroyed before
78
+ the observer's `before_destroy` is called.
63
79
 
64
80
  For an observer to be active, it must be registered first. This can be done by adding the following line into the `application.rb`:
65
81
 
66
82
  config.active_record.observers = :contact_observer
67
83
 
68
- Obeservers can also be registered on an environment-specific basis by simply using the corresponding environment's configuration file instead of `application.rb`.
84
+ Observers can also be registered on an environment-specific basis by simply using the corresponding environment's configuration file instead of `application.rb`.
69
85
 
70
86
  ### Action Controller Sweeper
71
87
 
@@ -78,9 +94,9 @@ class ListSweeper < ActionController::Caching::Sweeper
78
94
 
79
95
  def after_save(record)
80
96
  list = record.is_a?(List) ? record : record.list
81
- expire_page(:controller => "lists", :action => %w( show public feed ), :id => list.id)
82
- expire_action(:controller => "lists", :action => "all")
83
- list.shares.each { |share| expire_page(:controller => "lists", :action => "show", :id => share.url_key) }
97
+ expire_page(controller: "lists", action: %w( show public feed ), id: list.id)
98
+ expire_action(controller: "lists", action: "all")
99
+ list.shares.each { |share| expire_page(controller: "lists", action: "show", id: share.url_key) }
84
100
  end
85
101
  end
86
102
  ```
@@ -90,7 +106,7 @@ The sweeper is assigned in the controllers that wish to have its job performed u
90
106
  ```ruby
91
107
  class ListsController < ApplicationController
92
108
  caches_action :index, :show, :public, :feed
93
- cache_sweeper :list_sweeper, :only => [ :edit, :destroy, :share ]
109
+ cache_sweeper :list_sweeper, only: [ :edit, :destroy, :share ]
94
110
  end
95
111
  ```
96
112
 
@@ -101,7 +117,7 @@ You can also name an explicit class in the declaration of a sweeper, which is ne
101
117
  ```ruby
102
118
  class ListsController < ApplicationController
103
119
  caches_action :index, :show, :public, :feed
104
- cache_sweeper OpenBar::Sweeper, :only => [ :edit, :destroy, :share ]
120
+ cache_sweeper OpenBar::Sweeper, only: [ :edit, :destroy, :share ]
105
121
  end
106
122
  ```
107
123
 
@@ -3,7 +3,7 @@ module ActionController #:nodoc:
3
3
  extend ActiveSupport::Autoload
4
4
 
5
5
  eager_autoload do
6
- autoload :Sweeper, 'rails/observers/action_controller/caching/sweeping'
6
+ autoload :Sweeper, 'rails/observers/action_controller/caching/sweeper'
7
7
  autoload :Sweeping, 'rails/observers/action_controller/caching/sweeping'
8
8
  end
9
9
 
@@ -0,0 +1,61 @@
1
+ module ActionController #:nodoc:
2
+ module Caching
3
+ class Sweeper < ActiveRecord::Observer #:nodoc:
4
+ attr_accessor :controller
5
+
6
+ def initialize(*args)
7
+ super
8
+ @controller = nil
9
+ end
10
+
11
+ def before(controller)
12
+ self.controller = controller
13
+ callback(:before) if controller.perform_caching
14
+ true # before method from sweeper should always return true
15
+ end
16
+
17
+ def after(controller)
18
+ self.controller = controller
19
+ callback(:after) if controller.perform_caching
20
+ end
21
+
22
+ def around(controller)
23
+ before(controller)
24
+ yield
25
+ after(controller)
26
+ ensure
27
+ clean_up
28
+ end
29
+
30
+ protected
31
+ # gets the action cache path for the given options.
32
+ def action_path_for(options)
33
+ Actions::ActionCachePath.new(controller, options).path
34
+ end
35
+
36
+ # Retrieve instance variables set in the controller.
37
+ def assigns(key)
38
+ controller.instance_variable_get("@#{key}")
39
+ end
40
+
41
+ private
42
+ def clean_up
43
+ # Clean up, so that the controller can be collected after this request
44
+ self.controller = nil
45
+ end
46
+
47
+ def callback(timing)
48
+ controller_callback_method_name = "#{timing}_#{controller.controller_name.underscore}"
49
+ action_callback_method_name = "#{controller_callback_method_name}_#{controller.action_name}"
50
+
51
+ __send__(controller_callback_method_name) if respond_to?(controller_callback_method_name, true)
52
+ __send__(action_callback_method_name) if respond_to?(action_callback_method_name, true)
53
+ end
54
+
55
+ def method_missing(method, *arguments, &block)
56
+ return super unless @controller
57
+ @controller.__send__(method, *arguments, &block)
58
+ end
59
+ end
60
+ end
61
+ end
@@ -39,9 +39,9 @@ module ActionController #:nodoc:
39
39
  sweeper_instance = (sweeper.is_a?(Symbol) ? Object.const_get(sweeper.to_s.classify) : sweeper).instance
40
40
 
41
41
  if sweeper_instance.is_a?(Sweeper)
42
- around_filter(sweeper_instance, :only => configuration[:only])
42
+ around_action(sweeper_instance, :only => configuration[:only])
43
43
  else
44
- after_filter(sweeper_instance, :only => configuration[:only])
44
+ after_action(sweeper_instance, :only => configuration[:only])
45
45
  end
46
46
  end
47
47
  end
@@ -49,63 +49,7 @@ module ActionController #:nodoc:
49
49
  end
50
50
 
51
51
  if defined?(ActiveRecord) and defined?(ActiveRecord::Observer)
52
- class Sweeper < ActiveRecord::Observer #:nodoc:
53
- attr_accessor :controller
54
-
55
- def initialize(*args)
56
- super
57
- @controller = nil
58
- end
59
-
60
- def before(controller)
61
- self.controller = controller
62
- callback(:before) if controller.perform_caching
63
- true # before method from sweeper should always return true
64
- end
65
-
66
- def after(controller)
67
- self.controller = controller
68
- callback(:after) if controller.perform_caching
69
- end
70
-
71
- def around(controller)
72
- before(controller)
73
- yield
74
- after(controller)
75
- ensure
76
- clean_up
77
- end
78
-
79
- protected
80
- # gets the action cache path for the given options.
81
- def action_path_for(options)
82
- Actions::ActionCachePath.new(controller, options).path
83
- end
84
-
85
- # Retrieve instance variables set in the controller.
86
- def assigns(key)
87
- controller.instance_variable_get("@#{key}")
88
- end
89
-
90
- private
91
- def clean_up
92
- # Clean up, so that the controller can be collected after this request
93
- self.controller = nil
94
- end
95
-
96
- def callback(timing)
97
- controller_callback_method_name = "#{timing}_#{controller.controller_name.underscore}"
98
- action_callback_method_name = "#{controller_callback_method_name}_#{controller.action_name}"
99
-
100
- __send__(controller_callback_method_name) if respond_to?(controller_callback_method_name, true)
101
- __send__(action_callback_method_name) if respond_to?(action_callback_method_name, true)
102
- end
103
-
104
- def method_missing(method, *arguments, &block)
105
- return super unless @controller
106
- @controller.__send__(method, *arguments, &block)
107
- end
108
- end
52
+ require 'rails/observers/action_controller/caching/sweeper'
109
53
  end
110
54
  end
111
55
  end
@@ -0,0 +1,41 @@
1
+ require 'rails/observers/active_model/observing'
2
+
3
+ module ActiveResource
4
+ module Observing
5
+ def self.prepended(context)
6
+ context.include ActiveModel::Observing
7
+ end
8
+
9
+ def create(*)
10
+ notify_observers(:before_create)
11
+ if result = super
12
+ notify_observers(:after_create)
13
+ end
14
+ result
15
+ end
16
+
17
+ def save(*)
18
+ notify_observers(:before_save)
19
+ if result = super
20
+ notify_observers(:after_save)
21
+ end
22
+ result
23
+ end
24
+
25
+ def update(*)
26
+ notify_observers(:before_update)
27
+ if result = super
28
+ notify_observers(:after_update)
29
+ end
30
+ result
31
+ end
32
+
33
+ def destroy(*)
34
+ notify_observers(:before_destroy)
35
+ if result = super
36
+ notify_observers(:after_destroy)
37
+ end
38
+ result
39
+ end
40
+ end
41
+ end
@@ -123,3 +123,7 @@ module ActiveRecord
123
123
  end
124
124
  end
125
125
  end
126
+
127
+ if defined?(ActionController) and defined?(ActionController::Caching::Sweeping)
128
+ require 'rails/observers/action_controller/caching/sweeper'
129
+ end
@@ -6,10 +6,8 @@ module Rails
6
6
  initializer "active_record.observer", :before => "active_record.set_configs" do |app|
7
7
  ActiveSupport.on_load(:active_record) do
8
8
  require "rails/observers/activerecord/active_record"
9
-
10
- if observers = app.config.respond_to?(:active_record) && app.config.active_record.delete(:observers)
11
- send :observers=, observers
12
- end
9
+ observers = app.config.active_record.delete(:observers)
10
+ self.observers = observers if observers
13
11
  end
14
12
  end
15
13
 
@@ -19,14 +17,32 @@ module Rails
19
17
  end
20
18
  end
21
19
 
20
+ initializer "active_resource.observer" do |app|
21
+ ActiveSupport.on_load(:active_resource) do
22
+ require 'rails/observers/active_resource/observing'
23
+
24
+ prepend ActiveResource::Observing
25
+ end
26
+ end
27
+
22
28
  config.after_initialize do |app|
23
29
  ActiveSupport.on_load(:active_record) do
24
- ActiveRecord::Base.instantiate_observers
25
-
26
- ActionDispatch::Reloader.to_prepare do
30
+ # Rails 5.1 forward-compat. AD::R is deprecated to AS::R in Rails 5.
31
+ reloader = defined?(ActiveSupport::Reloader) ? ActiveSupport::Reloader : ActionDispatch::Reloader
32
+ reloader.to_prepare do
27
33
  ActiveRecord::Base.instantiate_observers
28
34
  end
29
35
  end
36
+
37
+ ActiveSupport.on_load(:active_resource) do
38
+ self.instantiate_observers
39
+
40
+ # Rails 5.1 forward-compat. AD::R is deprecated to AS::R in Rails 5.
41
+ reloader = defined?(ActiveSupport::Reloader) ? ActiveSupport::Reloader : ActionDispatch::Reloader
42
+ reloader.to_prepare do
43
+ ActiveResource::Base.instantiate_observers
44
+ end
45
+ end
30
46
  end
31
47
  end
32
48
  end
@@ -1,5 +1,5 @@
1
1
  module Rails
2
2
  module Observers
3
- VERSION = "0.1.2"
3
+ VERSION = "0.1.4"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-observers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rafael Mendonça França
@@ -9,104 +9,118 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-19 00:00:00.000000000 Z
12
+ date: 2017-06-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - ~>
18
+ - - ">="
19
19
  - !ruby/object:Gem::Version
20
20
  version: '4.0'
21
21
  type: :runtime
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
- - - ~>
25
+ - - ">="
26
26
  - !ruby/object:Gem::Version
27
27
  version: '4.0'
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: minitest
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
- - - '>='
32
+ - - ">="
33
33
  - !ruby/object:Gem::Version
34
34
  version: '3'
35
35
  type: :development
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
- - - '>='
39
+ - - ">="
40
40
  - !ruby/object:Gem::Version
41
41
  version: '3'
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: railties
44
44
  requirement: !ruby/object:Gem::Requirement
45
45
  requirements:
46
- - - ~>
46
+ - - ">="
47
47
  - !ruby/object:Gem::Version
48
48
  version: '4.0'
49
49
  type: :development
50
50
  prerelease: false
51
51
  version_requirements: !ruby/object:Gem::Requirement
52
52
  requirements:
53
- - - ~>
53
+ - - ">="
54
54
  - !ruby/object:Gem::Version
55
55
  version: '4.0'
56
56
  - !ruby/object:Gem::Dependency
57
57
  name: activerecord
58
58
  requirement: !ruby/object:Gem::Requirement
59
59
  requirements:
60
- - - ~>
60
+ - - ">="
61
61
  - !ruby/object:Gem::Version
62
62
  version: '4.0'
63
63
  type: :development
64
64
  prerelease: false
65
65
  version_requirements: !ruby/object:Gem::Requirement
66
66
  requirements:
67
- - - ~>
67
+ - - ">="
68
68
  - !ruby/object:Gem::Version
69
69
  version: '4.0'
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: actionmailer
72
72
  requirement: !ruby/object:Gem::Requirement
73
73
  requirements:
74
- - - ~>
74
+ - - ">="
75
75
  - !ruby/object:Gem::Version
76
76
  version: '4.0'
77
77
  type: :development
78
78
  prerelease: false
79
79
  version_requirements: !ruby/object:Gem::Requirement
80
80
  requirements:
81
- - - ~>
81
+ - - ">="
82
82
  - !ruby/object:Gem::Version
83
83
  version: '4.0'
84
84
  - !ruby/object:Gem::Dependency
85
85
  name: actionpack
86
86
  requirement: !ruby/object:Gem::Requirement
87
87
  requirements:
88
- - - ~>
88
+ - - ">="
89
89
  - !ruby/object:Gem::Version
90
90
  version: '4.0'
91
91
  type: :development
92
92
  prerelease: false
93
93
  version_requirements: !ruby/object:Gem::Requirement
94
94
  requirements:
95
- - - ~>
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '4.0'
98
+ - !ruby/object:Gem::Dependency
99
+ name: activeresource
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '4.0'
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
96
110
  - !ruby/object:Gem::Version
97
111
  version: '4.0'
98
112
  - !ruby/object:Gem::Dependency
99
113
  name: sqlite3
100
114
  requirement: !ruby/object:Gem::Requirement
101
115
  requirements:
102
- - - ~>
116
+ - - ">="
103
117
  - !ruby/object:Gem::Version
104
118
  version: '1.3'
105
119
  type: :development
106
120
  prerelease: false
107
121
  version_requirements: !ruby/object:Gem::Requirement
108
122
  requirements:
109
- - - ~>
123
+ - - ">="
110
124
  - !ruby/object:Gem::Version
111
125
  version: '1.3'
112
126
  description: Rails observer (removed from core in Rails 4.0)
@@ -117,12 +131,8 @@ executables: []
117
131
  extensions: []
118
132
  extra_rdoc_files: []
119
133
  files:
120
- - .gitignore
121
- - .travis.yml
122
- - Gemfile
123
134
  - LICENSE
124
135
  - README.md
125
- - Rakefile
126
136
  - lib/generators/active_record/observer/observer_generator.rb
127
137
  - lib/generators/active_record/observer/templates/observer.rb
128
138
  - lib/generators/rails/observer/USAGE
@@ -131,37 +141,21 @@ files:
131
141
  - lib/generators/test_unit/observer/templates/unit_test.rb
132
142
  - lib/rails-observers.rb
133
143
  - lib/rails/observers/action_controller/caching.rb
144
+ - lib/rails/observers/action_controller/caching/sweeper.rb
134
145
  - lib/rails/observers/action_controller/caching/sweeping.rb
135
146
  - lib/rails/observers/active_model.rb
136
147
  - lib/rails/observers/active_model/active_model.rb
137
148
  - lib/rails/observers/active_model/observer_array.rb
138
149
  - lib/rails/observers/active_model/observing.rb
150
+ - lib/rails/observers/active_resource/observing.rb
139
151
  - lib/rails/observers/activerecord/active_record.rb
140
152
  - lib/rails/observers/activerecord/base.rb
141
153
  - lib/rails/observers/activerecord/observer.rb
142
154
  - lib/rails/observers/railtie.rb
143
155
  - lib/rails/observers/version.rb
144
- - rails-observers.gemspec
145
- - rails-observers.gemspec.erb
146
- - test/configuration_test.rb
147
- - test/console_test.rb
148
- - test/fixtures/developers.yml
149
- - test/fixtures/minimalistics.yml
150
- - test/fixtures/topics.yml
151
- - test/generators/generators_test_helper.rb
152
- - test/generators/namespaced_generators_test.rb
153
- - test/generators/observer_generator_test.rb
154
- - test/helper.rb
155
- - test/isolation/abstract_unit.rb
156
- - test/lifecycle_test.rb
157
- - test/models/observers.rb
158
- - test/observer_array_test.rb
159
- - test/observing_test.rb
160
- - test/rake_test.rb
161
- - test/sweeper_test.rb
162
- - test/transaction_callbacks_test.rb
163
156
  homepage: https://github.com/rails/rails-observers
164
- licenses: []
157
+ licenses:
158
+ - MIT
165
159
  metadata: {}
166
160
  post_install_message:
167
161
  rdoc_options: []
@@ -169,36 +163,19 @@ require_paths:
169
163
  - lib
170
164
  required_ruby_version: !ruby/object:Gem::Requirement
171
165
  requirements:
172
- - - '>='
166
+ - - ">="
173
167
  - !ruby/object:Gem::Version
174
- version: '0'
168
+ version: 2.2.2
175
169
  required_rubygems_version: !ruby/object:Gem::Requirement
176
170
  requirements:
177
- - - '>='
171
+ - - ">="
178
172
  - !ruby/object:Gem::Version
179
173
  version: '0'
180
174
  requirements: []
181
175
  rubyforge_project:
182
- rubygems_version: 2.0.2
176
+ rubygems_version: 2.6.12
183
177
  signing_key:
184
178
  specification_version: 4
185
179
  summary: ActiveModel::Observer, ActiveRecord::Observer and ActionController::Caching::Sweeper
186
180
  extracted from Rails.
187
- test_files:
188
- - test/configuration_test.rb
189
- - test/console_test.rb
190
- - test/fixtures/developers.yml
191
- - test/fixtures/minimalistics.yml
192
- - test/fixtures/topics.yml
193
- - test/generators/generators_test_helper.rb
194
- - test/generators/namespaced_generators_test.rb
195
- - test/generators/observer_generator_test.rb
196
- - test/helper.rb
197
- - test/isolation/abstract_unit.rb
198
- - test/lifecycle_test.rb
199
- - test/models/observers.rb
200
- - test/observer_array_test.rb
201
- - test/observing_test.rb
202
- - test/rake_test.rb
203
- - test/sweeper_test.rb
204
- - test/transaction_callbacks_test.rb
181
+ test_files: []