Skip to content

Commit f94e76e

Browse files
committed
Use nested resources.
Instead of new "All Links" link, just use the label. Get these tests passing once again now that we're using nested resource routes.
1 parent 43b5789 commit f94e76e

File tree

7 files changed

+50
-53
lines changed

7 files changed

+50
-53
lines changed

app/controllers/microcosm_links_controller.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ class MicrocosmLinksController < ApplicationController
88
authorize_resource
99

1010
def index
11-
return "missing parameter microcosm_id" unless params.key?(:microcosm_id)
12-
13-
@microcosm_id = params[:microcosm_id]
14-
@links = MicrocosmLink.where(:microcosm_id => @microcosm_id)
11+
@microcosm = Microcosm.friendly.find(params[:microcosm_id])
12+
@links = @microcosm.microcosm_links
1513
end
1614

1715
def new
1816
return "missing parameter microcosm_id" unless params.key?(:microcosm_id)
1917

18+
@microcosm = Microcosm.friendly.find(params[:microcosm_id])
2019
@title = t ".title"
2120
@link = MicrocosmLink.new
2221
@link.microcosm_id = params[:microcosm_id]
@@ -25,7 +24,8 @@ def new
2524
def edit; end
2625

2726
def create
28-
@link = MicrocosmLink.new(link_params)
27+
@microcosm = Microcosm.friendly.find(params[:microcosm_id])
28+
@link = @microcosm.microcosm_links.build(link_params)
2929
if @link.save
3030
response.set_header("link_id", @link.id) # for testing
3131
redirect_to @link.microcosm, :notice => t(".success")

app/views/microcosm_links/_form.html.erb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
<%= bootstrap_form_for @link do |form| %>
2-
<%= form.hidden_field :microcosm_id, :value => @link.microcosm_id %>
1+
<%= bootstrap_form_for [@microcosm, @link] do |form| %>
32
<%= form.text_field :site %>
43
<%= form.text_field :url %>
54
<%= form.primary %>

app/views/microcosm_links/index.html.erb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<% content_for :heading do %>
22
<h1><%= t(".title") %></h1>
3-
<ul class='secondary-actions clearfix'>
4-
<li><%= link_to image_tag("new.png", :class => "small_icon", :border => 0) + t(".new"), new_microcosm_link_path(:params => { :microcosm_id => @microcosm_id }) %></li>
5-
</ul>
3+
<nav class="secondary-actions">
4+
<ul class='clearfix'>
5+
<li><%= link_to image_tag("new.png", :class => "small_icon", :border => 0) + t(".new"), new_microcosm_microcosm_link_path(@microcosm) %></li>
6+
</ul>
7+
</nav>
68
<% end %>
79

810
<% if !@links.empty? %>

app/views/microcosms/show.html.erb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,20 @@
3030
<%= auto_link @microcosm.description %>
3131
</p>
3232
<div class="d-flex">
33-
<label><%= t(".links") %></label>
33+
<label>
34+
<% if current_user == @microcosm.organizer %>
35+
<%= link_to t(".links"), microcosm_microcosm_links_path(@microcosm) %>
36+
<% else %>
37+
<%= t(".links") %>
38+
<% end %>
39+
</label>
3440
<ul class="ps-2 breadcrumb">
3541
<% @microcosm.microcosm_links.each do |link| %>
3642
<li class="breadcrumb-item">
3743
<a href="<%= link.url %>"><%= link.site %></a>
3844
</li>
3945
<% end %>
4046
</ul>
41-
<% if current_user %>
42-
<% if current_user == @microcosm.organizer %>
43-
<%= link_to t(".all_links"), microcosm_links_path(:params => { :microcosm_id => @microcosm.id }) %>
44-
<% end %>
45-
<% end %>
4647
</div>
4748
<div>
4849
<label><%= t(".organizer") %></label>

config/locales/en.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1580,7 +1580,6 @@ en:
15801580
new:
15811581
title: "New Microcosm"
15821582
show:
1583-
all_links: "All Links"
15841583
edit: "Edit"
15851584
header_title: "Microcosm"
15861585
links: "Links"

config/routes.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,10 @@
327327
end
328328

329329
# microcosms
330-
resources :microcosms
331-
resources :microcosm_links, :except => [:show]
330+
resources :microcosms do
331+
resources :microcosm_links, :only => [:create, :index, :new]
332+
end
333+
resources :microcosm_links, :only => [:destroy, :edit, :update]
332334

333335
# errors
334336
match "/403", :to => "errors#forbidden", :via => :all

test/controllers/microcosm_links_controller_test.rb

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ class MicrocosmLinksControllerTest < ActionDispatch::IntegrationTest
99

1010
def test_routes
1111
assert_routing(
12-
{ :path => "/microcosm_links", :method => :get },
13-
{ :controller => "microcosm_links", :action => "index" }
12+
{ :path => "/microcosms/foo/microcosm_links", :method => :get },
13+
{ :controller => "microcosm_links", :action => "index", :microcosm_id => "foo" }
1414
)
1515
assert_routing(
1616
{ :path => "/microcosm_links/1/edit", :method => :get },
@@ -21,12 +21,12 @@ def test_routes
2121
{ :controller => "microcosm_links", :action => "update", :id => "1" }
2222
)
2323
assert_routing(
24-
{ :path => "/microcosm_links/new", :method => :get },
25-
{ :controller => "microcosm_links", :action => "new" }
24+
{ :path => "/microcosms/foo/microcosm_links/new", :method => :get },
25+
{ :controller => "microcosm_links", :action => "new", :microcosm_id => "foo" }
2626
)
2727
assert_routing(
28-
{ :path => "/microcosm_links", :method => :post },
29-
{ :controller => "microcosm_links", :action => "create" }
28+
{ :path => "/microcosms/foo/microcosm_links", :method => :post },
29+
{ :controller => "microcosm_links", :action => "create", :microcosm_id => "foo" }
3030
)
3131
end
3232

@@ -35,7 +35,7 @@ def test_index_get
3535
m = create(:microcosm)
3636
link = create(:microcosm_link, :microcosm_id => m.id)
3737
# act
38-
get microcosm_links_path(:params => { :microcosm_id => m.id })
38+
get microcosm_microcosm_links_path(m.id)
3939
# assert
4040
assert_response :success
4141
assert_template "index"
@@ -58,7 +58,7 @@ def test_update_as_non_organizer
5858
link = create(:microcosm_link)
5959
session_for(create(:user))
6060
# act
61-
put microcosm_link_path(link), :params => { :microcosm_link => link.as_json }, :xhr => true
61+
put microcosm_link_path link, :microcosm_link => link
6262
# assert
6363
assert_redirected_to :controller => :errors, :action => :forbidden
6464
end
@@ -120,11 +120,13 @@ def controller_mock.render(_partial)
120120
def test_new_no_login
121121
# Make sure that you are redirected to the login page when you
122122
# are not logged in
123+
# arrange
124+
m = create(:microcosm)
123125
# act
124-
get new_microcosm_link_path
126+
get new_microcosm_microcosm_link_path(m)
125127
# assert
126128
assert_response :redirect
127-
assert_redirected_to login_path(:referer => new_microcosm_link_path)
129+
assert_redirected_to login_path(:referer => new_microcosm_microcosm_link_path(m))
128130
end
129131

130132
def test_new_form
@@ -133,35 +135,39 @@ def test_new_form
133135
m = create(:microcosm)
134136
session_for(m.organizer)
135137
# act
136-
get new_microcosm_link_path(:params => { :microcosm_id => m.id })
138+
get new_microcosm_microcosm_link_path(m)
137139
# assert
138140
assert_response :success
139141
assert_select "div.content-heading", :count => 1 do
140142
assert_select "h1", :text => /Microcosm Link/, :count => 1
141143
end
144+
action = microcosm_microcosm_links_path(m)
142145
assert_select "div#content", :count => 1 do
143-
assert_select "form[action='/microcosm_links'][method=post]", :count => 1 do
146+
assert_select "form[action='#{action}'][method=post]", :count => 1 do
144147
assert_select "input#microcosm_link_site[name='microcosm_link[site]']", :count => 1
145148
assert_select "input#microcosm_link_url[name='microcosm_link[url]']", :count => 1
146-
assert_select "input", :count => 5
149+
assert_select "input", :count => 4
147150
end
148151
end
149152
end
150153

151154
def test_create_when_save_works
152155
# arrange
153156
m = create(:microcosm)
154-
link_orig = create(:microcosm_link, :microcosm_id => m.id)
157+
link_orig = create(:microcosm_link, :microcosm => m)
158+
form = link_orig.attributes.except("id", "created_at", "updated_at")
155159
session_for(m.organizer)
156160

157161
# act
158162
link_new_id = nil
159163
assert_difference "MicrocosmLink.count", 1 do
160-
post microcosm_links_url, :params => { :microcosm_link => link_orig.as_json }, :xhr => true
164+
post microcosm_microcosm_links_path m.id, :microcosm_link => form
161165
link_new_id = @response.headers["link_id"]
162166
end
163167

164168
# assert
169+
# Not sure what's going on with this assigns magic.
170+
# assert_redirected_to "/microcosm/#{assigns(:microcosm_link).id}"
165171
assert_equal I18n.t("microcosm_links.create.success"), flash[:notice]
166172
link_new = MicrocosmLink.find_by(:id => link_new_id)
167173
# Assign the id m_new to m_orig, so we can do an equality test easily.
@@ -171,29 +177,17 @@ def test_create_when_save_works
171177

172178
def test_create_when_save_fails
173179
# arrange
174-
session_for(create(:user))
175-
link = create(:microcosm_link)
176-
link = link.attributes.except("id", "created_at", "updated_at", "slug")
177-
178-
mock_microcosm_link = Minitest::Mock.new
179-
mock_microcosm_link.expect :save, false
180-
181-
# We're going to stub render on this instance.
182-
controller_prime = MicrocosmLinksController.new
180+
mic = create(:microcosm)
181+
session_for(mic.organizer)
182+
link = build(:microcosm_link, :microcosm => mic, :url => "invalid url")
183+
form = link.attributes.except("id", "created_at", "updated_at")
183184

184185
# act and assert
185-
MicrocosmLinksController.stub :new, controller_prime do
186-
controller_prime.stub :render, "" do
187-
MicrocosmLink.stub :new, mock_microcosm_link do
188-
assert_difference "MicrocosmLink.count", 0 do
189-
post microcosm_links_path, :params => { :microcosm_link => link.as_json }, :xhr => true
190-
end
191-
end
192-
end
186+
assert_no_difference "MicrocosmLink.count", 0 do
187+
post microcosm_microcosm_links_path :microcosm_link => form, :microcosm_id => mic.id
193188
end
194189

195-
# assert_mock mock_microcosm
196-
# assert_mock render_mock
190+
assert_template :new
197191
end
198192

199193
def test_delete

0 commit comments

Comments
 (0)