Skip to content

Commit f4f356b

Browse files
committed
Add Provider resource with model, views, controller, and tests
Introduced the Provider model, associated views, and controller with CRUD actions. Established relationships with Branch and Region models. Added thorough tests for the functionality and updated routes and layouts for navigation.
1 parent c644d00 commit f4f356b

24 files changed

+569
-34
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
class ProvidersController < ApplicationController
2+
before_action :set_provider, only: %i[ show edit update destroy ]
3+
4+
# GET /providers
5+
def index
6+
@providers = Provider.all
7+
end
8+
9+
# GET /providers/1
10+
def show
11+
end
12+
13+
# GET /providers/new
14+
def new
15+
@provider = Provider.new
16+
end
17+
18+
# GET /providers/1/edit
19+
def edit
20+
end
21+
22+
# POST /providers
23+
def create
24+
@provider = Provider.new(provider_params)
25+
26+
respond_to do |format|
27+
if @provider.save
28+
format.html { redirect_to @provider, notice: "Provider was successfully created." }
29+
else
30+
format.html { render :new, status: :unprocessable_entity }
31+
end
32+
end
33+
end
34+
35+
# PATCH/PUT /providers/1
36+
def update
37+
respond_to do |format|
38+
if @provider.update(provider_params)
39+
format.html { redirect_to @provider, notice: "Provider was successfully updated." }
40+
else
41+
format.html { render :edit, status: :unprocessable_entity }
42+
end
43+
end
44+
end
45+
46+
# DELETE /providers/1
47+
def destroy
48+
@provider.destroy!
49+
50+
respond_to do |format|
51+
format.html { redirect_to providers_path, status: :see_other, notice: "Provider was successfully destroyed." }
52+
end
53+
end
54+
55+
private
56+
# Use callbacks to share common setup or constraints between actions.
57+
def set_provider
58+
@provider = Provider.find(params.expect(:id))
59+
end
60+
61+
# Only allow a list of trusted parameters through.
62+
def provider_params
63+
params.expect(provider: [ :name, :provider_type ])
64+
end
65+
end

app/helpers/providers_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module ProvidersHelper
2+
end

app/models/branch.rb

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

app/models/provider.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Provider < ApplicationRecord
2+
has_many :branches
3+
has_many :regions, through: :branches
4+
5+
validates :name, :provider_type, presence: true
6+
end

app/models/region.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
class Region < ApplicationRecord
2+
has_many :branches
3+
has_many :providers, through: :branches
4+
25
validates :name, presence: true
36
end

app/views/layouts/_sidebar.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
</li>
6363

6464
<li class="sidebar-item ">
65-
<a href="table.html" class='sidebar-link'>
65+
<a href="<%= providers_path %>" class='sidebar-link'>
6666
<i class="bi bi-hospital-fill"></i>
6767
<span>Providers</span>
6868
</a>

app/views/providers/_form.html.erb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<%= form_with(model: provider) do |form| %>
2+
<% if provider.errors.any? %>
3+
<div style="color: red">
4+
<h2><%= pluralize(provider.errors.count, "error") %> prohibited this provider from being saved:</h2>
5+
6+
<ul>
7+
<% provider.errors.each do |error| %>
8+
<li><%= error.full_message %></li>
9+
<% end %>
10+
</ul>
11+
</div>
12+
<% end %>
13+
14+
<div class="col-md-4">
15+
<div class="form-group">
16+
<%= form.label :name, style: "display: block" %>
17+
<%= form.text_field :name, id: "basicInput", class: "form-control", autofocus: true %>
18+
</div>
19+
</div>
20+
21+
<div class="col-md-4">
22+
<div class="form-group">
23+
<%= form.label :provider_type, style: "display: block" %>
24+
<%= form.text_field :provider_type, id: "basicInput", class: "form-control" %>
25+
</div>
26+
</div>
27+
28+
<div class="mt-4">
29+
<div class="col-12 d-flex justify-content-end">
30+
<%= form.submit "Create Provider", class: "btn btn-primary me-1 mb-1" %>
31+
<%= link_to "Cancel", providers_path, class: "btn btn-light-secondary me-1 mb-1" %>
32+
</div>
33+
</div>
34+
<% end %>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<div id="<%= dom_id provider %>">
2+
<p>
3+
<strong>Name:</strong>
4+
<%= provider.name %>
5+
</p>
6+
7+
<p>
8+
<strong>Provider type:</strong>
9+
<%= provider.provider_type %>
10+
</p>
11+
12+
</div>

app/views/providers/edit.html.erb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<% content_for :title, "Editing provider" %>
2+
3+
<h1>Editing provider</h1>
4+
5+
<%= render "form", provider: @provider %>
6+
7+
<br>
8+
9+
<div>
10+
<%= link_to "Show this provider", @provider %> |
11+
<%= link_to "Back to providers", providers_path %>
12+
</div>

app/views/providers/index.html.erb

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<% content_for :title, "Regions" %>
2+
3+
<section class="section">
4+
<div class="row" id="table-striped">
5+
<div class="col-12 cold-md-12">
6+
<div class="card">
7+
<div class="card-header d-flex justify-content-between align-items-center">
8+
<h2>Providers</h2>
9+
<%= link_to new_provider_path, class: "btn btn-primary" do %>
10+
<i class="bi bi-plus"></i> Add New Provider
11+
<% end %>
12+
</div>
13+
<div class="card-content">
14+
<div class="card-body">
15+
<p class="card-text">
16+
Lorem ipsum dolor sit amet, <code>consectetur adipiscing</code> elit. Sed do eiusmod tempor incididunt ut
17+
labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
18+
aliquip
19+
ex ea commodo consequat. Duis aute irure dolor in <code>reprehenderit</code> in voluptate velit esse
20+
cillum dolore eu fugiat nulla
21+
pariatur. Excepteur sint <code>occaecat cupidatat</code> non proident, sunt in culpa qui officia deserunt
22+
mollit anim id est
23+
laborum.
24+
</p>
25+
<!-- table striped -->
26+
<div class="table-responsive">
27+
<table class="table table-lg table-striped mb-0">
28+
<thead>
29+
<tr>
30+
<th>Provider</th>
31+
<th>Type</th>
32+
<th class="text-end">Provider Actions</th>
33+
</tr>
34+
</thead>
35+
<tbody>
36+
<% @providers.each do |provider| %>
37+
<tr>
38+
<td class="text-bold-500"><%= provider.name %></td>
39+
<td><%= provider.provider_type %></td>
40+
<td class="text-end">
41+
<%= link_to provider, class: "btn btn-primary btn-sm" do %>
42+
<i class="bi bi-search"></i> View
43+
<% end %>
44+
<%= link_to edit_provider_path(provider), class: "btn btn-secondary btn-sm" do %>
45+
<i class="bi bi-pencil"></i> Edit
46+
<% end %>
47+
<%= link_to provider, method: :delete, data: { confirm: "Are you sure?" }, class: "btn btn-danger btn-sm" do %>
48+
<i class="bi bi-trash"></i> Delete
49+
<% end %>
50+
</td>
51+
</tr>
52+
<% end %>
53+
</tbody>
54+
</table>
55+
</div>
56+
</div>
57+
</div>
58+
</div>
59+
</div>
60+
</div>
61+
</section>

0 commit comments

Comments
 (0)