From c7555bac99e68d0360bde31e2424d207585bc9f2 Mon Sep 17 00:00:00 2001 From: Vijay Swamidass Date: Mon, 17 Jun 2024 23:32:25 -0700 Subject: [PATCH] library users --- app/controllers/library_users_controller.rb | 36 +++++++++++++++++++++ app/helpers/library_users_helper.rb | 2 ++ app/models/library.rb | 2 +- app/policies/library_user_policy.rb | 18 +++++++++++ app/views/libraries/show.html.erb | 2 +- app/views/library_users/index.html.erb | 21 ++++++++++++ app/views/library_users/new.html.erb | 27 ++++++++++++++++ config/routes.rb | 1 + db/schema.rb | 2 +- spec/requests/library_users_spec.rb | 7 ++++ 10 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 app/controllers/library_users_controller.rb create mode 100644 app/helpers/library_users_helper.rb create mode 100644 app/policies/library_user_policy.rb create mode 100644 app/views/library_users/index.html.erb create mode 100644 app/views/library_users/new.html.erb create mode 100644 spec/requests/library_users_spec.rb diff --git a/app/controllers/library_users_controller.rb b/app/controllers/library_users_controller.rb new file mode 100644 index 0000000..59262b4 --- /dev/null +++ b/app/controllers/library_users_controller.rb @@ -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 + \ No newline at end of file diff --git a/app/helpers/library_users_helper.rb b/app/helpers/library_users_helper.rb new file mode 100644 index 0000000..2188beb --- /dev/null +++ b/app/helpers/library_users_helper.rb @@ -0,0 +1,2 @@ +module LibraryUsersHelper +end diff --git a/app/models/library.rb b/app/models/library.rb index 09300fa..8f930f6 100644 --- a/app/models/library.rb +++ b/app/models/library.rb @@ -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 diff --git a/app/policies/library_user_policy.rb b/app/policies/library_user_policy.rb new file mode 100644 index 0000000..fdb6151 --- /dev/null +++ b/app/policies/library_user_policy.rb @@ -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 diff --git a/app/views/libraries/show.html.erb b/app/views/libraries/show.html.erb index 792e623..dcab3e9 100644 --- a/app/views/libraries/show.html.erb +++ b/app/views/libraries/show.html.erb @@ -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 %> diff --git a/app/views/library_users/index.html.erb b/app/views/library_users/index.html.erb new file mode 100644 index 0000000..07c4380 --- /dev/null +++ b/app/views/library_users/index.html.erb @@ -0,0 +1,21 @@ +
<%= link_to @library.name, library_path(@library) %>
+ +
+

Users

+ <% 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 %> +
+ +
+
+
Email
+
Role
+
+ <% @users.each do |user| %> +
+
<%= user.email %>
+
<%= user.library_users.find_by(library: @library).role.capitalize %>
+
+ <% end %> +
diff --git a/app/views/library_users/new.html.erb b/app/views/library_users/new.html.erb new file mode 100644 index 0000000..5e6396d --- /dev/null +++ b/app/views/library_users/new.html.erb @@ -0,0 +1,27 @@ +

New Library User

+ +<%= form_with(model: [@library, @library_user], class: "contents") do |form| %> + <% if @library_user.errors.any? %> +
+

<%= pluralize(@library_user.errors.count, "error") %> prohibited this library_user from being saved:

+ + +
+ <% end %> + + +
+ <%= 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" } %> +
+ +
+ <%= form.submit "Save", class: "rounded-lg py-3 px-5 bg-sky-500 text-white inline-block font-medium cursor-pointer" %> +
+<% 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' %> diff --git a/config/routes.rb b/config/routes.rb index 58e79e3..10b9f9d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -20,6 +20,7 @@ # Nested Resources resources :libraries do + resources :library_users resources :documents member do get 'users' diff --git a/db/schema.rb b/db/schema.rb index 3ac9495..521f300 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2024_05_15_220929) do +ActiveRecord::Schema[7.1].define(version: 2024_05_15_220929) do # These are extensions that must be enabled in order to support this database enable_extension "pg_stat_statements" enable_extension "plpgsql" diff --git a/spec/requests/library_users_spec.rb b/spec/requests/library_users_spec.rb new file mode 100644 index 0000000..a37083a --- /dev/null +++ b/spec/requests/library_users_spec.rb @@ -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