Skip to content

Commit b49650e

Browse files
authored
Add contributor association between Users and Providers (#46)
1 parent 2a42b39 commit b49650e

File tree

10 files changed

+60
-10
lines changed

10 files changed

+60
-10
lines changed

app/controllers/providers_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,6 @@ def set_provider
5151
end
5252

5353
def provider_params
54-
params.expect(provider: [ :name, :provider_type, region_ids: [] ])
54+
params.expect(provider: [ :name, :provider_type, region_ids: [], user_ids: [] ])
5555
end
5656
end

app/models/contributor.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Contributor < ApplicationRecord
2+
belongs_to :provider
3+
belongs_to :user
4+
end

app/models/provider.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
class Provider < ApplicationRecord
22
has_many :branches
33
has_many :regions, through: :branches
4+
has_many :contributors
5+
has_many :users, through: :contributors
46

57
validates :name, :provider_type, presence: true
8+
validates :name, uniqueness: true
69
end

app/models/user.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
class User < ApplicationRecord
22
has_secure_password
33
has_many :sessions, dependent: :destroy
4+
has_many :contributors
5+
has_many :providers, through: :contributors
46

57
normalizes :email, with: ->(e) { e.strip.downcase }
68

app/views/providers/_form.html.erb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@
3232
</div>
3333
</div>
3434

35+
<div class="col-md-4">
36+
<div class="form-group">
37+
<%= form.label :contributors, style: "display: block" %>
38+
<%= form.collection_select :user_ids, User.order(:email), :id, :email, { include_hidden: false }, class: "form-select", multiple: true %>
39+
</div>
40+
</div>
41+
3542
<div class="mt-4">
3643
<div class="col-12 d-flex justify-content-end">
3744
<%= form.submit "Create Provider", class: "btn btn-primary me-1 mb-1" %>

app/views/providers/_provider.html.erb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,17 @@
2121
<% end %>
2222
</ul>
2323
</p>
24+
25+
<p>
26+
<strong>Provider contributors:</strong>
27+
<ul>
28+
<% provider.users.each do |user| %>
29+
<li>
30+
<%= link_to user_path(user) do %>
31+
<%= user.email %>
32+
<% end %>
33+
</li>
34+
<% end %>
35+
</ul>
36+
</p>
2437
</div>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class CreateContributor < ActiveRecord::Migration[8.0]
2+
def change
3+
create_table :contributors do |t|
4+
t.belongs_to :provider
5+
t.belongs_to :user
6+
7+
t.timestamps
8+
end
9+
end
10+
end

db/schema.rb

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
require "rails_helper"
22

33
RSpec.describe "providers/edit", type: :view do
4-
let(:provider) {
5-
Provider.create!(
6-
name: "MyString",
7-
provider_type: "MyString",
8-
)
9-
}
4+
let(:provider) { create(:provider) }
105

116
before(:each) do
127
assign(:provider, provider)
@@ -17,8 +12,15 @@
1712

1813
assert_select "form[action=?][method=?]", provider_path(provider), "post" do
1914
assert_select "input[name=?]", "provider[name]"
20-
2115
assert_select "input[name=?]", "provider[provider_type]"
2216
end
2317
end
18+
19+
it "renders the contributor form group" do
20+
render
21+
22+
assert_select "form[action=?][method=?]", provider_path(provider), "post" do
23+
assert_select "select[id=?]", "provider_user_ids"
24+
end
25+
end
2426
end

spec/views/providers/index.html.erb_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
provider_type: "Provider Type",
99
),
1010
Provider.create!(
11-
name: "Name",
11+
name: "Name1",
1212
provider_type: "Provider Type",
1313
),
1414
],)

0 commit comments

Comments
 (0)