Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions app/controllers/providers_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
class ProvidersController < ApplicationController
before_action :set_provider, only: %i[ show edit update destroy ]

def index
@providers = Provider.all
end

def show
end

def new
@provider = Provider.new
end

def edit
end

def create
@provider = Provider.new(provider_params)

respond_to do |format|
if @provider.save
format.html { redirect_to @provider, notice: "Provider was successfully created." }
else
format.html { render :new, status: :unprocessable_entity }
end
end
end

def update
respond_to do |format|
if @provider.update(provider_params)
format.html { redirect_to @provider, notice: "Provider was successfully updated." }
else
format.html { render :edit, status: :unprocessable_entity }
end
end
end

def destroy
@provider.destroy!

respond_to do |format|
format.html { redirect_to providers_path, status: :see_other, notice: "Provider was successfully destroyed." }
end
end

private
def set_provider
@provider = Provider.find(params.expect(:id))
end

def provider_params
params.expect(provider: [ :name, :provider_type, region_ids: [] ])
end
end
2 changes: 2 additions & 0 deletions app/helpers/providers_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module ProvidersHelper
end
4 changes: 4 additions & 0 deletions app/models/branch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Branch < ApplicationRecord
belongs_to :provider
belongs_to :region
end
6 changes: 6 additions & 0 deletions app/models/provider.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Provider < ApplicationRecord
has_many :branches
has_many :regions, through: :branches

validates :name, :provider_type, presence: true
end
3 changes: 3 additions & 0 deletions app/models/region.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
class Region < ApplicationRecord
has_many :branches
has_many :providers, through: :branches

validates :name, presence: true
end
10 changes: 5 additions & 5 deletions app/views/layouts/_sidebar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,17 @@
<li class="sidebar-title">Administration</li>

<li class="sidebar-item ">
<a href="<%= regions_path %>" class='sidebar-link'>
<i class="bi bi-globe"></i>
<%= link_to regions_path, class: 'sidebar-link' do %>
<i class="bi bi-hospital-fill"></i>
<span>Regions</span>
</a>
<% end %>
</li>

<li class="sidebar-item ">
<a href="table.html" class='sidebar-link'>
<%= link_to providers_path, class: 'sidebar-link' do %>
<i class="bi bi-hospital-fill"></i>
<span>Providers</span>
</a>
<% end %>
</li>

<li class="sidebar-item ">
Expand Down
41 changes: 41 additions & 0 deletions app/views/providers/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<%= form_with(model: provider) do |form| %>
<% if provider.errors.any? %>
<div style="color: red">
<h2><%= pluralize(provider.errors.count, "error") %> prohibited this provider from being saved:</h2>

<ul>
<% provider.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="col-md-4">
<div class="form-group">
<%= form.label :name, style: "display: block" %>
<%= form.text_field :name, id: "basicInput", class: "form-control", autofocus: true %>
</div>
</div>

<div class="col-md-4">
<div class="form-group">
<%= form.label :provider_type, style: "display: block" %>
<%= form.text_field :provider_type, id: "basicInput", class: "form-control" %>
</div>
</div>

<div class="col-md-4">
<div class="form-group">
<%= form.label :region, style: "display: block" %>
<%= form.collection_select :region_ids, Region.order(:name), :id, :name, { include_hidden: false }, class: "form-select", multiple: true %>
</div>
</div>

<div class="mt-4">
<div class="col-12 d-flex justify-content-end">
<%= form.submit "Create Provider", class: "btn btn-primary me-1 mb-1" %>
<%= link_to "Cancel", providers_path, class: "btn btn-light-secondary me-1 mb-1" %>
</div>
</div>
<% end %>
24 changes: 24 additions & 0 deletions app/views/providers/_provider.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<div id="<%= dom_id provider %>">
<p>
<strong>Name:</strong>
<%= provider.name %>
</p>

<p>
<strong>Provider type:</strong>
<%= provider.provider_type %>
</p>

<p>
<strong>Provider regions:</strong>
<ul>
<% provider.regions.each do |region| %>
<li>
<%= link_to region_path(region) do %>
<%= region.name %>
<% end %>
</li>
<% end %>
</ul>
</p>
</div>
12 changes: 12 additions & 0 deletions app/views/providers/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<% content_for :title, "Editing provider" %>

<h1>Editing provider</h1>

<%= render "form", provider: @provider %>

<br>

<div>
<%= link_to "Show this provider", @provider %> |
<%= link_to "Back to providers", providers_path %>
</div>
61 changes: 61 additions & 0 deletions app/views/providers/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<% content_for :title, "Regions" %>

<section class="section">
<div class="row" id="table-striped">
<div class="col-12 cold-md-12">
<div class="card">
<div class="card-header d-flex justify-content-between align-items-center">
<h2>Providers</h2>
<%= link_to new_provider_path, class: "btn btn-primary" do %>
<i class="bi bi-plus"></i> Add New Provider
<% end %>
</div>
<div class="card-content">
<div class="card-body">
<p class="card-text">
Lorem ipsum dolor sit amet, <code>consectetur adipiscing</code> elit. Sed do eiusmod tempor incididunt ut
labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip
ex ea commodo consequat. Duis aute irure dolor in <code>reprehenderit</code> in voluptate velit esse
cillum dolore eu fugiat nulla
pariatur. Excepteur sint <code>occaecat cupidatat</code> non proident, sunt in culpa qui officia deserunt
mollit anim id est
laborum.
</p>
<!-- table striped -->
<div class="table-responsive">
<table class="table table-lg table-striped mb-0">
<thead>
<tr>
<th>Provider</th>
<th>Type</th>
<th class="text-end">Provider Actions</th>
</tr>
</thead>
<tbody>
<% @providers.each do |provider| %>
<tr>
<td class="text-bold-500"><%= provider.name %></td>
<td><%= provider.provider_type %></td>
<td class="text-end">
<%= link_to provider, class: "btn btn-primary btn-sm" do %>
<i class="bi bi-search"></i> View
<% end %>
<%= link_to edit_provider_path(provider), class: "btn btn-secondary btn-sm" do %>
<i class="bi bi-pencil"></i> Edit
<% end %>
<%= link_to provider, method: :delete, data: { confirm: "Are you sure?" }, class: "btn btn-danger btn-sm" do %>
<i class="bi bi-trash"></i> Delete
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
23 changes: 23 additions & 0 deletions app/views/providers/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<% content_for :title, "New provider" %>

<section id="multiple-column-form">
<div class="row match-height">
<div class="col-12">
<div class="card">
<div class="card-header">
<h4>Create New Provider</h4>
</div>
<div class="card-content">
<div class="card-body">
<p>Providers should ... Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut
labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip
ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est
laborum.</p>
<%= render "form", provider: @provider %>
</div>
</div>
</div>
</div>
</div>
</section>
12 changes: 12 additions & 0 deletions app/views/providers/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<p style="color: green"><%= notice %></p>

<%= render @provider %>

<div>
<%= link_to "Edit this provider", edit_provider_path(@provider) %> |
<%= link_to "Back to providers", providers_path %>
</div>

<div>
<%= button_to "Destroy this provider", @provider, method: :delete, class: "btn btn-danger mt-4" %>
</div>
58 changes: 31 additions & 27 deletions app/views/regions/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,50 @@
<div class="card-content">
<div class="card-body">
<p class="card-text">
Lorem ipsum dolor sit amet, <code>consectetur adipiscing</code> elit. Sed do eiusmod tempor incididunt ut
labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip
ex ea commodo consequat. Duis aute irure dolor in <code>reprehenderit</code> in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint <code>occaecat cupidatat</code> non proident, sunt in culpa qui officia deserunt mollit anim id est
Lorem ipsum dolor sit amet, <code>consectetur adipiscing</code> elit. Sed do eiusmod tempor incididunt ut
labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip
ex ea commodo consequat. Duis aute irure dolor in <code>reprehenderit</code> in voluptate velit esse
cillum dolore eu fugiat nulla
pariatur. Excepteur sint <code>occaecat cupidatat</code> non proident, sunt in culpa qui officia deserunt
mollit anim id est
laborum.
</p>
<!-- table striped -->
<div class="table-responsive">
<table class="table table-lg table-striped mb-0">
<thead>
<tr>
<th>Region</th>
<th>Providers</th>
<th class="text-end">Region Actions</th>
</tr>
<tr>
<th>Region</th>
<th>Providers</th>
<th class="text-end">Region Actions</th>
</tr>
</thead>
<tbody>
<% @regions.each do |region| %>
<tr>
<td class="text-bold-500"><%= region.name %></td>
<td>1</td>
<td class="text-end">
<%= link_to region, class: "btn btn-primary btn-sm" do %>
<i class="bi bi-search"></i> View
<% end %>
<%= link_to edit_region_path(region), class: "btn btn-secondary btn-sm" do %>
<i class="bi bi-pencil"></i> Edit
<% end %>
<%= link_to region, method: :delete, data: { confirm: "Are you sure?" }, class: "btn btn-danger btn-sm" do %>
<i class="bi bi-trash"></i> Delete
<% end %>
</td>
</tr>
<% end %>
<% @regions.each do |region| %>
<tr>
<td class="text-bold-500"><%= region.name %></td>
<td>1</td>
<td class="text-end">
<%= link_to region, class: "btn btn-primary btn-sm" do %>
<i class="bi bi-search"></i> View
<% end %>
<%= link_to edit_region_path(region), class: "btn btn-secondary btn-sm" do %>
<i class="bi bi-pencil"></i> Edit
<% end %>
<%= link_to region, method: :delete, data: { confirm: "Are you sure?" }, class: "btn btn-danger btn-sm" do %>
<i class="bi bi-trash"></i> Delete
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</section>
</div>
</section>

1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Rails.application.routes.draw do
resources :providers
resources :regions
root "home#index"
get "home/index", as: :home
Expand Down
17 changes: 17 additions & 0 deletions db/migrate/20250204084321_create_providers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class CreateProviders < ActiveRecord::Migration[8.0]
def change
create_table :providers do |t|
t.string :name
t.string :provider_type

t.timestamps
end

create_table :branches do |t|
t.belongs_to :provider
t.belongs_to :region

t.timestamps
end
end
end
Loading