Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Styled users and associated providers #44

Merged
merged 2 commits into from
Jan 20, 2025
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
2 changes: 1 addition & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ def set_user

# Only allow a list of trusted parameters through.
def user_params
params.expect(user: [ :email_address, :password, :is_admin ])
params.expect(user: [ :email_address, :password, :is_admin, :provider_id ])
end
end
2 changes: 2 additions & 0 deletions app/models/provider.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Provider < ApplicationRecord
validates :name, presence: true
validates :name, uniqueness: true

has_many :users
end
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class User < ApplicationRecord
has_secure_password
has_many :sessions, dependent: :destroy
belongs_to :provider, optional: true

validates :email_address, presence: true
validates :email_address, uniqueness: true
Expand Down
65 changes: 28 additions & 37 deletions app/views/users/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,48 +1,39 @@
<%= form_with(model: user) do |form| %>
<% if user.errors.any? %>
<div style="color: red">
<h2><%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:</h2>

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

<div class="form-body">
<div class="row">
<div class="col-md-4">
<%= form.label :email_address, style: "display: block" %>
<div class="card-content">
<div class="card-body">
<p>Regions 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>
<%= form_with(model: @user, local: true, class: "form") do |f| %>
<div class="row">
<div class="col-md-12 col-12">
<div class="form-group">
<%= f.label :email_address, "Email Address" %>
<%= f.text_field :email_address, class: "form-control", placeholder: "[email protected]" %>
</div>
</div>
<div class="row">
<div class="col-md-4">
<%= f.label :provider_id, style: "display: block" %>
</div>
<div class="col-md-8 form-group">
<%= form.text_field :email_address %>
<%= f.collection_select(:provider_id, Provider.all, :id, :name, include_blank: "No provider (user must be an admin)") %>
</div>
</div>
<% if @user.new_record? %>
<div class="row">
<div class="col-md-4">
<%= form.label :password, style: "display: block" %>
</div>
<div class="col-md-8 form-group">
<%= form.text_field :password %>
</div>
</div>
<% end %>
<div class="row">
<div class="col-md-4">
<%= form.label :is_admin, style: "display: block" %>
<%= f.label :is_admin, style: "display: block" %>
</div>
<div class="col-md-8 form-group">
<%= form.checkbox :is_admin %>
<%= f.checkbox :is_admin %>
</div>
</div>
<div class="col-12 d-flex justify-content-end">
<%= submit_button(f) %>
<%= link_to "Cancel", users_path, class: "btn btn-light-secondary me-1 mb-1" %>
</div>
</div>
<% end %>
</div>
</div>


<div class="col-sm-12 d-flex justify-content-end">
<%= form.submit class: "btn btn-primary me-1 mb-1" %>
</div>

<% end %>
5 changes: 5 additions & 0 deletions app/views/users/_user.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@
<%= user.is_admin %>
</p>

<p>
<strong>Provider:</strong>
<%= user.provider.present? ? user.provider.name : "No provider" %>
</p>

</div>
22 changes: 12 additions & 10 deletions app/views/users/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<% content_for :title, "Editing user" %>

<h1>Editing user</h1>

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

<br>

<div>
<%= link_to "Show this user", @user %> |
<%= link_to "Back to users", users_path %>
</div>
<section id="multiple-column-form">
<div class="row match-height">
<div class="col-12">
<div class="card">
<div class="card-header">
<h3>Edit Users</h3>
</div>
<%= render "form", user:@user %>
</div>
</div>
</div>
</section>
67 changes: 51 additions & 16 deletions app/views/users/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,16 +1,51 @@
<p style="color: green"><%= notice %></p>

<% content_for :title, "Users" %>

<h1>Users</h1>

<div id="users">
<% @users.each do |user| %>
<%= render user %>
<p>
<%= link_to "Show this user", user %>
</p>
<% end %>
</div>

<%= link_to "New user", new_user_path %>
<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>Users</h2>
<%= link_to new_user_path, class: "btn btn-primary" do %>
<i class="bi bi-plus-circle-fill"></i> Add New User
<% 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>Users</th>
<th>Provider</th>
<th class="text-end">User Actions</th>
</tr>
</thead>
<tbody>
<% @users.each do |user| %>
<tr id="<%= dom_id(user) %>" class="user-listing">
<td class="text-bold-500"><%= user.email_address %></td>
<td><%= user.provider.name if user.provider.present? %></td>
<td class="text-end">
<%= show_page_tool_link(user, "Show User Details") %>
<%= link_to edit_user_path(user), class: "btn btn-secondary btn-sm" do %>
<i class="bi bi-pencil"></i>
<% end %>
<%= delete_tool_link(user, "Delete User") %>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</section>
23 changes: 12 additions & 11 deletions app/views/users/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<% content_for :title, "New user" %>

<h1>New user</h1>

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

<br>

<div>
<%= link_to "Back to users", users_path %>
</div>
<section id="multiple-column-form">
<div class="row match-height">
<div class="col-12">
<div class="card">
<div class="card-header">
<h3>Create New User</h3>
</div>
<%= render "form", user:@user %>
</div>
</div>
</div>
</section>
29 changes: 23 additions & 6 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
<p style="color: green"><%= notice %></p>

<%= render @user %>

<div>
<%= link_to "Edit this user", edit_user_path(@user) %> |
<%= link_to "Back to users", users_path %>
<section id="multiple-column-form">
<div class="row match-height">
<div class="col-12">
<div class="card">
<div class="card-header">
<h2>User</h2>
<h4><%= @user.email_address %></h4>
</div>
<div class="card-content">
<div class="card-body">
<%= render @user %>

<%= button_to "Destroy this user", @user, method: :delete %>
</div>
<div>
<%= link_to "Edit this user", edit_user_path(@user) %> |
<%= link_to "Back to users", users_path %>

<%= button_to "Destroy this user", @user, method: :delete %>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
5 changes: 5 additions & 0 deletions db/migrate/20250119200109_add_provider_id_to_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddProviderIdToUser < ActiveRecord::Migration[8.0]
def change
add_column :users, :provider_id, :integer
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions spec/models/provider_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@
it { should validate_presence_of(:name) }
it { should validate_uniqueness_of(:name) }
end

describe 'associations' do
subject { FactoryBot.build(:provider) }
it { should have_many :users }
end
end
5 changes: 5 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@
it { should validate_presence_of(:email_address) }
it { should validate_uniqueness_of(:email_address).ignoring_case_sensitivity }
end

describe 'associations' do
subject { FactoryBot.build(:user) }
it { should belong_to(:provider).optional }
end
end
5 changes: 2 additions & 3 deletions spec/views/users/index.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@

it "renders a list of users" do
render
cell_selector = 'div>p'
assert_select cell_selector, text: Regexp.new("Email address".to_s), count: 2
assert_select cell_selector, text: Regexp.new(false.to_s), count: 2
cell_selector = 'tr.user-listing'
assert_select cell_selector, text: Regexp.new("email address".to_s), count: 2
end
end
3 changes: 0 additions & 3 deletions spec/views/users/new.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@

assert_select "form[action=?][method=?]", users_path, "post" do
assert_select "input[name=?]", "user[email_address]"

assert_select "input[name=?]", "user[password]"

assert_select "input[name=?]", "user[is_admin]"
end
end
Expand Down
Loading