-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
115 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
class LibraryUsersController < ApplicationController | ||
def new | ||
@library = Library.find(params[:library_id]) | ||
@library_user = @library.library_users.build | ||
end | ||
|
||
def index | ||
@library = Library.find(params[:library_id]) | ||
@users = @library.users | ||
|
||
respond_to do |format| | ||
format.html # renders users.html.erb | ||
format.json { render json: @users } | ||
end | ||
end | ||
|
||
def create | ||
@library = Library.find(params[:library_id]) | ||
@library_user = @library.library_users.build(library_user_params) | ||
|
||
authorize @library_user | ||
|
||
if @library_user.save | ||
redirect_to library_library_users_path(@library), notice: 'Library user was successfully created.' | ||
else | ||
render :new | ||
end | ||
end | ||
|
||
private | ||
|
||
def library_user_params | ||
params.require(:library_user).permit(:user_id) | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module LibraryUsersHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
class LibraryUserPolicy < ApplicationPolicy | ||
attr_reader :user, :library_user | ||
|
||
def initialize(user, library_user) | ||
@user = user | ||
@library_user = library_user | ||
end | ||
|
||
def create? | ||
user.admin? | ||
end | ||
|
||
def update? | ||
false | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<div><%= link_to @library.name, library_path(@library) %></div> | ||
|
||
<div class="flex justify-between items-center py-2"> | ||
<h1 class="font-bold text-3xl text-sky-800">Users</h1> | ||
<% if policy(Library.new).create? %> | ||
<%= link_to 'New', new_library_library_user_path(@library), class: "rounded-lg p-2 bg-sky-600 text-white block font-light" %> | ||
<% end %> | ||
</div> | ||
|
||
<div class="bg-white shadow-md rounded-lg overflow-hidden"> | ||
<div class="grid grid-cols-3 gap-4 p-4 bg-gray-200 font-semibold"> | ||
<div>Email</div> | ||
<div>Role</div> | ||
</div> | ||
<% @users.each do |user| %> | ||
<div class="grid grid-cols-3 gap-4 p-4 border-b border-gray-200"> | ||
<div><%= user.email %></div> | ||
<div><%= user.library_users.find_by(library: @library).role.capitalize %></div> | ||
</div> | ||
<% end %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<h1>New Library User</h1> | ||
|
||
<%= form_with(model: [@library, @library_user], class: "contents") do |form| %> | ||
<% if @library_user.errors.any? %> | ||
<div id="error_explanation" class="bg-red-50 text-red-500 px-3 py-2 font-medium rounded-lg mt-3"> | ||
<h2><%= pluralize(@library_user.errors.count, "error") %> prohibited this library_user from being saved:</h2> | ||
|
||
<ul> | ||
<% @library_user.errors.each do |error| %> | ||
<li><%= error.full_message %></li> | ||
<% end %> | ||
</ul> | ||
</div> | ||
<% end %> | ||
|
||
<!-- User ID selection --> | ||
<div class="my-5"> | ||
<%= form.label :user_id, style: "display: block" %> | ||
<%= form.collection_select :user_id, User.order(email: :asc), :id, :email, { prompt: 'Select User' }, { class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" } %> | ||
</div> | ||
|
||
<div class="inline"> | ||
<%= form.submit "Save", class: "rounded-lg py-3 px-5 bg-sky-500 text-white inline-block font-medium cursor-pointer" %> | ||
</div> | ||
<% end %> | ||
<%= link_to 'Back', library_path(@library), class: 'rounded-lg py-3 px-5 bg-gray-500 text-white inline-block font-medium cursor-pointer' %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe "LibraryUsers", type: :request do | ||
describe "GET /index" do | ||
pending "add some examples (or delete) #{__FILE__}" | ||
end | ||
end |