Skip to content

Commit c9e7f9a

Browse files
committed
add warning when redefining controller method
1 parent defc9ca commit c9e7f9a

File tree

2 files changed

+98
-97
lines changed

2 files changed

+98
-97
lines changed

lib/active_admin/resource_dsl.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def csv(options = {}, &block)
131131
# action.
132132
#
133133
def action(set, name, options = {}, &block)
134-
raise ArgumentError, "method #{name} already defined" if controller.method_defined?(name)
134+
warn "Warning: method `#{name}` already defined" if controller.method_defined?(name)
135135

136136
set << ControllerAction.new(name, options)
137137
title = options.delete(:title)

spec/unit/action_builder_spec.rb

Lines changed: 97 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,156 +1,157 @@
11
require 'rails_helper'
22

33
RSpec.describe 'defining actions from registration blocks', type: :controller do
4-
context 'when method with given name does not exist' do
5-
let(:klass){ Admin::PostsController }
4+
let(:klass){ Admin::PostsController }
65

7-
before do
8-
load_resources { action! }
6+
before do
7+
load_resources { action! }
98

10-
@controller = klass.new
11-
end
9+
@controller = klass.new
10+
end
1211

13-
describe 'creates a member action' do
14-
after(:each) do
15-
klass.clear_member_actions!
16-
end
12+
describe 'creates a member action' do
13+
after(:each) do
14+
klass.clear_member_actions!
15+
end
1716

18-
context 'with a block' do
19-
let(:action!) do
20-
ActiveAdmin.register Post do
21-
member_action :comment do
22-
# Do nothing
23-
end
17+
context 'with a block' do
18+
let(:action!) do
19+
ActiveAdmin.register Post do
20+
member_action :comment do
21+
# Do nothing
2422
end
2523
end
24+
end
2625

27-
it 'should create a new public instance method' do
28-
expect(klass.public_instance_methods.collect(&:to_s)).to include('comment')
29-
end
26+
it 'should create a new public instance method' do
27+
expect(klass.public_instance_methods.collect(&:to_s)).to include('comment')
28+
end
3029

31-
it 'should add itself to the member actions config' do
32-
expect(klass.active_admin_config.member_actions.size).to eq 1
33-
end
30+
it 'should add itself to the member actions config' do
31+
expect(klass.active_admin_config.member_actions.size).to eq 1
32+
end
3433

35-
it 'should create a new named route' do
36-
expect(Rails.application.routes.url_helpers.methods.collect(&:to_s)).to include('comment_admin_post_path')
37-
end
34+
it 'should create a new named route' do
35+
expect(Rails.application.routes.url_helpers.methods.collect(&:to_s)).to include('comment_admin_post_path')
3836
end
37+
end
3938

40-
context 'without a block' do
41-
let(:action!) do
42-
ActiveAdmin.register Post do
43-
member_action :comment
44-
end
39+
context 'without a block' do
40+
let(:action!) do
41+
ActiveAdmin.register Post do
42+
member_action :comment
4543
end
44+
end
4645

47-
it 'should still generate a new empty action' do
48-
expect(klass.public_instance_methods.collect(&:to_s)).to include('comment')
49-
end
46+
it 'should still generate a new empty action' do
47+
expect(klass.public_instance_methods.collect(&:to_s)).to include('comment')
5048
end
49+
end
5150

52-
context 'with :title' do
53-
let(:action!) do
54-
ActiveAdmin.register Post do
55-
member_action :comment, title: 'My Awesome Comment' do
56-
render json: {a: 2}
57-
end
51+
context 'with :title' do
52+
let(:action!) do
53+
ActiveAdmin.register Post do
54+
member_action :comment, title: 'My Awesome Comment' do
55+
render json: {a: 2}
5856
end
5957
end
58+
end
6059

61-
it 'sets the page title' do
62-
params = {id: 1}
63-
params = {params: params} if ActiveAdmin::Dependency.rails5?
64-
get :comment, params
60+
it 'sets the page title' do
61+
params = {id: 1}
62+
params = {params: params} if ActiveAdmin::Dependency.rails5?
63+
get :comment, params
6564

66-
expect(controller.instance_variable_get(:@page_title)).to eq 'My Awesome Comment'
67-
end
65+
expect(controller.instance_variable_get(:@page_title)).to eq 'My Awesome Comment'
6866
end
6967
end
68+
end
7069

71-
describe 'creates a collection action' do
72-
after(:each) do
73-
klass.clear_collection_actions!
74-
end
70+
describe 'creates a collection action' do
71+
after(:each) do
72+
klass.clear_collection_actions!
73+
end
7574

76-
context 'with a block' do
77-
let(:action!) do
78-
ActiveAdmin.register Post do
79-
collection_action :comments do
80-
# Do nothing
81-
end
75+
context 'with a block' do
76+
let(:action!) do
77+
ActiveAdmin.register Post do
78+
collection_action :comments do
79+
# Do nothing
8280
end
8381
end
82+
end
8483

85-
it 'should create a public instance method' do
86-
expect(klass.public_instance_methods.collect(&:to_s)).to include('comments')
87-
end
84+
it 'should create a public instance method' do
85+
expect(klass.public_instance_methods.collect(&:to_s)).to include('comments')
86+
end
8887

89-
it 'should add itself to the member actions config' do
90-
expect(klass.active_admin_config.collection_actions.size).to eq 1
91-
end
88+
it 'should add itself to the member actions config' do
89+
expect(klass.active_admin_config.collection_actions.size).to eq 1
90+
end
9291

93-
it 'should create a named route' do
94-
expect(Rails.application.routes.url_helpers.methods.collect(&:to_s)).to include('comments_admin_posts_path')
95-
end
92+
it 'should create a named route' do
93+
expect(Rails.application.routes.url_helpers.methods.collect(&:to_s)).to include('comments_admin_posts_path')
9694
end
95+
end
9796

98-
context 'without a block' do
99-
let(:action!) do
100-
ActiveAdmin.register Post do
101-
collection_action :comments
102-
end
97+
context 'without a block' do
98+
let(:action!) do
99+
ActiveAdmin.register Post do
100+
collection_action :comments
103101
end
102+
end
104103

105-
it 'should still generate a new empty action' do
106-
expect(klass.public_instance_methods.collect(&:to_s)).to include('comments')
107-
end
104+
it 'should still generate a new empty action' do
105+
expect(klass.public_instance_methods.collect(&:to_s)).to include('comments')
108106
end
107+
end
109108

110-
context 'with :title' do
111-
let(:action!) do
112-
ActiveAdmin.register Post do
113-
collection_action :comments, title: 'My Awesome Comments' do
114-
render json: {a: 2}
115-
end
109+
context 'with :title' do
110+
let(:action!) do
111+
ActiveAdmin.register Post do
112+
collection_action :comments, title: 'My Awesome Comments' do
113+
render json: {a: 2}
116114
end
117115
end
116+
end
118117

119-
it 'sets the page title' do
120-
get :comments
118+
it 'sets the page title' do
119+
get :comments
121120

122-
expect(controller.instance_variable_get(:@page_title)).to eq 'My Awesome Comments'
123-
end
121+
expect(controller.instance_variable_get(:@page_title)).to eq 'My Awesome Comments'
124122
end
125123
end
126124
end
127125

128126
context 'when method with given name is already defined' do
127+
around :each do |example|
128+
original_stderr = $stderr
129+
$stderr = StringIO.new
130+
example.run
131+
$stderr = original_stderr
132+
end
133+
129134
describe 'defining member action' do
130-
subject do
131-
load_resources do
132-
ActiveAdmin.register Post do
133-
member_action :process
134-
end
135+
let :action! do
136+
ActiveAdmin.register Post do
137+
member_action :process
135138
end
136139
end
137140

138-
it 'raises error' do
139-
expect { subject }.to raise_error(ArgumentError)
141+
it 'writes warning to $stderr' do
142+
expect($stderr.string).to include('Warning: method `process` already defined')
140143
end
141144
end
142145

143146
describe 'defining collection action' do
144-
subject do
145-
load_resources do
146-
ActiveAdmin.register Post do
147-
collection_action :process
148-
end
147+
let :action! do
148+
ActiveAdmin.register Post do
149+
collection_action :process
149150
end
150151
end
151152

152-
it 'raises error' do
153-
expect { subject }.to raise_error(ArgumentError)
153+
it 'writes warning to $stderr' do
154+
expect($stderr.string).to include('Warning: method `process` already defined')
154155
end
155156
end
156157
end

0 commit comments

Comments
 (0)