Skip to content

Commit a4d3af3

Browse files
authored
Provider model implementation (#43)
1 parent c644d00 commit a4d3af3

24 files changed

+568
-38
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
class ProvidersController < ApplicationController
2+
before_action :set_provider, only: %i[ show edit update destroy ]
3+
4+
def index
5+
@providers = Provider.all
6+
end
7+
8+
def show
9+
end
10+
11+
def new
12+
@provider = Provider.new
13+
end
14+
15+
def edit
16+
end
17+
18+
def create
19+
@provider = Provider.new(provider_params)
20+
21+
respond_to do |format|
22+
if @provider.save
23+
format.html { redirect_to @provider, notice: "Provider was successfully created." }
24+
else
25+
format.html { render :new, status: :unprocessable_entity }
26+
end
27+
end
28+
end
29+
30+
def update
31+
respond_to do |format|
32+
if @provider.update(provider_params)
33+
format.html { redirect_to @provider, notice: "Provider was successfully updated." }
34+
else
35+
format.html { render :edit, status: :unprocessable_entity }
36+
end
37+
end
38+
end
39+
40+
def destroy
41+
@provider.destroy!
42+
43+
respond_to do |format|
44+
format.html { redirect_to providers_path, status: :see_other, notice: "Provider was successfully destroyed." }
45+
end
46+
end
47+
48+
private
49+
def set_provider
50+
@provider = Provider.find(params.expect(:id))
51+
end
52+
53+
def provider_params
54+
params.expect(provider: [ :name, :provider_type, region_ids: [] ])
55+
end
56+
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+
belongs_to :provider
3+
belongs_to :region
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: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,17 @@
5555
<li class="sidebar-title">Administration</li>
5656

5757
<li class="sidebar-item ">
58-
<a href="<%= regions_path %>" class='sidebar-link'>
59-
<i class="bi bi-globe"></i>
58+
<%= link_to regions_path, class: 'sidebar-link' do %>
59+
<i class="bi bi-hospital-fill"></i>
6060
<span>Regions</span>
61-
</a>
61+
<% end %>
6262
</li>
6363

6464
<li class="sidebar-item ">
65-
<a href="table.html" class='sidebar-link'>
65+
<%= link_to providers_path, class: 'sidebar-link' do %>
6666
<i class="bi bi-hospital-fill"></i>
6767
<span>Providers</span>
68-
</a>
68+
<% end %>
6969
</li>
7070

7171
<li class="sidebar-item ">

app/views/providers/_form.html.erb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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="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+
35+
<div class="mt-4">
36+
<div class="col-12 d-flex justify-content-end">
37+
<%= form.submit "Create Provider", class: "btn btn-primary me-1 mb-1" %>
38+
<%= link_to "Cancel", providers_path, class: "btn btn-light-secondary me-1 mb-1" %>
39+
</div>
40+
</div>
41+
<% end %>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
<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>
24+
</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)