Skip to content

Commit d6032c5

Browse files
committed
Associate author with snippet
1 parent cd01b3d commit d6032c5

File tree

5 files changed

+17
-5
lines changed

5 files changed

+17
-5
lines changed

app/controllers/share/snippets_controller.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ class Share::SnippetsController < ApplicationController
22
using Refinements::Emojoy
33

44
before_action :feature_enabled!
5+
before_action :authenticate_user!, only: %i[new create edit update destroy]
56

67
# GET /snippets
78
def index
@@ -20,18 +21,18 @@ def new
2021
source: "class User < ApplicationRecord\n\s\shas_many :posts\nend",
2122
language: "ruby"
2223
}
23-
@snippet = Snippet.new(snippet_params.presence || default_params)
24+
@snippet = current_user.snippets.new(snippet_params.presence || default_params)
2425
end
2526

2627
# GET /snippets/1/edit
2728
def edit
28-
@snippet = Snippet.find(params[:id])
29+
@snippet = current_user.snippets.find(params[:id])
2930
@snippet.assign_attributes(snippet_params)
3031
end
3132

3233
# POST /snippets
3334
def create
34-
@snippet = Snippet.new(snippet_params)
35+
@snippet = current_user.snippets.new(snippet_params)
3536

3637
if @snippet.save
3738
redirect_to share_snippet_redirect_url(@snippet), notice: "Your snippet has been saved".emojoy, status: :see_other
@@ -42,7 +43,7 @@ def create
4243

4344
# PATCH/PUT /snippets/1
4445
def update
45-
@snippet = Snippet.find(params[:id])
46+
@snippet = current_user.snippets.find(params[:id])
4647
if @snippet.update(snippet_params)
4748
@snippet.attach_screenshot_from_base64(params[:screenshot]) if params[:screenshot]
4849

@@ -54,7 +55,7 @@ def update
5455

5556
# DELETE /snippets/1
5657
def destroy
57-
@snippet = Snippet.find(params[:id])
58+
@snippet = current_user.snippets.find(params[:id])
5859
@snippet.destroy!
5960
redirect_to share_snippets_url, notice: "Your snippet has been deleted permanently".emojoy, status: :see_other
6061
end

app/models/snippet.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ class Snippet < ApplicationRecord
77

88
has_one_attached :screenshot
99

10+
belongs_to :author, polymorphic: true, inverse_of: :snippets
11+
1012
attr_reader :auto_detecting
1113

1214
def attach_screenshot_from_base64(data)

app/models/user.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ class User < ApplicationRecord
88

99
has_one :newsletter_subscription, as: :subscriber, dependent: :destroy
1010

11+
has_many :snippets, as: :author, dependent: :destroy
12+
1113
scope :confirmed, -> { where.not(confirmed_at: nil) }
1214
scope :recently_confirmed, -> { where("confirmed_at > ?", 2.weeks.ago) }
1315
scope :subscribers, -> { confirmed.joins(:newsletter_subscription) }

spec/factories/snippets.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
factory :snippet do
33
source { "puts \"Hello, world!\"" }
44
language { "ruby" }
5+
author { build(:user) }
56
end
67
end

spec/requests/share/snippets_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@
2727
get new_share_snippet_url
2828
expect(response).to be_successful
2929
end
30+
31+
it "redirects when not authenticated" do
32+
Flipper.enable(:snippets)
33+
get new_share_snippet_url
34+
expect(response).to be_not_found
35+
end
3036
end
3137

3238
describe "GET /edit" do

0 commit comments

Comments
 (0)