|
1 | 1 | require 'rails_helper'
|
2 | 2 |
|
3 | 3 | 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 } |
6 | 5 |
|
7 |
| - before do |
8 |
| - load_resources { action! } |
| 6 | + before do |
| 7 | + load_resources { action! } |
9 | 8 |
|
10 |
| - @controller = klass.new |
11 |
| - end |
| 9 | + @controller = klass.new |
| 10 | + end |
12 | 11 |
|
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 |
17 | 16 |
|
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 |
24 | 22 | end
|
25 | 23 | end
|
| 24 | + end |
26 | 25 |
|
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 |
30 | 29 |
|
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 |
34 | 33 |
|
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') |
38 | 36 | end
|
| 37 | + end |
39 | 38 |
|
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 |
45 | 43 | end
|
| 44 | + end |
46 | 45 |
|
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') |
50 | 48 | end
|
| 49 | + end |
51 | 50 |
|
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} |
58 | 56 | end
|
59 | 57 | end
|
| 58 | + end |
60 | 59 |
|
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 |
65 | 64 |
|
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' |
68 | 66 | end
|
69 | 67 | end
|
| 68 | + end |
70 | 69 |
|
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 |
75 | 74 |
|
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 |
82 | 80 | end
|
83 | 81 | end
|
| 82 | + end |
84 | 83 |
|
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 |
88 | 87 |
|
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 |
92 | 91 |
|
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') |
96 | 94 | end
|
| 95 | + end |
97 | 96 |
|
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 |
103 | 101 | end
|
| 102 | + end |
104 | 103 |
|
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') |
108 | 106 | end
|
| 107 | + end |
109 | 108 |
|
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} |
116 | 114 | end
|
117 | 115 | end
|
| 116 | + end |
118 | 117 |
|
119 |
| - it 'sets the page title' do |
120 |
| - get :comments |
| 118 | + it 'sets the page title' do |
| 119 | + get :comments |
121 | 120 |
|
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' |
124 | 122 | end
|
125 | 123 | end
|
126 | 124 | end
|
127 | 125 |
|
128 | 126 | 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 | + |
129 | 134 | 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 |
135 | 138 | end
|
136 | 139 | end
|
137 | 140 |
|
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') |
140 | 143 | end
|
141 | 144 | end
|
142 | 145 |
|
143 | 146 | 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 |
149 | 150 | end
|
150 | 151 | end
|
151 | 152 |
|
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') |
154 | 155 | end
|
155 | 156 | end
|
156 | 157 | end
|
|
0 commit comments