Skip to content

Commit a018b5c

Browse files
committed
Support Provider-region association with UI and tests.
1 parent fc59073 commit a018b5c

File tree

5 files changed

+33
-7
lines changed

5 files changed

+33
-7
lines changed

app/controllers/providers_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,6 @@ def set_provider
6060

6161
# Only allow a list of trusted parameters through.
6262
def provider_params
63-
params.expect(provider: [ :name, :provider_type ])
63+
params.expect(provider: [ :name, :provider_type, region_ids: [] ])
6464
end
6565
end

app/models/branch.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
class Branch < ApplicationRecord
2-
has_many :providers
3-
has_many :regions
2+
belongs_to :provider
3+
belongs_to :region
44
end

app/views/providers/_form.html.erb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@
2525
</div>
2626
</div>
2727

28+
<div class="col-md-4">
29+
<div class="form-group">
30+
<%= form.label :region, style: "display: block" %>
31+
<%= form.collection_select :region_ids, Region.order(:name), :id, :name, { include_hidden: false }, class: "form-select", multiple: true %>
32+
</div>
33+
</div>
34+
2835
<div class="mt-4">
2936
<div class="col-12 d-flex justify-content-end">
3037
<%= form.submit "Create Provider", class: "btn btn-primary me-1 mb-1" %>

app/views/providers/_provider.html.erb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,16 @@
99
<%= provider.provider_type %>
1010
</p>
1111

12+
<p>
13+
<strong>Provider regions:</strong>
14+
<ul>
15+
<% provider.regions.each do |region| %>
16+
<li>
17+
<%= link_to region_path(region) do %>
18+
<%= region.name %>
19+
<% end %>
20+
</li>
21+
<% end %>
22+
</ul>
23+
</p>
1224
</div>

spec/requests/providers_spec.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@
1515
RSpec.describe "/providers", type: :request do
1616
let(:user) { create(:user) }
1717

18-
include AuthHelper
19-
2018
before do
2119
sign_in(user)
2220
end
2321

22+
let(:region_ids) { [create(:region).id, create(:region).id] }
23+
let(:updated_region_ids) { [create(:region).id] }
2424
# This should return the minimal set of attributes required to create a valid
2525
# Provider. As you add validations to Provider, be sure to
2626
# adjust the attributes here as well.
2727
let(:valid_attributes) {
28-
{ name: "MyString", provider_type: "MyString" }
28+
{ name: "MyString", provider_type: "MyString", region_ids: region_ids }
2929
}
3030

3131
let(:invalid_attributes) {
@@ -71,6 +71,12 @@
7171
}.to change(Provider, :count).by(1)
7272
end
7373

74+
it "creates two new Branches" do
75+
expect {
76+
post providers_url, params: { provider: valid_attributes }
77+
}.to change(Branch, :count).by(2)
78+
end
79+
7480
it "redirects to the created provider" do
7581
post providers_url, params: { provider: valid_attributes }
7682
expect(response).to redirect_to(provider_url(Provider.last))
@@ -94,7 +100,7 @@
94100
describe "PATCH /update" do
95101
context "with valid parameters" do
96102
let(:new_attributes) {
97-
{ name: "MyString", provider_type: "MyType" }
103+
{ name: "MyString", provider_type: "MyType", region_ids: updated_region_ids }
98104
}
99105

100106
it "updates the requested provider" do
@@ -103,6 +109,7 @@
103109
provider.reload
104110
expect(provider.name).to eq("MyString")
105111
expect(provider.provider_type).to eq("MyType")
112+
expect(provider.regions.length).to eq(updated_region_ids.length)
106113
end
107114

108115
it "redirects to the provider" do

0 commit comments

Comments
 (0)