Skip to content

Commit 19d24e2

Browse files
committed
use current_provider for topic when user is not an admin
1 parent 9937ee3 commit 19d24e2

File tree

6 files changed

+51
-8
lines changed

6 files changed

+51
-8
lines changed

Procfile.dev

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web: bin/rails server -p 3000

app/controllers/topics_controller.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,14 @@ def other_available_providers
5252
end
5353

5454
def topic_params
55-
params.require(:topic).permit(:title, :description, :uid, :language_id, :provider_id, documents: [])
55+
params
56+
.require(:topic)
57+
.permit(:title, :description, :uid, :language_id, :provider_id, documents: []).tap do |perm_params|
58+
if perm_params["provider_id"].present?
59+
perm_params["provider_id"] = Current.user.providers.find(perm_params["provider_id"]).id
60+
perm_params["provider_id"] = current_provider.id if current_provider && !Current.user.is_admin?
61+
end
62+
end
5663
end
5764

5865
def search_params

app/views/topics/_form.html.erb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
<%= f.label :language %>
1717
<%= f.collection_select :language_id, Language.all, :id, :name, { prompt: "Select Language" }, class: "form-select" %>
1818
</div>
19-
<div class="form-group">
20-
<%= f.label :provider %>
21-
<%= f.collection_select :provider_id, Provider.all, :id, :name, { prompt: "Select Provider" }, class: "form-select" %>
22-
</div>
19+
<% if Current.user.is_admin? %>
20+
<div class="form-group">
21+
<%= f.label :provider %>
22+
<%= f.collection_select :provider_id, Provider.all, :id, :name, { prompt: "Select Provider" }, class: "form-select" %>
23+
</div>
24+
<% end %>
2325
<div class="form-group">
2426
<%= f.label :description %>
2527
<%= f.text_area :description, class: "form-control", placeholder: "Descripion" %>

app/views/topics/index.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<div class="card">
99
<div class="card-header d-flex justify-content-between align-items-center">
1010
<h2 class="card-title"><%= topics_title %></h2>
11-
<%= link_to new_topic_path, class: "btn btn-primary" do %>
11+
<%= link_to new_topic_path, class: "btn btn-primary", data: { turbo: false } do %>
1212
<i class="bi bi-plus"></i> Add New Topic
1313
<% end %>
1414
</div>

bin/server

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#!/usr/bin/env sh
22

3-
overmind start -r all
3+
overmind start -r all -f Procfile.dev

spec/requests/topics/create_spec.rb

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
let(:language) { create(:language) }
88
let(:topic_params) { attributes_for(:topic, provider_id: provider.id, language_id: language.id) }
99

10-
before { sign_in(user) }
10+
before do
11+
provider.users << user
12+
sign_in(user)
13+
end
1114

1215
it "creates a Topic" do
1316
post topics_url, params: { topic: topic_params }
@@ -18,5 +21,35 @@
1821
expect(topic.description).to eq("details")
1922
expect(topic.state).to eq("active")
2023
end
24+
25+
context "when current provider is set" do
26+
let(:current_provider) { create(:provider) }
27+
28+
before do
29+
current_provider.users << user
30+
cookies = ActionDispatch::Request.new(Rails.application.env_config).cookie_jar
31+
cookies.signed[:current_provider_id] = current_provider.id
32+
end
33+
34+
it "creates a Topic for the current provider" do
35+
post topics_url, params: { topic: topic_params }
36+
37+
expect(response).to redirect_to(topics_url)
38+
topic = Topic.last
39+
expect(topic.provider).to eq(current_provider)
40+
end
41+
42+
context "when user is an admin" do
43+
before { user.update(is_admin: true) }
44+
45+
it "creates a Topic for the selected provider" do
46+
post topics_url, params: { topic: topic_params }
47+
48+
expect(response).to redirect_to(topics_url)
49+
topic = Topic.last
50+
expect(topic.provider).to eq(provider)
51+
end
52+
end
53+
end
2154
end
2255
end

0 commit comments

Comments
 (0)