Skip to content

Commit 51c6dda

Browse files
committed
Add feature gate for snippets
1 parent 727ae1a commit 51c6dda

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

app/controllers/snippets_controller.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class SnippetsController < ApplicationController
2+
before_action :feature_enabled!
3+
24
# GET /snippets
35
def index
46
@snippets = Snippet.all
@@ -54,4 +56,11 @@ def destroy
5456
def snippet_params
5557
params.fetch(:snippet, {}).permit(:filename, :source, :url, :language)
5658
end
59+
60+
def feature_enabled!
61+
return if user_signed_in? &&
62+
Flipper.enabled?(:snippets, current_user)
63+
64+
raise ActionController::RoutingError.new("Not Found")
65+
end
5766
end

spec/requests/snippets_spec.rb

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
RSpec.describe "/snippets", type: :request do
44
describe "GET /index" do
55
it "renders a successful response" do
6+
Flipper.enable(:snippets, login_as_user)
7+
68
FactoryBot.create(:snippet)
79
get snippets_url
810
expect(response).to be_successful
@@ -11,6 +13,8 @@
1113

1214
describe "GET /show" do
1315
it "renders a successful response" do
16+
Flipper.enable(:snippets, login_as_user)
17+
1418
snippet = FactoryBot.create(:snippet)
1519
get snippet_url(snippet)
1620
expect(response).to be_successful
@@ -19,13 +23,15 @@
1923

2024
describe "GET /new" do
2125
it "renders a successful response" do
26+
Flipper.enable(:snippets, login_as_user)
2227
get new_snippet_url
2328
expect(response).to be_successful
2429
end
2530
end
2631

2732
describe "GET /edit" do
2833
it "renders a successful response" do
34+
Flipper.enable(:snippets, login_as_user)
2935
snippet = FactoryBot.create(:snippet)
3036
get edit_snippet_url(snippet)
3137
expect(response).to be_successful
@@ -34,6 +40,10 @@
3440

3541
describe "POST /create" do
3642
context "with valid parameters" do
43+
before do
44+
Flipper.enable(:snippets, login_as_user)
45+
end
46+
3747
it "creates a new Snippet" do
3848
expect {
3949
post snippets_url, params: {snippet: {source: "puts \"Hello!\"", language: "ruby"}}
@@ -47,6 +57,10 @@
4757
end
4858

4959
context "with invalid parameters" do
60+
before do
61+
Flipper.enable(:snippets, login_as_user)
62+
end
63+
5064
it "does not create a new Snippet" do
5165
expect {
5266
post snippets_url, params: {snippet: {}}
@@ -62,9 +76,9 @@
6276

6377
describe "PATCH /update" do
6478
context "with valid parameters" do
65-
let(:new_attributes) {
66-
skip("Add a hash of attributes valid for your model")
67-
}
79+
before do
80+
Flipper.enable(:snippets, login_as_user)
81+
end
6882

6983
it "updates the requested snippet" do
7084
snippet = FactoryBot.create(:snippet)
@@ -82,6 +96,10 @@
8296
end
8397

8498
context "with invalid parameters" do
99+
before do
100+
Flipper.enable(:snippets, login_as_user)
101+
end
102+
85103
it "renders a response with 422 status (i.e. to display the 'edit' template)" do
86104
snippet = FactoryBot.create(:snippet)
87105
patch snippet_url(snippet), params: {snippet: {language: "does_not_exist"}}
@@ -92,13 +110,15 @@
92110

93111
describe "DELETE /destroy" do
94112
it "destroys the requested snippet" do
113+
Flipper.enable(:snippets, login_as_user)
95114
snippet = FactoryBot.create(:snippet)
96115
expect {
97116
delete snippet_url(snippet)
98117
}.to change(Snippet, :count).by(-1)
99118
end
100119

101120
it "redirects to the snippets list" do
121+
Flipper.enable(:snippets, login_as_user)
102122
snippet = FactoryBot.create(:snippet)
103123
delete snippet_url(snippet)
104124
expect(response).to redirect_to(snippets_url)

0 commit comments

Comments
 (0)