Skip to content

Commit

Permalink
Merge pull request #39 from salesforce/add-library-users
Browse files Browse the repository at this point in the history
library users
  • Loading branch information
vswamidass-sfdc authored Jun 18, 2024
2 parents d3716a6 + b2a919f commit 2486203
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 5 deletions.
36 changes: 36 additions & 0 deletions app/controllers/library_users_controller.rb
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

2 changes: 2 additions & 0 deletions app/helpers/library_users_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module LibraryUsersHelper
end
2 changes: 1 addition & 1 deletion app/models/library.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ class Library < ApplicationRecord
validates :name, presence: true
belongs_to :user

has_many :library_users
has_many :library_users, dependent: :destroy
has_many :users, through: :library_users
end
2 changes: 1 addition & 1 deletion app/policies/document_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ def update?

def user_is_editor?
library_user = LibraryUser.find_by(user: user, library: document.library)
library_user&.editor?
library_user&.editor? || library_user&.admin?
end
end
2 changes: 1 addition & 1 deletion app/policies/library_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ def update?

def user_is_editor?
library_user = LibraryUser.find_by(user: user, library: library)
library_user&.editor?
library_user&.editor? || library_user&.admin?
end
end
18 changes: 18 additions & 0 deletions app/policies/library_user_policy.rb
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
2 changes: 1 addition & 1 deletion app/views/libraries/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<% if policy(@library).edit? %>
<%= link_to 'New Document', new_document_path(library_id: @library.id), class: "rounded-lg p-2 bg-sky-600 text-white" %>
<%= link_to 'Edit', edit_library_path(@library), title: "Edit Library", class: "rounded-lg p-2 bg-sky-600 text-white" %>
<%= link_to 'Users', users_library_path(@library), class: "rounded-lg p-2 bg-sky-600 text-white" %>
<%= link_to 'Users', library_library_users_path(@library), class: "rounded-lg p-2 bg-sky-600 text-white" %>
<% end %>
</div>
</div>
Expand Down
21 changes: 21 additions & 0 deletions app/views/library_users/index.html.erb
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>
27 changes: 27 additions & 0 deletions app/views/library_users/new.html.erb
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' %>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

# Nested Resources
resources :libraries do
resources :library_users
resources :documents
member do
get 'users'
Expand Down
2 changes: 1 addition & 1 deletion db/schema.rb

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

7 changes: 7 additions & 0 deletions spec/requests/library_users_spec.rb
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

0 comments on commit 2486203

Please sign in to comment.