Skip to content

Commit ebb8073

Browse files
committed
Move snippet controller to share namespace
1 parent 8407198 commit ebb8073

File tree

13 files changed

+86
-81
lines changed

13 files changed

+86
-81
lines changed

app/controllers/snippets_controller.rb renamed to app/controllers/share/snippets_controller.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class SnippetsController < ApplicationController
1+
class Share::SnippetsController < ApplicationController
22
before_action :feature_enabled!
33

44
# GET /snippets
@@ -32,7 +32,7 @@ def create
3232
@snippet = Snippet.new(snippet_params)
3333

3434
if @snippet.save
35-
redirect_to @snippet, notice: "Snippet was successfully created."
35+
redirect_to share_snippet_url(@snippet), notice: "Snippet was successfully created."
3636
else
3737
render :new, status: :unprocessable_entity
3838
end
@@ -43,7 +43,7 @@ def update
4343
@snippet = Snippet.find(params[:id])
4444
if @snippet.update(snippet_params)
4545
@snippet.attach_screenshot_from_base64(params[:screenshot]) if params[:screenshot]
46-
redirect_to @snippet, notice: "Snippet was successfully updated.", status: :see_other
46+
redirect_to share_snippet_url(@snippet), notice: "Snippet was successfully updated.", status: :see_other
4747
else
4848
render :edit, status: :unprocessable_entity
4949
end
@@ -53,7 +53,7 @@ def update
5353
def destroy
5454
@snippet = Snippet.find(params[:id])
5555
@snippet.destroy!
56-
redirect_to snippets_url, notice: "Snippet was successfully destroyed.", status: :see_other
56+
redirect_to share_snippets_url, notice: "Snippet was successfully destroyed.", status: :see_other
5757
end
5858

5959
private
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<%= render Pages::Header.new(title: "Edit snippet") %>
2+
<div class="section-content container py-gap">
3+
<%= render Share::Snippets::Form.new(snippet: @snippet) %>
4+
5+
<br>
6+
7+
<div>
8+
<%= link_to "Show this snippet", share_snippet_path(@snippet) %> |
9+
<%= link_to "Back to snippets", share_snippets_path %>
10+
</div>
11+
</div>

app/views/snippets/form.rb renamed to app/views/share/snippets/form.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class Snippets::Form < ApplicationComponent
1+
class Share::Snippets::Form < ApplicationComponent
22
include Phlex::Rails::Helpers::DOMID
33
include Phlex::Rails::Helpers::FormWith
44
include Phlex::Rails::Helpers::TurboFrameTag
@@ -12,7 +12,7 @@ def initialize(snippet:)
1212

1313
def view_template
1414
form_with(
15-
model: snippet,
15+
model: [:share, snippet],
1616
class: "grid-content",
1717
data: {
1818
controller: "snippet-preview snippet-screenshot",
@@ -117,9 +117,9 @@ def language_select_options
117117

118118
def form_path
119119
if snippet.persisted?
120-
edit_snippet_path(snippet)
120+
edit_share_snippet_path(snippet)
121121
else
122-
new_snippet_path
122+
new_share_snippet_path
123123
end
124124
end
125125
end

app/views/snippets/index.html.erb renamed to app/views/share/snippets/index.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<%= render Pages::Header.new(title: "Snippets") %>
22
<div id="snippets" class="section-content container py-gap">
33
<div class="flex">
4-
<%= link_to "New snippet", new_snippet_path, class: "button primary" %>
4+
<%= link_to "New snippet", new_share_snippet_path, class: "button primary" %>
55
</div>
66

77
<% @snippets.each do |snippet| %>
8-
<%= link_to snippet, class: "block" do %>
8+
<%= link_to share_snippet_path(snippet), class: "block" do %>
99
<% if snippet.screenshot.attached? %>
1010
<%= image_tag snippet.screenshot %>
1111
<% else %>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<%= render Pages::Header.new(title: "New Snippet") %>
22
<div class="section-content container py-gap">
3-
<%= render Snippets::Form.new(snippet: @snippet) %>
3+
<%= render Share::Snippets::Form.new(snippet: @snippet) %>
44

55
<br>
66

77
<div>
8-
<%= link_to "Back to snippets", snippets_path %>
8+
<%= link_to "Back to snippets", share_snippets_path %>
99
</div>
1010
</div>

app/views/snippets/show.html.erb renamed to app/views/share/snippets/show.html.erb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
<%= image_tag @snippet.screenshot if @snippet.screenshot.attached? %>
66

77
<div>
8-
<%= link_to "Edit this snippet", edit_snippet_path(@snippet) %> |
9-
<%= link_to "Back to snippets", snippets_path %>
8+
<%= link_to "Edit this snippet", edit_share_snippet_path(@snippet) %> |
9+
<%= link_to "Back to snippets", share_snippets_path %>
1010

11-
<%= button_to "Destroy this snippet", @snippet, method: :delete %>
11+
<%= button_to "Destroy this snippet", share_snippet_path(@snippet), method: :delete %>
1212
</div>
1313
</div>

app/views/snippets/edit.html.erb

Lines changed: 0 additions & 11 deletions
This file was deleted.

config/routes.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010
sitepress_root controller: :site
1111
sitepress_pages controller: :site
1212

13-
resources :snippets
13+
namespace :share do
14+
resources :snippets
15+
# resources :snippets do
16+
# resource :screenshot, only: [:new, :create], as: :screenshot
17+
# end
18+
end
1419

1520
resources :newsletters, only: [:index, :show]
1621

spec/requests/snippets_spec.rb renamed to spec/requests/share/snippets_spec.rb

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
Flipper.enable(:snippets, login_as_user)
77

88
FactoryBot.create(:snippet)
9-
get snippets_url
9+
get share_snippets_url
1010
expect(response).to be_successful
1111
end
1212
end
@@ -16,15 +16,15 @@
1616
Flipper.enable(:snippets, login_as_user)
1717

1818
snippet = FactoryBot.create(:snippet)
19-
get snippet_url(snippet)
19+
get share_snippet_url(snippet)
2020
expect(response).to be_successful
2121
end
2222
end
2323

2424
describe "GET /new" do
2525
it "renders a successful response" do
2626
Flipper.enable(:snippets, login_as_user)
27-
get new_snippet_url
27+
get new_share_snippet_url
2828
expect(response).to be_successful
2929
end
3030
end
@@ -33,7 +33,7 @@
3333
it "renders a successful response" do
3434
Flipper.enable(:snippets, login_as_user)
3535
snippet = FactoryBot.create(:snippet)
36-
get edit_snippet_url(snippet)
36+
get edit_share_snippet_url(snippet)
3737
expect(response).to be_successful
3838
end
3939
end
@@ -46,13 +46,13 @@
4646

4747
it "creates a new Snippet" do
4848
expect {
49-
post snippets_url, params: {snippet: {source: "puts \"Hello!\"", language: "ruby"}}
49+
post share_snippets_url, params: {snippet: {source: "puts \"Hello!\"", language: "ruby"}}
5050
}.to change(Snippet, :count).by(1)
5151
end
5252

5353
it "redirects to the created snippet" do
54-
post snippets_url, params: {snippet: {source: "puts \"Hello!\"", language: "ruby"}}
55-
expect(response).to redirect_to(snippet_url(Snippet.last))
54+
post share_snippets_url, params: {snippet: {source: "puts \"Hello!\"", language: "ruby"}}
55+
expect(response).to redirect_to(share_snippet_url(Snippet.last))
5656
end
5757
end
5858

@@ -63,12 +63,12 @@
6363

6464
it "does not create a new Snippet" do
6565
expect {
66-
post snippets_url, params: {snippet: {}}
66+
post share_snippets_url, params: {snippet: {}}
6767
}.to change(Snippet, :count).by(0)
6868
end
6969

7070
it "renders a response with 422 status (i.e. to display the 'new' template)" do
71-
post snippets_url, params: {snippet: {}}
71+
post share_snippets_url, params: {snippet: {}}
7272
expect(response).to have_http_status(:unprocessable_entity)
7373
end
7474
end
@@ -82,16 +82,16 @@
8282

8383
it "updates the requested snippet" do
8484
snippet = FactoryBot.create(:snippet)
85-
patch snippet_url(snippet), params: {snippet: {source: "puts \"Goodbye!\""}}
85+
patch share_snippet_url(snippet), params: {snippet: {source: "puts \"Goodbye!\""}}
8686
snippet.reload
8787
expect(snippet.source).to eq("puts \"Goodbye!\"")
8888
end
8989

9090
it "redirects to the snippet" do
9191
snippet = FactoryBot.create(:snippet)
92-
patch snippet_url(snippet), params: {snippet: {source: "puts \"Goodbye!\""}}
92+
patch share_snippet_url(snippet), params: {snippet: {source: "puts \"Goodbye!\""}}
9393
snippet.reload
94-
expect(response).to redirect_to(snippet_url(snippet))
94+
expect(response).to redirect_to(share_snippet_url(snippet))
9595
end
9696
end
9797

@@ -102,7 +102,7 @@
102102

103103
it "renders a response with 422 status (i.e. to display the 'edit' template)" do
104104
snippet = FactoryBot.create(:snippet)
105-
patch snippet_url(snippet), params: {snippet: {language: "does_not_exist"}}
105+
patch share_snippet_url(snippet), params: {snippet: {language: "does_not_exist"}}
106106
expect(response).to have_http_status(:unprocessable_entity)
107107
end
108108
end
@@ -113,15 +113,15 @@
113113
Flipper.enable(:snippets, login_as_user)
114114
snippet = FactoryBot.create(:snippet)
115115
expect {
116-
delete snippet_url(snippet)
116+
delete share_snippet_url(snippet)
117117
}.to change(Snippet, :count).by(-1)
118118
end
119119

120120
it "redirects to the snippets list" do
121121
Flipper.enable(:snippets, login_as_user)
122122
snippet = FactoryBot.create(:snippet)
123-
delete snippet_url(snippet)
124-
expect(response).to redirect_to(snippets_url)
123+
delete share_snippet_url(snippet)
124+
expect(response).to redirect_to(share_snippets_url)
125125
end
126126
end
127127
end

0 commit comments

Comments
 (0)