Skip to content

Commit 47ff00c

Browse files
committed
Add contributor association between Users and Providers
Introduced a many-to-many relationship between Users and Providers via Contributors. Updated views and forms to manage contributors, restricted to admin users. Enhanced Providers with a unique name validation and adjusted tests to cover new behavior.
1 parent b58278c commit 47ff00c

File tree

12 files changed

+92
-9
lines changed

12 files changed

+92
-9
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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@
3232
</div>
3333
</div>
3434

35+
<% if Current.user.is_admin %>
36+
<div class="col-md-4">
37+
<div class="form-group">
38+
<%= form.label :contributors, style: "display: block" %>
39+
<%= form.collection_select :user_ids, User.order(:email), :id, :email, { include_hidden: false }, class: "form-select", multiple: true %>
40+
</div>
41+
</div>
42+
<% end %>
43+
3544
<div class="mt-4">
3645
<div class="col-12 d-flex justify-content-end">
3746
<%= 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: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/factories/users.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,10 @@
88
is_admin { true }
99
end
1010
end
11+
12+
factory :admin do
13+
email { "john@doe.com" }
14+
password { "password" }
15+
is_admin { true }
16+
end
1117
end
Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
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) }
5+
let(:admin) { create(:admin) }
6+
let(:user) { create(:user) }
7+
8+
before do
9+
allow(Current).to receive(:user).and_return(user)
10+
end
1011

1112
before(:each) do
1213
assign(:provider, provider)
@@ -17,8 +18,29 @@
1718

1819
assert_select "form[action=?][method=?]", provider_path(provider), "post" do
1920
assert_select "input[name=?]", "provider[name]"
20-
2121
assert_select "input[name=?]", "provider[provider_type]"
2222
end
2323
end
24+
25+
it "does not render the contributor form group for common user" do
26+
render
27+
28+
assert_select "form[action=?][method=?]", provider_path(provider), "post" do
29+
assert_select "select[id=?]", "provider_user_ids", false
30+
end
31+
end
32+
33+
describe "for admin" do
34+
before do
35+
allow(Current).to receive(:user).and_return(admin)
36+
end
37+
38+
it "renders the contributor form group" do
39+
render
40+
41+
assert_select "form[action=?][method=?]", provider_path(provider), "post" do
42+
assert_select "select[id=?]", "provider_user_ids"
43+
end
44+
end
45+
end
2446
end

0 commit comments

Comments
 (0)