Skip to content

Commit 31d5383

Browse files
Stop including modules directly in example class
1 parent 47ec5d2 commit 31d5383

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

spec/unit/view_helpers/display_helper_spec.rb

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,29 @@
55
require 'active_admin/view_helpers/method_or_proc_helper'
66

77
RSpec.describe ActiveAdmin::ViewHelpers::DisplayHelper do
8-
include ActiveAdmin::ViewHelpers::ActiveAdminApplicationHelper
9-
include ActiveAdmin::ViewHelpers::AutoLinkHelper
10-
include ActiveAdmin::ViewHelpers::DisplayHelper
11-
include MethodOrProcHelper
12-
include ActionView::Helpers::UrlHelper
13-
include ActionView::Helpers::TranslationHelper
14-
include ActionView::Helpers::SanitizeHelper
15-
16-
def active_admin_namespace
17-
active_admin_application.namespaces[:admin]
8+
let(:view_klass) do
9+
Class.new(ActionView::Base) do
10+
include ActiveAdmin::ViewHelpers::ActiveAdminApplicationHelper
11+
include ActiveAdmin::ViewHelpers::AutoLinkHelper
12+
include ActiveAdmin::ViewHelpers::DisplayHelper
13+
include MethodOrProcHelper
14+
include ActionView::Helpers::UrlHelper
15+
include ActionView::Helpers::TranslationHelper
16+
include ActionView::Helpers::SanitizeHelper
17+
end
1818
end
1919

20-
def authorized?(*)
21-
true
22-
end
20+
let(:active_admin_namespace){ view.active_admin_application.namespaces[:admin] }
2321

24-
def url_options
25-
{ locale: nil }
26-
end
22+
let(:view) { mock_action_view(view_klass) }
2723

28-
let(:displayed_name) { display_name(resource) }
24+
let(:displayed_name) { view.display_name(resource) }
2925

3026
before do
27+
allow(view).to receive(:authorized?).and_return(true)
28+
allow(view).to receive(:active_admin_namespace).and_return(active_admin_namespace)
29+
allow(view).to receive(:url_options).and_return(locale: nil)
30+
3131
load_resources do
3232
ActiveAdmin.register(User)
3333
ActiveAdmin.register(Post){ belongs_to :user, optional: true }
@@ -106,7 +106,7 @@ def url_options
106106
it "should default to `to_s`" do
107107
result = resource.to_s
108108

109-
expect(displayed_name).to eq sanitize(result)
109+
expect(displayed_name).to eq view.sanitize(result)
110110
end
111111
end
112112

@@ -143,26 +143,26 @@ class ThisModel
143143

144144
describe '#format_attribute' do
145145
it 'calls the provided block to format the value' do
146-
value = format_attribute double(foo: 2), ->r { r.foo + 1 }
146+
value = view.format_attribute double(foo: 2), ->r { r.foo + 1 }
147147

148148
expect(value).to eq '3'
149149
end
150150

151151
it 'finds values as methods' do
152-
value = format_attribute double(name: 'Joe'), :name
152+
value = view.format_attribute double(name: 'Joe'), :name
153153

154154
expect(value).to eq 'Joe'
155155
end
156156

157157
it 'finds values from hashes' do
158-
value = format_attribute({id: 100}, :id)
158+
value = view.format_attribute({id: 100}, :id)
159159

160160
expect(value).to eq '100'
161161
end
162162

163163
[1, 1.2, :a_symbol].each do |val|
164164
it "calls to_s to format the value of type #{val.class}" do
165-
value = format_attribute double(foo: val), :foo
165+
value = view.format_attribute double(foo: val), :foo
166166

167167
expect(value).to eq val.to_s
168168
end
@@ -171,39 +171,39 @@ class ThisModel
171171
it 'localizes dates' do
172172
date = Date.parse '2016/02/28'
173173

174-
value = format_attribute double(date: date), :date
174+
value = view.format_attribute double(date: date), :date
175175

176176
expect(value).to eq 'February 28, 2016'
177177
end
178178

179179
it 'localizes times' do
180180
time = Time.parse '2016/02/28 9:34 PM'
181181

182-
value = format_attribute double(time: time), :time
182+
value = view.format_attribute double(time: time), :time
183183

184184
expect(value).to eq 'February 28, 2016 21:34'
185185
end
186186

187187
it 'uses a display_name method for arbitrary objects' do
188188
object = double to_s: :wrong, display_name: :right
189189

190-
value = format_attribute double(object: object), :object
190+
value = view.format_attribute double(object: object), :object
191191

192192
expect(value).to eq 'right'
193193
end
194194

195195
it 'auto-links ActiveRecord records by association' do
196196
post = Post.create! author: User.new
197197

198-
value = format_attribute post, :author
198+
value = view.format_attribute post, :author
199199

200200
expect(value).to match /<a href="\/admin\/users\/\d+"> <\/a>/
201201
end
202202

203203
it 'auto-links ActiveRecord records & uses a display_name method' do
204204
post = Post.create! author: User.new(first_name: 'A', last_name: 'B')
205205

206-
value = format_attribute post, :author
206+
value = view.format_attribute post, :author
207207

208208
expect(value).to match /<a href="\/admin\/users\/\d+">A B<\/a>/
209209
end
@@ -213,7 +213,7 @@ class ThisModel
213213
it 'calls status_tag for boolean values' do
214214
post = Post.new starred: true
215215

216-
value = format_attribute post, :starred
216+
value = view.format_attribute post, :starred
217217

218218
expect(value.to_s).to eq "<span class=\"status_tag yes\">Yes</span>\n"
219219
end
@@ -226,9 +226,9 @@ class ThisModel
226226
post.define_singleton_method(:false_method) do
227227
false
228228
end
229-
true_value = format_attribute post, :true_method
229+
true_value = view.format_attribute post, :true_method
230230
expect(true_value.to_s).to eq "<span class=\"status_tag yes\">Yes</span>\n"
231-
false_value = format_attribute post, :false_method
231+
false_value = view.format_attribute post, :false_method
232232
expect(false_value.to_s).to eq "<span class=\"status_tag no\">No</span>\n"
233233
end
234234

0 commit comments

Comments
 (0)