8000 Support all Rails 4 controller callbacks #3872 · stephancom/activeadmin@8e579d4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8e579d4

Browse files
committed
Support all Rails 4 controller callbacks activeadmin#3872
Add prepend (and append) filters to config. By supporting Rails 4’s actions, we need to hardcode 26 method names which seems too much. Instead, I’ve implemented class load time method enumeration, and ensure availability by the test.
1 parent 28e8a8f commit 8e579d4

File tree

2 files changed

+27
-29
lines changed

2 files changed

+27
-29
lines changed

lib/active_admin/application.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ def routes(rails_router)
216216
# Example usage:
217217
# ActiveAdmin.before_filter :authenticate_admin!
218218
#
219-
%w(before_filter skip_before_filter after_filter skip_after_filter around_filter skip_filter).each do |name|
219+
AbstractController::Callbacks::ClassMethods.public_instance_methods.
220+
select { |m| m.match(/(filter|action)/) }.each do |name|
220221
define_method name do |*args, &block|
221222
controllers_for_filters.each do |controller|
222223
controller.public_send name, *args, &block

spec/unit/controller_filters_spec.rb

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,30 @@
1212
]
1313
end
1414

15-
it 'before_filter' do
16-
controllers.each{ |c| expect(c).to receive(:before_filter).and_return(true) }
17-
application.before_filter :my_filter, only: :show
18-
end
19-
20-
it 'skip_before_filter' do
21-
controllers.each{ |c| expect(c).to receive(:skip_before_filter).and_return(true) }
22-
application.skip_before_filter :my_filter, only: :show
23-
end
24-
25-
it 'after_filter' do
26-
controllers.each{ |c| expect(c).to receive(:after_filter).and_return(true) }
27-
application.after_filter :my_filter, only: :show
28-
end
29-
30-
it 'skip after_filter' do
31-
controllers.each{ |c| expect(c).to receive(:skip_after_filter).and_return(true) }
32-
application.skip_after_filter :my_filter, only: :show
33-
end
34-
35-
it 'around_filter' do
36-
controllers.each{ |c| expect(c).to receive(:around_filter).and_return(true) }
37-
application.around_filter :my_filter, only: :show
38-
end
39-
40-
it 'skip_filter' do
41-
controllers.each{ |c| expect(c).to receive(:skip_filter).and_return(true) }
42-
application.skip_filter :my_filter, only: :show
15+
expected_actions = (
16+
prefixes = %w(skip append prepend) << nil
17+
positions = %w(before around after)
18+
suffixes = %w(filter)
19+
base = %w(skip_filter)
20+
if Rails::VERSION::MAJOR >= 4
21+
suffixes += %w(action)
22+
base += %w(skip_action_callback)
23+
end
24+
25+
prefixes.each_with_object(base) do |prefix, stack|
26+
positions.each do |position|
27+
suffixes.each do |suffix|
28+
stack << [prefix, position, suffix].compact.join("_").to_sym
29+
end
30+
end
31+
end
32+
)
33+
34+
expected_actions.each do |action|
35+
it action do
36+
args = [:my_filter, { only: :show }]
37+
contr 4981 ollers.each { |c| expect(c).to receive(action).with(args) }
38+
application.public_send action, args
39+
end
4340
end
4441
end

0 commit comments

Comments
 (0)
0