From 516642dc9567cdfbd3debc244f75fb5dbf59f3d6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marius=20D=C3=B6rbandt?=
Date: Fri, 2 Dec 2022 20:08:03 +0100
Subject: [PATCH 01/27] Fix notifications sorting
---
app/controllers/notifications_controller.rb | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb
index 07904ed0..2d9ab893 100644
--- a/app/controllers/notifications_controller.rb
+++ b/app/controllers/notifications_controller.rb
@@ -5,6 +5,6 @@ def index
@dates = @notifications.group_by do |notification|
notification.date.strftime('%d. %B %y')
end
- @dates.sort_by { |date, _| date }.reverse
+ @dates = @dates.sort_by { |date, _| date }.reverse
end
end
From 409e8bd17df9566fac5abc3fee6f78f8a646efe8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marius=20D=C3=B6rbandt?=
Date: Fri, 2 Dec 2022 20:12:30 +0100
Subject: [PATCH 02/27] Clean up notifications index page
---
app/views/notifications/index.html.erb | 12 +++++++-----
1 file changed, 7 insertions(+), 5 deletions(-)
diff --git a/app/views/notifications/index.html.erb b/app/views/notifications/index.html.erb
index d551d02d..bd0ff321 100644
--- a/app/views/notifications/index.html.erb
+++ b/app/views/notifications/index.html.erb
@@ -1,16 +1,18 @@
<%= notice %>
- <% @dates.each do |date| %>
+ <% @dates.each do |(date, notifications)| %>
- <%= date[0] %>
+ <%= date %>
-
- <% date[1].each do |notification|%>
+
+ <% notifications.each do |notification|%>
+
<%= link_to notification.notification_snippet, notifications_path %>
+
<% end %>
<% end %>
-
\ No newline at end of file
+
From 59b30160a24eb6972e3952a89655356e1f8ba02d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marius=20D=C3=B6rbandt?=
Date: Fri, 2 Dec 2022 20:16:24 +0100
Subject: [PATCH 03/27] Remove notification snippet from Notification
---
app/views/notifications/index.html.erb | 1 -
db/migrate/20221128175122_remove_notification_snippet.rb | 5 +++++
db/schema.rb | 3 +--
3 files changed, 6 insertions(+), 3 deletions(-)
create mode 100644 db/migrate/20221128175122_remove_notification_snippet.rb
diff --git a/app/views/notifications/index.html.erb b/app/views/notifications/index.html.erb
index bd0ff321..5b74a7c8 100644
--- a/app/views/notifications/index.html.erb
+++ b/app/views/notifications/index.html.erb
@@ -9,7 +9,6 @@
<% notifications.each do |notification|%>
- <%= link_to notification.notification_snippet, notifications_path %>
<% end %>
diff --git a/db/migrate/20221128175122_remove_notification_snippet.rb b/db/migrate/20221128175122_remove_notification_snippet.rb
new file mode 100644
index 00000000..b979df6c
--- /dev/null
+++ b/db/migrate/20221128175122_remove_notification_snippet.rb
@@ -0,0 +1,5 @@
+class RemoveNotificationSnippet < ActiveRecord::Migration[7.0]
+ def change
+ remove_column :notifications, :notification_snippet, :string
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index e3b9f4b0..a4a412c2 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: 2022_11_22_114920) do
+ActiveRecord::Schema[7.0].define(version: 2022_11_28_175122) do
create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
@@ -57,7 +57,6 @@
end
create_table "notifications", force: :cascade do |t|
- t.string "notification_snippet"
t.datetime "date"
t.integer "user_id", null: false
t.datetime "created_at", null: false
From dcf46791eaa194b67fff8d54a6ba4f51a4874c47 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marius=20D=C3=B6rbandt?=
Date: Fri, 2 Dec 2022 20:22:00 +0100
Subject: [PATCH 04/27] Add LendRequestNotification
---
app/models/lend_request_notification.rb | 4 ++++
...21128184413_create_lend_request_notifications.rb | 10 ++++++++++
db/schema.rb | 13 ++++++++++++-
spec/factories/lend_request_notifications.rb | 6 ++++++
spec/models/lend_request_notification_spec.rb | 5 +++++
5 files changed, 37 insertions(+), 1 deletion(-)
create mode 100644 app/models/lend_request_notification.rb
create mode 100644 db/migrate/20221128184413_create_lend_request_notifications.rb
create mode 100644 spec/factories/lend_request_notifications.rb
create mode 100644 spec/models/lend_request_notification_spec.rb
diff --git a/app/models/lend_request_notification.rb b/app/models/lend_request_notification.rb
new file mode 100644
index 00000000..096fddf0
--- /dev/null
+++ b/app/models/lend_request_notification.rb
@@ -0,0 +1,4 @@
+class LendRequestNotification < ApplicationRecord
+ belongs_to :borrower, class_name: "User"
+ belongs_to :item
+end
diff --git a/db/migrate/20221128184413_create_lend_request_notifications.rb b/db/migrate/20221128184413_create_lend_request_notifications.rb
new file mode 100644
index 00000000..44966e3c
--- /dev/null
+++ b/db/migrate/20221128184413_create_lend_request_notifications.rb
@@ -0,0 +1,10 @@
+class CreateLendRequestNotifications < ActiveRecord::Migration[7.0]
+ def change
+ create_table :lend_request_notifications do |t|
+ t.references :borrower, null: false, foreign_key: { to_table: :users }
+ t.references :item, null: false, foreign_key: true
+
+ t.timestamps
+ end
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index a4a412c2..8f64acd1 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: 2022_11_28_175122) do
+ActiveRecord::Schema[7.0].define(version: 2022_11_28_184413) do
create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
@@ -56,6 +56,15 @@
t.index ["owner"], name: "index_items_on_owner"
end
+ create_table "lend_request_notifications", force: :cascade do |t|
+ t.integer "borrower_id", null: false
+ t.integer "item_id", null: false
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ t.index ["borrower_id"], name: "index_lend_request_notifications_on_borrower_id"
+ t.index ["item_id"], name: "index_lend_request_notifications_on_item_id"
+ end
+
create_table "notifications", force: :cascade do |t|
t.datetime "date"
t.integer "user_id", null: false
@@ -80,5 +89,7 @@
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
add_foreign_key "items", "users", column: "holder"
add_foreign_key "items", "users", column: "owner"
+ add_foreign_key "lend_request_notifications", "items"
+ add_foreign_key "lend_request_notifications", "users", column: "borrower_id"
add_foreign_key "notifications", "users"
end
diff --git a/spec/factories/lend_request_notifications.rb b/spec/factories/lend_request_notifications.rb
new file mode 100644
index 00000000..7cc27613
--- /dev/null
+++ b/spec/factories/lend_request_notifications.rb
@@ -0,0 +1,6 @@
+FactoryBot.define do
+ factory :lend_request_notification do
+ borrower { nil }
+ item { nil }
+ end
+end
diff --git a/spec/models/lend_request_notification_spec.rb b/spec/models/lend_request_notification_spec.rb
new file mode 100644
index 00000000..c1a00cab
--- /dev/null
+++ b/spec/models/lend_request_notification_spec.rb
@@ -0,0 +1,5 @@
+require 'rails_helper'
+
+RSpec.describe LendRequestNotification, type: :model do
+ pending "add some examples to (or delete) #{__FILE__}"
+end
From ae752f35c82a01f02882fbd9968f03abe6e7b7ec Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marius=20D=C3=B6rbandt?=
Date: Fri, 2 Dec 2022 20:23:33 +0100
Subject: [PATCH 05/27] Make Notification actable
---
Gemfile | 3 +++
Gemfile.lock | 6 ++++++
app/models/notification.rb | 2 ++
db/migrate/20221202144014_add_actable_to_notification.rb | 7 +++++++
db/schema.rb | 5 ++++-
5 files changed, 22 insertions(+), 1 deletion(-)
create mode 100644 db/migrate/20221202144014_add_actable_to_notification.rb
diff --git a/Gemfile b/Gemfile
index ddeeb82c..551ff0ac 100644
--- a/Gemfile
+++ b/Gemfile
@@ -9,6 +9,9 @@ gem "rails", "~> 7.0.4"
# The original asset pipeline for Rails [https://github.com/rails/sprockets-rails]
gem "sprockets-rails"
+# Support for MTI (Multiple Table Inheritance)
+gem "active_record-acts_as"
+
# Use the Puma web server [https://github.com/puma/puma]
gem "puma", "~> 5.0"
diff --git a/Gemfile.lock b/Gemfile.lock
index 288f7b88..80687ff3 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -58,6 +58,10 @@ GEM
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.1, >= 1.2.0)
+ active_record-acts_as (5.0.3)
+ activerecord (>= 6.0)
+ activesupport (>= 6.0)
+ ruby2_keywords
activejob (7.0.4)
activesupport (= 7.0.4)
globalid (>= 0.3.6)
@@ -250,6 +254,7 @@ GEM
rubocop-rspec (2.15.0)
rubocop (~> 1.33)
ruby-progressbar (1.11.0)
+ ruby2_keywords (0.0.5)
rubyzip (2.3.2)
sassc (2.4.0)
ffi (~> 1.9)
@@ -317,6 +322,7 @@ PLATFORMS
ruby
DEPENDENCIES
+ active_record-acts_as
bootsnap
bootstrap (~> 5.2.2)
capybara
diff --git a/app/models/notification.rb b/app/models/notification.rb
index c99183b1..dcb86e96 100644
--- a/app/models/notification.rb
+++ b/app/models/notification.rb
@@ -1,3 +1,5 @@
class Notification < ApplicationRecord
+ actable
+
belongs_to :user
end
diff --git a/db/migrate/20221202144014_add_actable_to_notification.rb b/db/migrate/20221202144014_add_actable_to_notification.rb
new file mode 100644
index 00000000..c39595c4
--- /dev/null
+++ b/db/migrate/20221202144014_add_actable_to_notification.rb
@@ -0,0 +1,7 @@
+class AddActableToNotification < ActiveRecord::Migration[7.0]
+ def change
+ change_table :notifications do |t|
+ t.actable
+ end
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 8f64acd1..55b01c45 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: 2022_11_28_184413) do
+ActiveRecord::Schema[7.0].define(version: 2022_12_02_144014) do
create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
@@ -70,6 +70,9 @@
t.integer "user_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
+ t.string "actable_type"
+ t.integer "actable_id"
+ t.index ["actable_type", "actable_id"], name: "index_notifications_on_actable"
t.index ["user_id"], name: "index_notifications_on_user_id"
end
From f015572a435a09ad45d0ab9eb6ccec03b1a7b6ca Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marius=20D=C3=B6rbandt?=
Date: Fri, 2 Dec 2022 20:24:31 +0100
Subject: [PATCH 06/27] Let LendRequestNotification act_as a Notification
---
app/models/lend_request_notification.rb | 2 ++
1 file changed, 2 insertions(+)
diff --git a/app/models/lend_request_notification.rb b/app/models/lend_request_notification.rb
index 096fddf0..d3514a97 100644
--- a/app/models/lend_request_notification.rb
+++ b/app/models/lend_request_notification.rb
@@ -1,4 +1,6 @@
class LendRequestNotification < ApplicationRecord
+ acts_as :notification
+
belongs_to :borrower, class_name: "User"
belongs_to :item
end
From b123d60425f26d456b4130208c09b8206b5a74e7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marius=20D=C3=B6rbandt?=
Date: Fri, 2 Dec 2022 20:25:31 +0100
Subject: [PATCH 07/27] Add title and description to LendRequestNotification
---
app/models/lend_request_notification.rb | 8 ++++++++
config/locales/de.yml | 3 +++
config/locales/en.yml | 3 +++
3 files changed, 14 insertions(+)
diff --git a/app/models/lend_request_notification.rb b/app/models/lend_request_notification.rb
index d3514a97..41ac5f02 100644
--- a/app/models/lend_request_notification.rb
+++ b/app/models/lend_request_notification.rb
@@ -3,4 +3,12 @@ class LendRequestNotification < ApplicationRecord
belongs_to :borrower, class_name: "User"
belongs_to :item
+
+ def title
+ I18n.t "views.notifications.lend_request.title"
+ end
+
+ def description
+ I18n.t "views.notifications.lend_request.description", user: borrower.name, item: item.name
+ end
end
diff --git a/config/locales/de.yml b/config/locales/de.yml
index 0798dce0..da06320d 100644
--- a/config/locales/de.yml
+++ b/config/locales/de.yml
@@ -14,6 +14,9 @@ de:
title: "Übersicht"
notifications:
title: "Benachrichtigungen"
+ lend_request:
+ title: "Ausleihanfrage"
+ description: "%{user} möchte %{item} von dir ausleihen."
search:
title: "Suche"
home:
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 50c20381..6745b45a 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -45,6 +45,9 @@ en:
title: "Dashboard"
notifications:
title: "Notifications"
+ lend_request:
+ title: "Lend Request"
+ description: "%{user} wants to borrow your %{item}."
search:
title: "Search"
home:
From 3c375fff950008a6b23124bb79e33c60a4119414 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marius=20D=C3=B6rbandt?=
Date: Fri, 2 Dec 2022 20:26:17 +0100
Subject: [PATCH 08/27] Add method delegation to specific Notification class
---
app/models/notification.rb | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/app/models/notification.rb b/app/models/notification.rb
index dcb86e96..d6e30e86 100644
--- a/app/models/notification.rb
+++ b/app/models/notification.rb
@@ -2,4 +2,14 @@ class Notification < ApplicationRecord
actable
belongs_to :user
+
+ # delegate methods from the "subclasses" (which aren't really subclasses)
+ # to the specific instances
+ def method_missing(method, *args, &block)
+ if specific.self_respond_to?(method)
+ specific.send(method, *args, &block)
+ else
+ super
+ end
+ end
end
From 8bb97fdfa3ddf8fdf6db0357cb6fbfbbaea67cc1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marius=20D=C3=B6rbandt?=
Date: Fri, 2 Dec 2022 20:30:00 +0100
Subject: [PATCH 09/27] Show a notification's title and description
---
app/views/notifications/index.html.erb | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/app/views/notifications/index.html.erb b/app/views/notifications/index.html.erb
index 5b74a7c8..77dd21e7 100644
--- a/app/views/notifications/index.html.erb
+++ b/app/views/notifications/index.html.erb
@@ -8,7 +8,11 @@
<% notifications.each do |notification|%>
+
+ <%= notification.title %>
+
+ <%= notification.description %>
<% end %>
From 7f228eac8cc847ad1eea4d5e721181adf2bb276e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marius=20D=C3=B6rbandt?=
Date: Fri, 2 Dec 2022 20:30:51 +0100
Subject: [PATCH 10/27] Add ability to show a custom partial for notifications
---
app/helpers/application_helper.rb | 7 +++++++
app/models/notification.rb | 4 ++++
app/views/notifications/index.html.erb | 1 +
3 files changed, 12 insertions(+)
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index de6be794..bcaf0e55 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -1,2 +1,9 @@
module ApplicationHelper
+ def render_if_exists(path_to_partial, *args, &block)
+ begin
+ render path_to_partial, *args, &block
+ rescue ActionView::MissingTemplate
+ # do nothing
+ end
+ end
end
diff --git a/app/models/notification.rb b/app/models/notification.rb
index d6e30e86..2d4d2f40 100644
--- a/app/models/notification.rb
+++ b/app/models/notification.rb
@@ -3,6 +3,10 @@ class Notification < ApplicationRecord
belongs_to :user
+ def custom_partial
+ specific.class.name.underscore
+ end
+
# delegate methods from the "subclasses" (which aren't really subclasses)
# to the specific instances
def method_missing(method, *args, &block)
diff --git a/app/views/notifications/index.html.erb b/app/views/notifications/index.html.erb
index 77dd21e7..a65781a0 100644
--- a/app/views/notifications/index.html.erb
+++ b/app/views/notifications/index.html.erb
@@ -14,6 +14,7 @@
<%= notification.description %>
+ <%= render_if_exists notification.custom_partial, notification: notification %>
<% end %>
From 3043103a20ab8992f3d27ab6f66d17e4567a93fc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marius=20D=C3=B6rbandt?=
Date: Fri, 2 Dec 2022 20:32:04 +0100
Subject: [PATCH 11/27] Add dummy buttons for LendRequestNotification
---
app/views/notifications/_lend_request_notification.html.erb | 2 ++
config/locales/de.yml | 2 ++
config/locales/en.yml | 2 ++
3 files changed, 6 insertions(+)
create mode 100644 app/views/notifications/_lend_request_notification.html.erb
diff --git a/app/views/notifications/_lend_request_notification.html.erb b/app/views/notifications/_lend_request_notification.html.erb
new file mode 100644
index 00000000..5afffeb5
--- /dev/null
+++ b/app/views/notifications/_lend_request_notification.html.erb
@@ -0,0 +1,2 @@
+<%= button_to t("defaults.accept"), "/" %>
+<%= button_to t("defaults.decline"), "/" %>
diff --git a/config/locales/de.yml b/config/locales/de.yml
index da06320d..074a2a9a 100644
--- a/config/locales/de.yml
+++ b/config/locales/de.yml
@@ -1,6 +1,8 @@
de:
defaults:
delete: "Löschen"
+ accept: "Annehmen"
+ decline: "Ablehnen"
views:
landing_page:
title: "Bookkeeper Blau"
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 6745b45a..ef5ea89d 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -32,6 +32,8 @@
en:
defaults:
delete: "Delete"
+ accept: "Accept"
+ decline: "Decline"
views:
landing_page:
title: "Bookkeeper Blue"
From 9383e81727154776cf9d4aa00c2c6f30cfae3b6e Mon Sep 17 00:00:00 2001
From: LinoH5
Date: Sun, 11 Dec 2022 14:13:10 +0100
Subject: [PATCH 12/27] starts implementing waitlist and adding users to it
---
app/controllers/items_controller.rb | 11 +++++-
app/models/item.rb | 8 ++++
app/models/user.rb | 2 +
app/models/waitlist.rb | 4 ++
app/views/items/show.html.erb | 3 +-
config/routes.rb | 1 +
db/migrate/20221210212739_create_waitlists.rb | 9 +++++
...0213248_create_join_table_user_waitlist.rb | 8 ++++
db/schema.rb | 37 +++++++++++++------
spec/factories/waitlists.rb | 5 +++
spec/models/waitlist_spec.rb | 5 +++
11 files changed, 78 insertions(+), 15 deletions(-)
create mode 100644 app/models/waitlist.rb
create mode 100644 db/migrate/20221210212739_create_waitlists.rb
create mode 100644 db/migrate/20221210213248_create_join_table_user_waitlist.rb
create mode 100644 spec/factories/waitlists.rb
create mode 100644 spec/models/waitlist_spec.rb
diff --git a/app/controllers/items_controller.rb b/app/controllers/items_controller.rb
index 179649ce..2752a377 100644
--- a/app/controllers/items_controller.rb
+++ b/app/controllers/items_controller.rb
@@ -22,6 +22,7 @@ def edit
# POST /items or /items.json
def create
@item = Item.new(item_params)
+ @item.waitlist = Waitlist.new
respond_to do |format|
if @item.save
@@ -57,6 +58,14 @@ def destroy
end
end
+ def add_to_waitlist
+ @item = Item.find(params[:id])
+ @user = current_user
+ @item.add_to_waitlist(@user)
+ @item.save
+ redirect_to item_url(@item)
+ end
+
private
# Use callbacks to share common setup or constraints between actions.
@@ -67,6 +76,6 @@ def set_item
# Only allow a list of trusted parameters through.
def item_params
params.require(:item).permit(:name, :category, :location, :description, :image, :price_ct, :rental_duration_sec,
- :rental_start, :return_checklist, :owner, :holder)
+ :rental_start, :return_checklist, :owner, :holder, :waitlist_id)
end
end
diff --git a/app/models/item.rb b/app/models/item.rb
index b18ae54d..7684ee28 100644
--- a/app/models/item.rb
+++ b/app/models/item.rb
@@ -1,6 +1,7 @@
# class of a basic item.
class Item < ApplicationRecord
has_one_attached :image
+ has_one :waitlist
validates :name, presence: true
validates :category, presence: true
@@ -22,4 +23,11 @@ def price_in_euro
def price_in_euro=(euros)
self.price_ct = euros * 100
end
+
+ def add_to_waitlist (user)
+ #check if not owner and not allready in waitlist, should later on also check if not available
+ if (!self.waitlist.users.exists?(user.id) && self.owner != user.id)
+ self.waitlist.users << user
+ end
+ end
end
diff --git a/app/models/user.rb b/app/models/user.rb
index 4596184d..fd9f85f4 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -12,6 +12,8 @@ class User < ApplicationRecord
has_many :ownerships, class_name: 'Ownership', dependent: :destroy
has_many :owned_groups, through: :ownerships, source: :group
+ has_and_belongs_to_many :waitlists
+
# Method expects all emails to follow format "firstname.lastname@anything" in order to extract first name out of email
def first_name
email.split("@")[0].split(".")[0].capitalize
diff --git a/app/models/waitlist.rb b/app/models/waitlist.rb
new file mode 100644
index 00000000..ac51c771
--- /dev/null
+++ b/app/models/waitlist.rb
@@ -0,0 +1,4 @@
+class Waitlist < ApplicationRecord
+ belongs_to :item
+ has_and_belongs_to_many :users
+end
diff --git a/app/views/items/show.html.erb b/app/views/items/show.html.erb
index e42ef214..8b0f19c8 100644
--- a/app/views/items/show.html.erb
+++ b/app/views/items/show.html.erb
@@ -149,8 +149,7 @@
<%= t('views.show_item.lend_until')%>
<%= ((@item.rental_start.nil? ? Time.now() : @item.rental_start) + (@item.rental_duration_sec.nil? ? 86400 : @item.rental_duration_sec)).strftime('%d.%m.%Y') %>
- <%= button_to t('views.show_item.enter_waitlist'), new_item_path(@item), method: :post, class: "secondaryButton" %>
-
+ <%= button_to t('views.show_item.enter_waitlist'), { action: "add_to_waitlist" }, class: "primaryButton" %>
diff --git a/config/routes.rb b/config/routes.rb
index b40eae56..13aadead 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -12,6 +12,7 @@
end
resources :users
+ post 'add_to_waitlist/:id', to: 'items#add_to_waitlist', as: 'add_to_waitlist'
# Defines the root path route ("/")
root "landing_page#index"
end
diff --git a/db/migrate/20221210212739_create_waitlists.rb b/db/migrate/20221210212739_create_waitlists.rb
new file mode 100644
index 00000000..60247d40
--- /dev/null
+++ b/db/migrate/20221210212739_create_waitlists.rb
@@ -0,0 +1,9 @@
+class CreateWaitlists < ActiveRecord::Migration[7.0]
+ def change
+ create_table :waitlists do |t|
+ t.references :item, null: false, foreign_key: true
+
+ t.timestamps
+ end
+ end
+end
diff --git a/db/migrate/20221210213248_create_join_table_user_waitlist.rb b/db/migrate/20221210213248_create_join_table_user_waitlist.rb
new file mode 100644
index 00000000..3e0392f6
--- /dev/null
+++ b/db/migrate/20221210213248_create_join_table_user_waitlist.rb
@@ -0,0 +1,8 @@
+class CreateJoinTableUserWaitlist < ActiveRecord::Migration[7.0]
+ def change
+ create_join_table :users, :waitlists do |t|
+ # t.index [:user_id, :waitlist_id]
+ # t.index [:waitlist_id, :user_id]
+ end
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index a15e6b06..f025b797 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: 2022_12_05_160740) do
+ActiveRecord::Schema[7.0].define(version: 2022_12_10_213248) do
create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
@@ -62,6 +62,15 @@
t.index ["owner"], name: "index_items_on_owner"
end
+ create_table "lend_request_notifications", force: :cascade do |t|
+ t.integer "borrower_id", null: false
+ t.integer "item_id", null: false
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ t.index ["borrower_id"], name: "index_lend_request_notifications_on_borrower_id"
+ t.index ["item_id"], name: "index_lend_request_notifications_on_item_id"
+ end
+
create_table "memberships", force: :cascade do |t|
t.string "type"
t.integer "group_id", null: false
@@ -72,15 +81,6 @@
t.index ["group_id"], name: "index_memberships_on_group_id"
t.index ["user_id"], name: "index_memberships_on_user_id"
end
-
- create_table "lend_request_notifications", force: :cascade do |t|
- t.integer "borrower_id", null: false
- t.integer "item_id", null: false
- t.datetime "created_at", null: false
- t.datetime "updated_at", null: false
- t.index ["borrower_id"], name: "index_lend_request_notifications_on_borrower_id"
- t.index ["item_id"], name: "index_lend_request_notifications_on_item_id"
- end
create_table "notifications", force: :cascade do |t|
t.datetime "date"
@@ -105,13 +105,26 @@
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
+ create_table "users_waitlists", id: false, force: :cascade do |t|
+ t.integer "user_id", null: false
+ t.integer "waitlist_id", null: false
+ end
+
+ create_table "waitlists", force: :cascade do |t|
+ t.integer "item_id", null: false
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ t.index ["item_id"], name: "index_waitlists_on_item_id"
+ end
+
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
add_foreign_key "items", "users", column: "holder"
add_foreign_key "items", "users", column: "owner"
- add_foreign_key "memberships", "groups"
- add_foreign_key "memberships", "users"
add_foreign_key "lend_request_notifications", "items"
add_foreign_key "lend_request_notifications", "users", column: "borrower_id"
+ add_foreign_key "memberships", "groups"
+ add_foreign_key "memberships", "users"
add_foreign_key "notifications", "users"
+ add_foreign_key "waitlists", "items"
end
diff --git a/spec/factories/waitlists.rb b/spec/factories/waitlists.rb
new file mode 100644
index 00000000..1335f320
--- /dev/null
+++ b/spec/factories/waitlists.rb
@@ -0,0 +1,5 @@
+FactoryBot.define do
+ factory :waitlist do
+ item { nil }
+ end
+end
diff --git a/spec/models/waitlist_spec.rb b/spec/models/waitlist_spec.rb
new file mode 100644
index 00000000..1b8f43be
--- /dev/null
+++ b/spec/models/waitlist_spec.rb
@@ -0,0 +1,5 @@
+require 'rails_helper'
+
+RSpec.describe Waitlist, type: :model do
+ pending "add some examples to (or delete) #{__FILE__}"
+end
From cda5b8d792eacad4d03cf5c35d64ee391c488257 Mon Sep 17 00:00:00 2001
From: LinoH5
Date: Mon, 12 Dec 2022 15:56:31 +0100
Subject: [PATCH 13/27] implements leaving waitlist, notifications, status
info, refactoring
---
app/controllers/items_controller.rb | 16 +++++
app/models/added_to_waitlist_notification.rb | 13 ++++
app/models/item.rb | 9 +--
.../move_up_on_waitlist_notification.rb | 13 ++++
app/models/waitlist.rb | 60 +++++++++++++++++++
app/views/items/show.html.erb | 14 ++++-
config/locales/de.yml | 13 ++++
config/locales/en.yml | 13 ++++
config/routes.rb | 1 +
..._create_added_to_waitlist_notifications.rb | 9 +++
...reate_move_up_on_waitlist_notifications.rb | 9 +++
db/schema.rb | 18 +++++-
.../added_to_waitlist_notifications.rb | 6 ++
.../move_up_on_waitlist_notifications.rb | 5 ++
.../added_to_waitlist_notification_spec.rb | 5 ++
.../move_up_on_waitlist_notification_spec.rb | 5 ++
16 files changed, 203 insertions(+), 6 deletions(-)
create mode 100644 app/models/added_to_waitlist_notification.rb
create mode 100644 app/models/move_up_on_waitlist_notification.rb
create mode 100644 db/migrate/20221212102651_create_added_to_waitlist_notifications.rb
create mode 100644 db/migrate/20221212120242_create_move_up_on_waitlist_notifications.rb
create mode 100644 spec/factories/added_to_waitlist_notifications.rb
create mode 100644 spec/factories/move_up_on_waitlist_notifications.rb
create mode 100644 spec/models/added_to_waitlist_notification_spec.rb
create mode 100644 spec/models/move_up_on_waitlist_notification_spec.rb
diff --git a/app/controllers/items_controller.rb b/app/controllers/items_controller.rb
index 2752a377..3f152268 100644
--- a/app/controllers/items_controller.rb
+++ b/app/controllers/items_controller.rb
@@ -62,6 +62,22 @@ def add_to_waitlist
@item = Item.find(params[:id])
@user = current_user
@item.add_to_waitlist(@user)
+
+ respond_to do |format|
+ if @item.save
+ format.html { redirect_to item_url(@item), notice: t("models.waitlist.added_to_waitlist", position: @item.waitlist.position(@user) + 1) }
+ #format.json { render :show, status: :ok, location: @item }
+ else
+ format.html { redirect_to item_url(@item), alert: t("models.waitlist.failed_adding_to_waitlist") }
+ #format.json { render :show, status: :unprocessable_entity, location: @item }
+ end
+ end
+ end
+
+ def leave_waitlist
+ @item = Item.find(params[:id])
+ @user = current_user
+ @item.remove_from_waitlist(@user)
@item.save
redirect_to item_url(@item)
end
diff --git a/app/models/added_to_waitlist_notification.rb b/app/models/added_to_waitlist_notification.rb
new file mode 100644
index 00000000..39d79b33
--- /dev/null
+++ b/app/models/added_to_waitlist_notification.rb
@@ -0,0 +1,13 @@
+class AddedToWaitlistNotification < ApplicationRecord
+ acts_as :notification
+
+ belongs_to :item
+
+ def title
+ I18n.t("views.notifications.added_to_waitlist.title")
+ end
+
+ def description
+ I18n.t("views.notifications.added_to_waitlist.description", position: item.waitlist.position(user) + 1, item: item.name)
+ end
+end
diff --git a/app/models/item.rb b/app/models/item.rb
index 7684ee28..17968a73 100644
--- a/app/models/item.rb
+++ b/app/models/item.rb
@@ -25,9 +25,10 @@ def price_in_euro=(euros)
end
def add_to_waitlist (user)
- #check if not owner and not allready in waitlist, should later on also check if not available
- if (!self.waitlist.users.exists?(user.id) && self.owner != user.id)
- self.waitlist.users << user
- end
+ self.waitlist.add_user(user)
+ end
+
+ def remove_from_waitlist (user)
+ self.waitlist.remove_user(user)
end
end
diff --git a/app/models/move_up_on_waitlist_notification.rb b/app/models/move_up_on_waitlist_notification.rb
new file mode 100644
index 00000000..ffb7649e
--- /dev/null
+++ b/app/models/move_up_on_waitlist_notification.rb
@@ -0,0 +1,13 @@
+class MoveUpOnWaitlistNotification < ApplicationRecord
+ acts_as :notification
+
+ belongs_to :item
+
+ def title
+ I18n.t("views.notifications.move_up_on_waitlist.title")
+ end
+
+ def description
+ I18n.t("views.notifications.move_up_on_waitlist.description", position: item.waitlist.position(user) + 1, item: item.name)
+ end
+end
diff --git a/app/models/waitlist.rb b/app/models/waitlist.rb
index ac51c771..4459ed03 100644
--- a/app/models/waitlist.rb
+++ b/app/models/waitlist.rb
@@ -1,4 +1,64 @@
class Waitlist < ApplicationRecord
belongs_to :item
has_and_belongs_to_many :users
+
+ def position (user)
+ self.users.find_index(user)
+ end
+
+ def first_user
+ self.users.first
+ end
+
+ def add_user (user)
+ if (!self.users.exists?(user.id) && user.id != self.item.owner)
+ self.users << user
+ self.add_added_to_waitlist_notification(user)
+ end
+ end
+
+ def remove_user (user)
+ user_index = self.users.find_index(user)
+ self.users.delete(user)
+
+ self.delete_waitlist_notifications(user)
+
+ unless user_index.nil? || user_index >= self.users.size
+ self.users[user_index..-1].each do |temp_user|
+ self.delete_waitlist_notifications(temp_user)
+ self.add_move_up_on_waitlist_notification(temp_user)
+ end
+ end
+ end
+
+ private
+
+ def delete_waitlist_notifications (user)
+ self.delete_added_to_waitlist_notification(user)
+ self.delete_moved_up_on_waitlist_notification(user)
+ end
+
+ def add_added_to_waitlist_notification (user)
+ @notification = AddedToWaitlistNotification.new(user: user, date: Time.now, item: item)
+ @notification.save
+ end
+
+ def add_move_up_on_waitlist_notification (user)
+ @notification = MoveUpOnWaitlistNotification.new(user: user, date: Time.now, item: item)
+ @notification.save
+ end
+
+ def delete_added_to_waitlist_notification (user)
+ @notification = AddedToWaitlistNotification.find_by(item: item, user: user)
+ unless @notification.nil?
+ @notification.destroy
+ end
+ end
+
+ def delete_moved_up_on_waitlist_notification (user)
+ @notification = MoveUpOnWaitlistNotification.find_by(item: item, user: user)
+ unless @notification.nil?
+ @notification.destroy
+ end
+ end
end
diff --git a/app/views/items/show.html.erb b/app/views/items/show.html.erb
index 8b0f19c8..2fcc47ec 100644
--- a/app/views/items/show.html.erb
+++ b/app/views/items/show.html.erb
@@ -148,8 +148,20 @@
<%= User.find(@item.owner).email %>
<%= t('views.show_item.lend_until')%>
<%= ((@item.rental_start.nil? ? Time.now() : @item.rental_start) + (@item.rental_duration_sec.nil? ? 86400 : @item.rental_duration_sec)).strftime('%d.%m.%Y') %>
+ <%= t('views.show_item.waitlist')%>
+
+ <% if !@item.waitlist.users.exists?(current_user.id) %>
+ <%= t('views.show_item.users_waiting', amount: @item.waitlist.users.size) %>
+ <% else %>
+ <%= t('views.show_item.waitlist_position', position: @item.waitlist.users.find_index(current_user) + 1) %>
+ <% end %>
+
- <%= button_to t('views.show_item.enter_waitlist'), { action: "add_to_waitlist" }, class: "primaryButton" %>
+ <% if !@item.waitlist.users.exists?(current_user.id) %>
+ <%= button_to t('views.show_item.enter_waitlist'), { action: "add_to_waitlist" }, class: "primaryButton" %>
+ <% else %>
+ <%= button_to t('views.show_item.leave_waitlist'), { action: "leave_waitlist" }, class: "primaryButton" %>
+ <% end %>
diff --git a/config/locales/de.yml b/config/locales/de.yml
index 3f49d729..bcb38f9d 100644
--- a/config/locales/de.yml
+++ b/config/locales/de.yml
@@ -27,6 +27,12 @@ de:
lend_request:
title: "Ausleihanfrage"
description: "%{user} möchte %{item} von dir ausleihen."
+ added_to_waitlist:
+ title: "Zur Warteliste hinzugefügt"
+ description: "Du wurdest an Position %{position} zur Warteliste für %{item} hinzugefügt."
+ move_up_on_waitlist:
+ title: "Auf Warteliste aufgerückt"
+ description: "Du bist auf Position %{position} der Warteliste für %{item} aufgerückt."
search:
title: "Suche"
filter_button: "Filter"
@@ -42,6 +48,10 @@ de:
owner: "Besitzer"
lend_until: "Ausleihen bis"
enter_waitlist: "Auf die Warteliste"
+ leave_waitlist: "Warteliste verlassen"
+ waitlist: "Warteliste"
+ users_waiting: "%{amount} Nutzer warten auf dieses Objekt."
+ waitlist_position: "Sie stehen an Position %{position} der Warteliste."
lend: "Ausleihen"
edit_item:
rental_duration_sec: "Ausleihdauer (Sekunden)"
@@ -53,6 +63,9 @@ de:
created: "Gegenstand wurde erfolgreich erstellt."
updated: "Gegenstand wurde erfolgreich verändert."
destroyed: "Gegenstand wurde erfolgreich entfernt."
+ waitlist:
+ added_to_waitlist: "Zur Warteliste an Position %{position} hinzugefügt."
+ failed_adding_to_waitlist: "Nicht zur Warteliste hinzugefügt."
helpers:
submit:
update: "Gegendstand ändern"
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 555c40e4..a4a784f9 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -58,6 +58,12 @@ en:
lend_request:
title: "Lend Request"
description: "%{user} wants to borrow your %{item}."
+ added_to_waitlist:
+ title: "Added to Waitlist"
+ description: "You have been added to Waitlist of %{item} at Position %{position}."
+ move_up_on_waitlist:
+ title: "Moved up on Waitlist"
+ description: "You have been moved up to Position %{position} on Waitlist for %{item}."
search:
title: "Search"
filter_button: "Filter"
@@ -73,6 +79,10 @@ en:
owner: "Owner"
lend_until: "Lend Until"
enter_waitlist: "Enter Waitlist"
+ leave_waitlist: "Leave Waitlist"
+ waitlist: "Waitlist"
+ users_waiting: "%{amount} Users are waiting for this Object."
+ waitlist_position: "You are at Position %{position} of the Waitlist."
lend: "Lend"
edit_item:
rental_duration_sec: "Rental Duration (seconds)"
@@ -84,6 +94,9 @@ en:
created: "Item was successfully created."
updated: "Item was successfully updated."
destroyed: "Item was successfully destroyed."
+ waitlist:
+ added_to_waitlist: "Added to Waitlist at Position %{position}."
+ failed_adding_to_waitlist: "Not added to Waitlist."
helpers:
submit:
update: "Update Item"
diff --git a/config/routes.rb b/config/routes.rb
index 13aadead..ec38e1b8 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -13,6 +13,7 @@
resources :users
post 'add_to_waitlist/:id', to: 'items#add_to_waitlist', as: 'add_to_waitlist'
+ post 'leave_waitlist/:id', to: 'items#leave_waitlist', as: 'leave_waitlist'
# Defines the root path route ("/")
root "landing_page#index"
end
diff --git a/db/migrate/20221212102651_create_added_to_waitlist_notifications.rb b/db/migrate/20221212102651_create_added_to_waitlist_notifications.rb
new file mode 100644
index 00000000..6457f2d2
--- /dev/null
+++ b/db/migrate/20221212102651_create_added_to_waitlist_notifications.rb
@@ -0,0 +1,9 @@
+class CreateAddedToWaitlistNotifications < ActiveRecord::Migration[7.0]
+ def change
+ create_table :added_to_waitlist_notifications do |t|
+ t.references :item, null: false, foreign_key: true
+
+ t.timestamps
+ end
+ end
+end
diff --git a/db/migrate/20221212120242_create_move_up_on_waitlist_notifications.rb b/db/migrate/20221212120242_create_move_up_on_waitlist_notifications.rb
new file mode 100644
index 00000000..0ccf49b4
--- /dev/null
+++ b/db/migrate/20221212120242_create_move_up_on_waitlist_notifications.rb
@@ -0,0 +1,9 @@
+class CreateMoveUpOnWaitlistNotifications < ActiveRecord::Migration[7.0]
+ def change
+ create_table :move_up_on_waitlist_notifications do |t|
+ t.references :item, null: false, foreign_key: true
+
+ t.timestamps
+ end
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index f025b797..ffcbe78b 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: 2022_12_10_213248) do
+ActiveRecord::Schema[7.0].define(version: 2022_12_12_120242) do
create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
@@ -39,6 +39,13 @@
t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true
end
+ create_table "added_to_waitlist_notifications", force: :cascade do |t|
+ t.integer "item_id", null: false
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ t.index ["item_id"], name: "index_added_to_waitlist_notifications_on_item_id"
+ end
+
create_table "groups", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
@@ -82,6 +89,13 @@
t.index ["user_id"], name: "index_memberships_on_user_id"
end
+ create_table "move_up_on_waitlist_notifications", force: :cascade do |t|
+ t.integer "item_id", null: false
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ t.index ["item_id"], name: "index_move_up_on_waitlist_notifications_on_item_id"
+ end
+
create_table "notifications", force: :cascade do |t|
t.datetime "date"
t.integer "user_id", null: false
@@ -119,12 +133,14 @@
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
+ add_foreign_key "added_to_waitlist_notifications", "items"
add_foreign_key "items", "users", column: "holder"
add_foreign_key "items", "users", column: "owner"
add_foreign_key "lend_request_notifications", "items"
add_foreign_key "lend_request_notifications", "users", column: "borrower_id"
add_foreign_key "memberships", "groups"
add_foreign_key "memberships", "users"
+ add_foreign_key "move_up_on_waitlist_notifications", "items"
add_foreign_key "notifications", "users"
add_foreign_key "waitlists", "items"
end
diff --git a/spec/factories/added_to_waitlist_notifications.rb b/spec/factories/added_to_waitlist_notifications.rb
new file mode 100644
index 00000000..6d14908f
--- /dev/null
+++ b/spec/factories/added_to_waitlist_notifications.rb
@@ -0,0 +1,6 @@
+FactoryBot.define do
+ factory :added_to_waitlist_notification do
+ user { nil }
+ item { nil }
+ end
+end
diff --git a/spec/factories/move_up_on_waitlist_notifications.rb b/spec/factories/move_up_on_waitlist_notifications.rb
new file mode 100644
index 00000000..2627e284
--- /dev/null
+++ b/spec/factories/move_up_on_waitlist_notifications.rb
@@ -0,0 +1,5 @@
+FactoryBot.define do
+ factory :move_up_on_waitlist_notification do
+ item { nil }
+ end
+end
diff --git a/spec/models/added_to_waitlist_notification_spec.rb b/spec/models/added_to_waitlist_notification_spec.rb
new file mode 100644
index 00000000..5d6d941a
--- /dev/null
+++ b/spec/models/added_to_waitlist_notification_spec.rb
@@ -0,0 +1,5 @@
+require 'rails_helper'
+
+RSpec.describe AddedToWaitlistNotification, type: :model do
+ pending "add some examples to (or delete) #{__FILE__}"
+end
diff --git a/spec/models/move_up_on_waitlist_notification_spec.rb b/spec/models/move_up_on_waitlist_notification_spec.rb
new file mode 100644
index 00000000..7ec90afc
--- /dev/null
+++ b/spec/models/move_up_on_waitlist_notification_spec.rb
@@ -0,0 +1,5 @@
+require 'rails_helper'
+
+RSpec.describe MoveUpOnWaitlistNotification, type: :model do
+ pending "add some examples to (or delete) #{__FILE__}"
+end
From 788811985730afdcfc7397ea19926ec8fc1802eb Mon Sep 17 00:00:00 2001
From: Julian
Date: Mon, 12 Dec 2022 17:58:23 +0100
Subject: [PATCH 14/27] add tests for waitlist model
---
spec/factories/users.rb | 6 +++++
spec/factories/waitlists.rb | 6 ++++-
spec/models/waitlist_spec.rb | 51 +++++++++++++++++++++++++++++++++++-
3 files changed, 61 insertions(+), 2 deletions(-)
diff --git a/spec/factories/users.rb b/spec/factories/users.rb
index 16469685..9a75e4e6 100644
--- a/spec/factories/users.rb
+++ b/spec/factories/users.rb
@@ -10,4 +10,10 @@
password { 'password' }
password_confirmation { 'password' }
end
+
+ factory :peter, class: 'User' do
+ email { "peter.lustig@hpi.de" }
+ password { 'password' }
+ password_confirmation { 'password' }
+ end
end
diff --git a/spec/factories/waitlists.rb b/spec/factories/waitlists.rb
index 1335f320..c8227a84 100644
--- a/spec/factories/waitlists.rb
+++ b/spec/factories/waitlists.rb
@@ -1,5 +1,9 @@
FactoryBot.define do
factory :waitlist do
- item { nil }
+ users { [ build(:user), build(:max) ] }
+ end
+ factory :waitlist_with_item, class: 'Waitlist' do
+ users { [ build(:user), build(:max) ] }
+ item { build(:item) }
end
end
diff --git a/spec/models/waitlist_spec.rb b/spec/models/waitlist_spec.rb
index 1b8f43be..074e2c28 100644
--- a/spec/models/waitlist_spec.rb
+++ b/spec/models/waitlist_spec.rb
@@ -1,5 +1,54 @@
require 'rails_helper'
RSpec.describe Waitlist, type: :model do
- pending "add some examples to (or delete) #{__FILE__}"
+ it "is valid with all required attributes" do
+ waitlist = create(:waitlist_with_item)
+ expect(waitlist).to be_valid
+ end
+
+ it "is not valid without an item" do
+ waitlist = described_class.new
+ expect(waitlist).not_to be_valid
+ end
+
+ it "returns the correct position" do
+ waitlist = create(:waitlist_with_item)
+ expect(waitlist.position(waitlist.users[0])).to eq(0)
+ expect(waitlist.position(waitlist.users[1])).to eq(1)
+ end
+
+ it "returns correct first user" do
+ waitlist = create(:waitlist_with_item)
+ expect(waitlist.first_user).to eq(waitlist.users[0])
+ end
+
+ it "adds a user to the waitlist" do
+ waitlist = create(:waitlist_with_item)
+ user = create(:peter)
+ waitlist.add_user(user)
+ expect(waitlist.users).to include(user)
+ end
+
+ it "removes a user from the waitlist" do
+ waitlist = create(:waitlist_with_item)
+ user = waitlist.users[0]
+ waitlist.remove_user(user)
+ expect(waitlist.users).not_to include(user)
+ end
+
+ it "creates a notification when a user is added to the waitlist" do
+ waitlist = create(:waitlist_with_item)
+ user = create(:peter)
+ waitlist.add_user(user)
+ notification = AddedToWaitlistNotification.find_by(user: user, item: waitlist.item)
+ expect(notification).to_not be_nil
+ end
+
+ it "creates a moved up notification for users after when a user is removed from the waitlist" do
+ waitlist = create(:waitlist_with_item)
+ user = waitlist.users[0]
+ waitlist.remove_user(user)
+ notification = MoveUpOnWaitlistNotification.find_by(user: waitlist.users[0], item: waitlist.item)
+ expect(notification).to_not be_nil
+ end
end
From f48475422c85dab7faca5f68ed7562f7e1c211cf Mon Sep 17 00:00:00 2001
From: Julian
Date: Mon, 12 Dec 2022 21:30:30 +0100
Subject: [PATCH 15/27] failing tests
---
spec/features/items/show.html.erb_spec.rb | 63 ++++++++++++++++++++++-
1 file changed, 61 insertions(+), 2 deletions(-)
diff --git a/spec/features/items/show.html.erb_spec.rb b/spec/features/items/show.html.erb_spec.rb
index f6cfced8..806b6830 100644
--- a/spec/features/items/show.html.erb_spec.rb
+++ b/spec/features/items/show.html.erb_spec.rb
@@ -3,10 +3,16 @@
RSpec.describe "items/show", type: :feature do
let(:user) { create(:user) }
let(:user2) { create(:user) }
- let(:item) { create(:item, owner: user.id) }
+ let(:item) do
+ item = create(:item, owner: user.id)
+ item.waitlist = create(:waitlist_with_item)
+ item.waitlist.item = item
+ item
+ end
it "renders without rental start and duration set" do
- an_item = create(:item_without_time)
+ sign_in user
+ an_item = create(:item_without_time, waitlist: create(:waitlist_with_item))
visit item_url(an_item)
expect(page).to have_text(Time.zone.now.advance(days: 1).strftime('%d.%m.%Y'))
end
@@ -24,10 +30,63 @@
end
it "renders attributes" do
+ sign_in user
visit item_path(item)
expect(page).to have_text(item.name)
expect(page).to have_text(item.category)
expect(page).to have_text(item.location)
expect(page).to have_text(item.description)
end
+
+ # tests:
+ # - has add to waitlist button when not on list
+ # - has remove from waitlist button when on list
+ # - buttons perform actions correctly
+ # - creates added / move up notifications
+
+ it "has enter waitlist button when not on list" do
+ sign_in user
+ visit item_path(item)
+ expect(page).to have_text("Enter Waitlist")
+ end
+
+ it "has leave waitlist button when on list" do
+ sign_in user
+ item.waitlist.add_user(user)
+ visit item_path(item)
+ expect(page).to have_text("Leave Waitlist")
+ end
+
+ it "adds user to waitlist when clicking add to waitlist button" do
+ sign_in user
+ visit item_path(item)
+ find(:button, "Enter Waitlist").click
+ expect(item.waitlist.users).to include(user)
+ end
+
+ it "removes user from waitlist when clicking remove from waitlist button" do
+ sign_in user
+ item.waitlist.add_user(user)
+ visit item_path(item)
+ find(:button, "Leave Waitlist").click
+ expect(item.waitlist.users).to_not include(user)
+ end
+
+ it "creates added to waitlist notification when adding user to waitlist" do
+ sign_in user
+ visit item_path(item)
+ find(:button, "Enter Waitlist").click
+ notification = AddedToWaitlistNotification.find_by(user: user, item: item)
+ expect(notification).to_not be_nil
+ end
+
+ it "creates move up on waitlist notification when removing user from waitlist" do
+ sign_in user
+ item.waitlist.add_user(user)
+ visit item_path(item)
+ find(:button, "Leave Waitlist").click
+ notification = MoveUpOnWaitlistNotification.find_by(user: item.waitlist.users[0], item: item)
+ expect(notification).to_not be_nil
+ end
+
end
From 3b36248831103dbd2281d5ae02030224b7f4b81d Mon Sep 17 00:00:00 2001
From: Julian
Date: Mon, 12 Dec 2022 22:31:18 +0100
Subject: [PATCH 16/27] tests and stuff
---
app/controllers/items_controller.rb | 1 +
spec/features/items/show.html.erb_spec.rb | 28 +++++++++++------------
2 files changed, 15 insertions(+), 14 deletions(-)
diff --git a/app/controllers/items_controller.rb b/app/controllers/items_controller.rb
index 3f152268..20311522 100644
--- a/app/controllers/items_controller.rb
+++ b/app/controllers/items_controller.rb
@@ -62,6 +62,7 @@ def add_to_waitlist
@item = Item.find(params[:id])
@user = current_user
@item.add_to_waitlist(@user)
+
respond_to do |format|
if @item.save
diff --git a/spec/features/items/show.html.erb_spec.rb b/spec/features/items/show.html.erb_spec.rb
index 806b6830..c860ba3e 100644
--- a/spec/features/items/show.html.erb_spec.rb
+++ b/spec/features/items/show.html.erb_spec.rb
@@ -1,5 +1,5 @@
require 'rails_helper'
-
+require 'pp'
RSpec.describe "items/show", type: :feature do
let(:user) { create(:user) }
let(:user2) { create(:user) }
@@ -45,47 +45,47 @@
# - creates added / move up notifications
it "has enter waitlist button when not on list" do
- sign_in user
+ sign_in user2
visit item_path(item)
expect(page).to have_text("Enter Waitlist")
end
it "has leave waitlist button when on list" do
- sign_in user
- item.waitlist.add_user(user)
+ sign_in user2
+ item.waitlist.add_user(user2)
visit item_path(item)
expect(page).to have_text("Leave Waitlist")
end
it "adds user to waitlist when clicking add to waitlist button" do
- sign_in user
+ sign_in user2
visit item_path(item)
find(:button, "Enter Waitlist").click
- expect(item.waitlist.users).to include(user)
+ expect(Item.find(item.id).waitlist.users).to include(user2)
end
it "removes user from waitlist when clicking remove from waitlist button" do
- sign_in user
- item.waitlist.add_user(user)
+ sign_in user2
+ item.waitlist.add_user(user2)
visit item_path(item)
find(:button, "Leave Waitlist").click
- expect(item.waitlist.users).to_not include(user)
+ expect(Item.find(item.id).waitlist.users).to_not include(user2)
end
it "creates added to waitlist notification when adding user to waitlist" do
- sign_in user
+ sign_in user2
visit item_path(item)
find(:button, "Enter Waitlist").click
- notification = AddedToWaitlistNotification.find_by(user: user, item: item)
+ notification = AddedToWaitlistNotification.find_by(user: user2, item: item)
expect(notification).to_not be_nil
end
it "creates move up on waitlist notification when removing user from waitlist" do
- sign_in user
- item.waitlist.add_user(user)
+ sign_in item.waitlist.users[0]
+ item.waitlist.add_user(user2)
visit item_path(item)
find(:button, "Leave Waitlist").click
- notification = MoveUpOnWaitlistNotification.find_by(user: item.waitlist.users[0], item: item)
+ notification = MoveUpOnWaitlistNotification.find_by(user: Item.find(item.id).waitlist.users[0], item: item)
expect(notification).to_not be_nil
end
From 3a14d782e2659a2a7bed31ceaee4403d4d48f754 Mon Sep 17 00:00:00 2001
From: LinoH5
Date: Mon, 12 Dec 2022 22:54:58 +0100
Subject: [PATCH 17/27] renames users in tests to clarify their role
---
app/controllers/items_controller.rb | 1 -
spec/features/items/show.html.erb_spec.rb | 31 +++++++++++------------
2 files changed, 15 insertions(+), 17 deletions(-)
diff --git a/app/controllers/items_controller.rb b/app/controllers/items_controller.rb
index 20311522..3f152268 100644
--- a/app/controllers/items_controller.rb
+++ b/app/controllers/items_controller.rb
@@ -62,7 +62,6 @@ def add_to_waitlist
@item = Item.find(params[:id])
@user = current_user
@item.add_to_waitlist(@user)
-
respond_to do |format|
if @item.save
diff --git a/spec/features/items/show.html.erb_spec.rb b/spec/features/items/show.html.erb_spec.rb
index c860ba3e..6480ff3a 100644
--- a/spec/features/items/show.html.erb_spec.rb
+++ b/spec/features/items/show.html.erb_spec.rb
@@ -1,10 +1,10 @@
require 'rails_helper'
-require 'pp'
+
RSpec.describe "items/show", type: :feature do
+ let(:owner) { create(:user) }
let(:user) { create(:user) }
- let(:user2) { create(:user) }
let(:item) do
- item = create(:item, owner: user.id)
+ item = create(:item, owner: owner.id)
item.waitlist = create(:waitlist_with_item)
item.waitlist.item = item
item
@@ -18,13 +18,13 @@
end
it "shows edit button for owner" do
- sign_in user
+ sign_in owner
visit item_path(item)
expect(page).to have_link(href: edit_item_url(item))
end
it "does not show edit button for non-owner" do
- sign_in user2
+ sign_in user
visit item_path(item)
expect(page).not_to have_link(href: edit_item_url(item))
end
@@ -45,44 +45,43 @@
# - creates added / move up notifications
it "has enter waitlist button when not on list" do
- sign_in user2
+ sign_in user
visit item_path(item)
expect(page).to have_text("Enter Waitlist")
end
it "has leave waitlist button when on list" do
- sign_in user2
- item.waitlist.add_user(user2)
+ sign_in user
+ item.waitlist.add_user(user)
visit item_path(item)
expect(page).to have_text("Leave Waitlist")
end
it "adds user to waitlist when clicking add to waitlist button" do
- sign_in user2
+ sign_in user
visit item_path(item)
find(:button, "Enter Waitlist").click
- expect(Item.find(item.id).waitlist.users).to include(user2)
+ expect(Item.find(item.id).waitlist.users).to include(user)
end
it "removes user from waitlist when clicking remove from waitlist button" do
- sign_in user2
- item.waitlist.add_user(user2)
+ sign_in user
+ item.waitlist.add_user(user)
visit item_path(item)
find(:button, "Leave Waitlist").click
- expect(Item.find(item.id).waitlist.users).to_not include(user2)
+ expect(Item.find(item.id).waitlist.users).to_not include(user)
end
it "creates added to waitlist notification when adding user to waitlist" do
- sign_in user2
+ sign_in user
visit item_path(item)
find(:button, "Enter Waitlist").click
- notification = AddedToWaitlistNotification.find_by(user: user2, item: item)
+ notification = AddedToWaitlistNotification.find_by(user: user, item: item)
expect(notification).to_not be_nil
end
it "creates move up on waitlist notification when removing user from waitlist" do
sign_in item.waitlist.users[0]
- item.waitlist.add_user(user2)
visit item_path(item)
find(:button, "Leave Waitlist").click
notification = MoveUpOnWaitlistNotification.find_by(user: Item.find(item.id).waitlist.users[0], item: item)
From b1f5a545cb2312f3517ad711c3d57480ac3baf38 Mon Sep 17 00:00:00 2001
From: LinoH5
Date: Mon, 12 Dec 2022 23:28:25 +0100
Subject: [PATCH 18/27] adds success status update for waitlist.add_user
---
app/controllers/items_controller.rb | 3 +--
app/models/waitlist.rb | 4 +++-
2 files changed, 4 insertions(+), 3 deletions(-)
diff --git a/app/controllers/items_controller.rb b/app/controllers/items_controller.rb
index 3f152268..2b46006b 100644
--- a/app/controllers/items_controller.rb
+++ b/app/controllers/items_controller.rb
@@ -61,10 +61,9 @@ def destroy
def add_to_waitlist
@item = Item.find(params[:id])
@user = current_user
- @item.add_to_waitlist(@user)
respond_to do |format|
- if @item.save
+ if @item.add_to_waitlist(@user) && @item.save
format.html { redirect_to item_url(@item), notice: t("models.waitlist.added_to_waitlist", position: @item.waitlist.position(@user) + 1) }
#format.json { render :show, status: :ok, location: @item }
else
diff --git a/app/models/waitlist.rb b/app/models/waitlist.rb
index 4459ed03..fda921c3 100644
--- a/app/models/waitlist.rb
+++ b/app/models/waitlist.rb
@@ -11,10 +11,12 @@ def first_user
end
def add_user (user)
- if (!self.users.exists?(user.id) && user.id != self.item.owner)
+ unless self.users.exists?(user.id) || user.id == self.item.owner
self.users << user
self.add_added_to_waitlist_notification(user)
+ return true
end
+ return false
end
def remove_user (user)
From eaeede249c7bfe4dfd6989f01034d102db97a234 Mon Sep 17 00:00:00 2001
From: Alexander Ungefug <82446024+AlexanderUngefug@users.noreply.github.com>
Date: Tue, 13 Dec 2022 10:52:42 +0100
Subject: [PATCH 19/27] rubocop automatic corrections
---
app/controllers/items_controller.rb | 11 ++--
app/helpers/application_helper.rb | 8 +--
app/models/added_to_waitlist_notification.rb | 3 +-
app/models/item.rb | 8 +--
.../move_up_on_waitlist_notification.rb | 3 +-
app/models/waitlist.rb | 64 +++++++++----------
...21202144014_add_actable_to_notification.rb | 4 +-
spec/features/items/show.html.erb_spec.rb | 8 +--
spec/features/search/search_spec.rb | 14 ++--
spec/models/waitlist_spec.rb | 4 +-
10 files changed, 64 insertions(+), 63 deletions(-)
diff --git a/app/controllers/items_controller.rb b/app/controllers/items_controller.rb
index 2b46006b..ffaaff72 100644
--- a/app/controllers/items_controller.rb
+++ b/app/controllers/items_controller.rb
@@ -61,14 +61,17 @@ def destroy
def add_to_waitlist
@item = Item.find(params[:id])
@user = current_user
-
+
respond_to do |format|
if @item.add_to_waitlist(@user) && @item.save
- format.html { redirect_to item_url(@item), notice: t("models.waitlist.added_to_waitlist", position: @item.waitlist.position(@user) + 1) }
- #format.json { render :show, status: :ok, location: @item }
+ format.html do
+ redirect_to item_url(@item),
+ notice: t("models.waitlist.added_to_waitlist", position: @item.waitlist.position(@user) + 1)
+ end
+ # format.json { render :show, status: :ok, location: @item }
else
format.html { redirect_to item_url(@item), alert: t("models.waitlist.failed_adding_to_waitlist") }
- #format.json { render :show, status: :unprocessable_entity, location: @item }
+ # format.json { render :show, status: :unprocessable_entity, location: @item }
end
end
end
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index bcaf0e55..700e9393 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -1,9 +1,7 @@
module ApplicationHelper
def render_if_exists(path_to_partial, *args, &block)
- begin
- render path_to_partial, *args, &block
- rescue ActionView::MissingTemplate
- # do nothing
- end
+ render path_to_partial, *args, &block
+ rescue ActionView::MissingTemplate
+ # do nothing
end
end
diff --git a/app/models/added_to_waitlist_notification.rb b/app/models/added_to_waitlist_notification.rb
index 39d79b33..0451358a 100644
--- a/app/models/added_to_waitlist_notification.rb
+++ b/app/models/added_to_waitlist_notification.rb
@@ -8,6 +8,7 @@ def title
end
def description
- I18n.t("views.notifications.added_to_waitlist.description", position: item.waitlist.position(user) + 1, item: item.name)
+ I18n.t("views.notifications.added_to_waitlist.description", position: item.waitlist.position(user) + 1,
+ item: item.name)
end
end
diff --git a/app/models/item.rb b/app/models/item.rb
index 17968a73..293e4feb 100644
--- a/app/models/item.rb
+++ b/app/models/item.rb
@@ -24,11 +24,11 @@ def price_in_euro=(euros)
self.price_ct = euros * 100
end
- def add_to_waitlist (user)
- self.waitlist.add_user(user)
+ def add_to_waitlist(user)
+ waitlist.add_user(user)
end
- def remove_from_waitlist (user)
- self.waitlist.remove_user(user)
+ def remove_from_waitlist(user)
+ waitlist.remove_user(user)
end
end
diff --git a/app/models/move_up_on_waitlist_notification.rb b/app/models/move_up_on_waitlist_notification.rb
index ffb7649e..a7c750e6 100644
--- a/app/models/move_up_on_waitlist_notification.rb
+++ b/app/models/move_up_on_waitlist_notification.rb
@@ -8,6 +8,7 @@ def title
end
def description
- I18n.t("views.notifications.move_up_on_waitlist.description", position: item.waitlist.position(user) + 1, item: item.name)
+ I18n.t("views.notifications.move_up_on_waitlist.description", position: item.waitlist.position(user) + 1,
+ item: item.name)
end
end
diff --git a/app/models/waitlist.rb b/app/models/waitlist.rb
index fda921c3..18f15e53 100644
--- a/app/models/waitlist.rb
+++ b/app/models/waitlist.rb
@@ -2,65 +2,65 @@ class Waitlist < ApplicationRecord
belongs_to :item
has_and_belongs_to_many :users
- def position (user)
- self.users.find_index(user)
+ def position(user)
+ users.find_index(user)
end
def first_user
- self.users.first
+ users.first
end
- def add_user (user)
- unless self.users.exists?(user.id) || user.id == self.item.owner
- self.users << user
- self.add_added_to_waitlist_notification(user)
+ def add_user(user)
+ unless users.exists?(user.id) || user.id == item.owner
+ users << user
+ add_added_to_waitlist_notification(user)
return true
end
- return false
+ false
end
- def remove_user (user)
- user_index = self.users.find_index(user)
- self.users.delete(user)
+ def remove_user(user)
+ user_index = users.find_index(user)
+ users.delete(user)
- self.delete_waitlist_notifications(user)
+ delete_waitlist_notifications(user)
- unless user_index.nil? || user_index >= self.users.size
- self.users[user_index..-1].each do |temp_user|
- self.delete_waitlist_notifications(temp_user)
- self.add_move_up_on_waitlist_notification(temp_user)
- end
+ return if user_index.nil? || user_index >= users.size
+
+ users[user_index..].each do |temp_user|
+ delete_waitlist_notifications(temp_user)
+ add_move_up_on_waitlist_notification(temp_user)
end
end
private
- def delete_waitlist_notifications (user)
- self.delete_added_to_waitlist_notification(user)
- self.delete_moved_up_on_waitlist_notification(user)
+ def delete_waitlist_notifications(user)
+ delete_added_to_waitlist_notification(user)
+ delete_moved_up_on_waitlist_notification(user)
end
- def add_added_to_waitlist_notification (user)
- @notification = AddedToWaitlistNotification.new(user: user, date: Time.now, item: item)
+ def add_added_to_waitlist_notification(user)
+ @notification = AddedToWaitlistNotification.new(user: user, date: Time.zone.now, item: item)
@notification.save
end
- def add_move_up_on_waitlist_notification (user)
- @notification = MoveUpOnWaitlistNotification.new(user: user, date: Time.now, item: item)
+ def add_move_up_on_waitlist_notification(user)
+ @notification = MoveUpOnWaitlistNotification.new(user: user, date: Time.zone.now, item: item)
@notification.save
end
- def delete_added_to_waitlist_notification (user)
+ def delete_added_to_waitlist_notification(user)
@notification = AddedToWaitlistNotification.find_by(item: item, user: user)
- unless @notification.nil?
- @notification.destroy
- end
+ return if @notification.nil?
+
+ @notification.destroy
end
- def delete_moved_up_on_waitlist_notification (user)
+ def delete_moved_up_on_waitlist_notification(user)
@notification = MoveUpOnWaitlistNotification.find_by(item: item, user: user)
- unless @notification.nil?
- @notification.destroy
- end
+ return if @notification.nil?
+
+ @notification.destroy
end
end
diff --git a/db/migrate/20221202144014_add_actable_to_notification.rb b/db/migrate/20221202144014_add_actable_to_notification.rb
index c39595c4..0b92d658 100644
--- a/db/migrate/20221202144014_add_actable_to_notification.rb
+++ b/db/migrate/20221202144014_add_actable_to_notification.rb
@@ -1,7 +1,5 @@
class AddActableToNotification < ActiveRecord::Migration[7.0]
def change
- change_table :notifications do |t|
- t.actable
- end
+ change_table :notifications, &:actable
end
end
diff --git a/spec/features/items/show.html.erb_spec.rb b/spec/features/items/show.html.erb_spec.rb
index 6480ff3a..e856ce26 100644
--- a/spec/features/items/show.html.erb_spec.rb
+++ b/spec/features/items/show.html.erb_spec.rb
@@ -4,7 +4,7 @@
let(:owner) { create(:user) }
let(:user) { create(:user) }
let(:item) do
- item = create(:item, owner: owner.id)
+ item = create(:item, owner: owner.id)
item.waitlist = create(:waitlist_with_item)
item.waitlist.item = item
item
@@ -69,7 +69,7 @@
item.waitlist.add_user(user)
visit item_path(item)
find(:button, "Leave Waitlist").click
- expect(Item.find(item.id).waitlist.users).to_not include(user)
+ expect(Item.find(item.id).waitlist.users).not_to include(user)
end
it "creates added to waitlist notification when adding user to waitlist" do
@@ -77,7 +77,7 @@
visit item_path(item)
find(:button, "Enter Waitlist").click
notification = AddedToWaitlistNotification.find_by(user: user, item: item)
- expect(notification).to_not be_nil
+ expect(notification).not_to be_nil
end
it "creates move up on waitlist notification when removing user from waitlist" do
@@ -85,7 +85,7 @@
visit item_path(item)
find(:button, "Leave Waitlist").click
notification = MoveUpOnWaitlistNotification.find_by(user: Item.find(item.id).waitlist.users[0], item: item)
- expect(notification).to_not be_nil
+ expect(notification).not_to be_nil
end
end
diff --git a/spec/features/search/search_spec.rb b/spec/features/search/search_spec.rb
index e918dda3..88007b45 100644
--- a/spec/features/search/search_spec.rb
+++ b/spec/features/search/search_spec.rb
@@ -1,6 +1,13 @@
require "rails_helper"
describe "Search page", type: :feature do
+ before do
+ @item_book = create(:item_book)
+ @item_beamer = create(:item_beamer)
+ @item_whiteboard = create(:item_whiteboard)
+ visit search_path
+ end
+
it "translates the close button to German" do
page.driver.header 'Accept-language', 'de-DE'
visit search_path
@@ -12,13 +19,6 @@
expect(page).to have_text("Close")
end
- before do
- @item_book = create(:item_book)
- @item_beamer = create(:item_beamer)
- @item_whiteboard = create(:item_whiteboard)
- visit search_path
- end
-
it "partial matching works for title" do
page.fill_in "search", with: "Ruby"
click_button("submit")
diff --git a/spec/models/waitlist_spec.rb b/spec/models/waitlist_spec.rb
index 074e2c28..eaa404a8 100644
--- a/spec/models/waitlist_spec.rb
+++ b/spec/models/waitlist_spec.rb
@@ -41,7 +41,7 @@
user = create(:peter)
waitlist.add_user(user)
notification = AddedToWaitlistNotification.find_by(user: user, item: waitlist.item)
- expect(notification).to_not be_nil
+ expect(notification).not_to be_nil
end
it "creates a moved up notification for users after when a user is removed from the waitlist" do
@@ -49,6 +49,6 @@
user = waitlist.users[0]
waitlist.remove_user(user)
notification = MoveUpOnWaitlistNotification.find_by(user: waitlist.users[0], item: waitlist.item)
- expect(notification).to_not be_nil
+ expect(notification).not_to be_nil
end
end
From eac2d901244864a9b463baff4d3c453458be17e6 Mon Sep 17 00:00:00 2001
From: LinoH5
Date: Tue, 13 Dec 2022 11:31:50 +0100
Subject: [PATCH 20/27] removes waitlist button for owner, more tests, rubocop
corrections
---
app/controllers/items_controller.rb | 9 +--
app/helpers/application_helper.rb | 8 +--
app/models/added_to_waitlist_notification.rb | 3 +-
app/models/item.rb | 8 +--
.../move_up_on_waitlist_notification.rb | 3 +-
app/models/waitlist.rb | 64 +++++++++----------
app/views/items/show.html.erb | 10 +--
...21202144014_add_actable_to_notification.rb | 4 +-
spec/features/items/show.html.erb_spec.rb | 21 +++---
spec/features/search/search_spec.rb | 14 ++--
spec/models/waitlist_spec.rb | 13 +++-
11 files changed, 83 insertions(+), 74 deletions(-)
diff --git a/app/controllers/items_controller.rb b/app/controllers/items_controller.rb
index 2b46006b..00b66220 100644
--- a/app/controllers/items_controller.rb
+++ b/app/controllers/items_controller.rb
@@ -61,14 +61,15 @@ def destroy
def add_to_waitlist
@item = Item.find(params[:id])
@user = current_user
-
+
respond_to do |format|
if @item.add_to_waitlist(@user) && @item.save
- format.html { redirect_to item_url(@item), notice: t("models.waitlist.added_to_waitlist", position: @item.waitlist.position(@user) + 1) }
- #format.json { render :show, status: :ok, location: @item }
+ format.html do
+ redirect_to item_url(@item),
+ notice: t("models.waitlist.added_to_waitlist", position: @item.waitlist.position(@user) + 1)
+ end
else
format.html { redirect_to item_url(@item), alert: t("models.waitlist.failed_adding_to_waitlist") }
- #format.json { render :show, status: :unprocessable_entity, location: @item }
end
end
end
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index bcaf0e55..700e9393 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -1,9 +1,7 @@
module ApplicationHelper
def render_if_exists(path_to_partial, *args, &block)
- begin
- render path_to_partial, *args, &block
- rescue ActionView::MissingTemplate
- # do nothing
- end
+ render path_to_partial, *args, &block
+ rescue ActionView::MissingTemplate
+ # do nothing
end
end
diff --git a/app/models/added_to_waitlist_notification.rb b/app/models/added_to_waitlist_notification.rb
index 39d79b33..0451358a 100644
--- a/app/models/added_to_waitlist_notification.rb
+++ b/app/models/added_to_waitlist_notification.rb
@@ -8,6 +8,7 @@ def title
end
def description
- I18n.t("views.notifications.added_to_waitlist.description", position: item.waitlist.position(user) + 1, item: item.name)
+ I18n.t("views.notifications.added_to_waitlist.description", position: item.waitlist.position(user) + 1,
+ item: item.name)
end
end
diff --git a/app/models/item.rb b/app/models/item.rb
index 17968a73..293e4feb 100644
--- a/app/models/item.rb
+++ b/app/models/item.rb
@@ -24,11 +24,11 @@ def price_in_euro=(euros)
self.price_ct = euros * 100
end
- def add_to_waitlist (user)
- self.waitlist.add_user(user)
+ def add_to_waitlist(user)
+ waitlist.add_user(user)
end
- def remove_from_waitlist (user)
- self.waitlist.remove_user(user)
+ def remove_from_waitlist(user)
+ waitlist.remove_user(user)
end
end
diff --git a/app/models/move_up_on_waitlist_notification.rb b/app/models/move_up_on_waitlist_notification.rb
index ffb7649e..a7c750e6 100644
--- a/app/models/move_up_on_waitlist_notification.rb
+++ b/app/models/move_up_on_waitlist_notification.rb
@@ -8,6 +8,7 @@ def title
end
def description
- I18n.t("views.notifications.move_up_on_waitlist.description", position: item.waitlist.position(user) + 1, item: item.name)
+ I18n.t("views.notifications.move_up_on_waitlist.description", position: item.waitlist.position(user) + 1,
+ item: item.name)
end
end
diff --git a/app/models/waitlist.rb b/app/models/waitlist.rb
index fda921c3..18f15e53 100644
--- a/app/models/waitlist.rb
+++ b/app/models/waitlist.rb
@@ -2,65 +2,65 @@ class Waitlist < ApplicationRecord
belongs_to :item
has_and_belongs_to_many :users
- def position (user)
- self.users.find_index(user)
+ def position(user)
+ users.find_index(user)
end
def first_user
- self.users.first
+ users.first
end
- def add_user (user)
- unless self.users.exists?(user.id) || user.id == self.item.owner
- self.users << user
- self.add_added_to_waitlist_notification(user)
+ def add_user(user)
+ unless users.exists?(user.id) || user.id == item.owner
+ users << user
+ add_added_to_waitlist_notification(user)
return true
end
- return false
+ false
end
- def remove_user (user)
- user_index = self.users.find_index(user)
- self.users.delete(user)
+ def remove_user(user)
+ user_index = users.find_index(user)
+ users.delete(user)
- self.delete_waitlist_notifications(user)
+ delete_waitlist_notifications(user)
- unless user_index.nil? || user_index >= self.users.size
- self.users[user_index..-1].each do |temp_user|
- self.delete_waitlist_notifications(temp_user)
- self.add_move_up_on_waitlist_notification(temp_user)
- end
+ return if user_index.nil? || user_index >= users.size
+
+ users[user_index..].each do |temp_user|
+ delete_waitlist_notifications(temp_user)
+ add_move_up_on_waitlist_notification(temp_user)
end
end
private
- def delete_waitlist_notifications (user)
- self.delete_added_to_waitlist_notification(user)
- self.delete_moved_up_on_waitlist_notification(user)
+ def delete_waitlist_notifications(user)
+ delete_added_to_waitlist_notification(user)
+ delete_moved_up_on_waitlist_notification(user)
end
- def add_added_to_waitlist_notification (user)
- @notification = AddedToWaitlistNotification.new(user: user, date: Time.now, item: item)
+ def add_added_to_waitlist_notification(user)
+ @notification = AddedToWaitlistNotification.new(user: user, date: Time.zone.now, item: item)
@notification.save
end
- def add_move_up_on_waitlist_notification (user)
- @notification = MoveUpOnWaitlistNotification.new(user: user, date: Time.now, item: item)
+ def add_move_up_on_waitlist_notification(user)
+ @notification = MoveUpOnWaitlistNotification.new(user: user, date: Time.zone.now, item: item)
@notification.save
end
- def delete_added_to_waitlist_notification (user)
+ def delete_added_to_waitlist_notification(user)
@notification = AddedToWaitlistNotification.find_by(item: item, user: user)
- unless @notification.nil?
- @notification.destroy
- end
+ return if @notification.nil?
+
+ @notification.destroy
end
- def delete_moved_up_on_waitlist_notification (user)
+ def delete_moved_up_on_waitlist_notification(user)
@notification = MoveUpOnWaitlistNotification.find_by(item: item, user: user)
- unless @notification.nil?
- @notification.destroy
- end
+ return if @notification.nil?
+
+ @notification.destroy
end
end
diff --git a/app/views/items/show.html.erb b/app/views/items/show.html.erb
index 2fcc47ec..a2d4bbb8 100644
--- a/app/views/items/show.html.erb
+++ b/app/views/items/show.html.erb
@@ -157,10 +157,12 @@
<% end %>
- <% if !@item.waitlist.users.exists?(current_user.id) %>
- <%= button_to t('views.show_item.enter_waitlist'), { action: "add_to_waitlist" }, class: "primaryButton" %>
- <% else %>
- <%= button_to t('views.show_item.leave_waitlist'), { action: "leave_waitlist" }, class: "primaryButton" %>
+ <% if current_user.id != @item.owner %>
+ <% if !@item.waitlist.users.exists?(current_user.id) %>
+ <%= button_to t('views.show_item.enter_waitlist'), { action: "add_to_waitlist" }, class: "primaryButton" %>
+ <% else %>
+ <%= button_to t('views.show_item.leave_waitlist'), { action: "leave_waitlist" }, class: "primaryButton" %>
+ <% end %>
<% end %>
diff --git a/db/migrate/20221202144014_add_actable_to_notification.rb b/db/migrate/20221202144014_add_actable_to_notification.rb
index c39595c4..0b92d658 100644
--- a/db/migrate/20221202144014_add_actable_to_notification.rb
+++ b/db/migrate/20221202144014_add_actable_to_notification.rb
@@ -1,7 +1,5 @@
class AddActableToNotification < ActiveRecord::Migration[7.0]
def change
- change_table :notifications do |t|
- t.actable
- end
+ change_table :notifications, &:actable
end
end
diff --git a/spec/features/items/show.html.erb_spec.rb b/spec/features/items/show.html.erb_spec.rb
index 6480ff3a..bf2afd0a 100644
--- a/spec/features/items/show.html.erb_spec.rb
+++ b/spec/features/items/show.html.erb_spec.rb
@@ -4,7 +4,7 @@
let(:owner) { create(:user) }
let(:user) { create(:user) }
let(:item) do
- item = create(:item, owner: owner.id)
+ item = create(:item, owner: owner.id)
item.waitlist = create(:waitlist_with_item)
item.waitlist.item = item
item
@@ -38,18 +38,19 @@
expect(page).to have_text(item.description)
end
- # tests:
- # - has add to waitlist button when not on list
- # - has remove from waitlist button when on list
- # - buttons perform actions correctly
- # - creates added / move up notifications
-
it "has enter waitlist button when not on list" do
sign_in user
visit item_path(item)
expect(page).to have_text("Enter Waitlist")
end
+ it "does not have enter/leave waitlist button when owner of item" do
+ sign_in owner
+ visit item_path(item)
+ expect(page).not_to have_text("Enter Waitlist")
+ expect(page).not_to have_text("Leave Waitlist")
+ end
+
it "has leave waitlist button when on list" do
sign_in user
item.waitlist.add_user(user)
@@ -69,7 +70,7 @@
item.waitlist.add_user(user)
visit item_path(item)
find(:button, "Leave Waitlist").click
- expect(Item.find(item.id).waitlist.users).to_not include(user)
+ expect(Item.find(item.id).waitlist.users).not_to include(user)
end
it "creates added to waitlist notification when adding user to waitlist" do
@@ -77,7 +78,7 @@
visit item_path(item)
find(:button, "Enter Waitlist").click
notification = AddedToWaitlistNotification.find_by(user: user, item: item)
- expect(notification).to_not be_nil
+ expect(notification).not_to be_nil
end
it "creates move up on waitlist notification when removing user from waitlist" do
@@ -85,7 +86,7 @@
visit item_path(item)
find(:button, "Leave Waitlist").click
notification = MoveUpOnWaitlistNotification.find_by(user: Item.find(item.id).waitlist.users[0], item: item)
- expect(notification).to_not be_nil
+ expect(notification).not_to be_nil
end
end
diff --git a/spec/features/search/search_spec.rb b/spec/features/search/search_spec.rb
index e918dda3..88007b45 100644
--- a/spec/features/search/search_spec.rb
+++ b/spec/features/search/search_spec.rb
@@ -1,6 +1,13 @@
require "rails_helper"
describe "Search page", type: :feature do
+ before do
+ @item_book = create(:item_book)
+ @item_beamer = create(:item_beamer)
+ @item_whiteboard = create(:item_whiteboard)
+ visit search_path
+ end
+
it "translates the close button to German" do
page.driver.header 'Accept-language', 'de-DE'
visit search_path
@@ -12,13 +19,6 @@
expect(page).to have_text("Close")
end
- before do
- @item_book = create(:item_book)
- @item_beamer = create(:item_beamer)
- @item_whiteboard = create(:item_whiteboard)
- visit search_path
- end
-
it "partial matching works for title" do
page.fill_in "search", with: "Ruby"
click_button("submit")
diff --git a/spec/models/waitlist_spec.rb b/spec/models/waitlist_spec.rb
index 074e2c28..391787f5 100644
--- a/spec/models/waitlist_spec.rb
+++ b/spec/models/waitlist_spec.rb
@@ -25,10 +25,17 @@
it "adds a user to the waitlist" do
waitlist = create(:waitlist_with_item)
user = create(:peter)
- waitlist.add_user(user)
+ expect(waitlist.add_user(user)).to be(true)
expect(waitlist.users).to include(user)
end
+ it "does not add item owner to waitlist" do
+ waitlist = create(:waitlist_with_item)
+ owner = Item.find_by(owner: waitlist.item.owner)
+ expect(waitlist.add_user(owner)).to be(false)
+ expect(waitlist.users).not_to include(owner)
+ end
+
it "removes a user from the waitlist" do
waitlist = create(:waitlist_with_item)
user = waitlist.users[0]
@@ -41,7 +48,7 @@
user = create(:peter)
waitlist.add_user(user)
notification = AddedToWaitlistNotification.find_by(user: user, item: waitlist.item)
- expect(notification).to_not be_nil
+ expect(notification).not_to be_nil
end
it "creates a moved up notification for users after when a user is removed from the waitlist" do
@@ -49,6 +56,6 @@
user = waitlist.users[0]
waitlist.remove_user(user)
notification = MoveUpOnWaitlistNotification.find_by(user: waitlist.users[0], item: waitlist.item)
- expect(notification).to_not be_nil
+ expect(notification).not_to be_nil
end
end
From fc5816101ce4b5a6101e8a6ed175dca7a56b5d74 Mon Sep 17 00:00:00 2001
From: LinoH5
Date: Tue, 13 Dec 2022 15:12:10 +0100
Subject: [PATCH 21/27] fixed failing tests
---
app/views/items/show.html.erb | 10 +++---
spec/factories/items.rb | 14 ++++++++
spec/features/items/show.html.erb_spec.rb | 34 +++++++++++--------
.../return_request_notifications_spec.rb | 1 +
.../added_to_waitlist_notification_spec.rb | 5 ---
.../move_up_on_waitlist_notification_spec.rb | 5 ---
spec/requests/items_spec.rb | 1 +
7 files changed, 42 insertions(+), 28 deletions(-)
delete mode 100644 spec/models/added_to_waitlist_notification_spec.rb
delete mode 100644 spec/models/move_up_on_waitlist_notification_spec.rb
diff --git a/app/views/items/show.html.erb b/app/views/items/show.html.erb
index 3502720b..9ee3623c 100644
--- a/app/views/items/show.html.erb
+++ b/app/views/items/show.html.erb
@@ -150,10 +150,12 @@
<%= ((@item.rental_start.nil? ? Time.now() : @item.rental_start) + (@item.rental_duration_sec.nil? ? 86400 : @item.rental_duration_sec)).strftime('%d.%m.%Y') %>
<%= t('views.show_item.waitlist')%>
- <% if !@item.waitlist.users.exists?(current_user.id) %>
- <%= t('views.show_item.users_waiting', amount: @item.waitlist.users.size) %>
- <% else %>
- <%= t('views.show_item.waitlist_position', position: @item.waitlist.users.find_index(current_user) + 1) %>
+ <% if !current_user.nil? %>
+ <% if !@item.waitlist.users.exists?(current_user.id) %>
+ <%= t('views.show_item.users_waiting', amount: @item.waitlist.users.size) %>
+ <% else %>
+ <%= t('views.show_item.waitlist_position', position: @item.waitlist.users.find_index(current_user) + 1) %>
+ <% end %>
<% end %>
<% if !current_user.nil? && current_user.id != @item.owner %>
diff --git a/spec/factories/items.rb b/spec/factories/items.rb
index a9f758e9..b4158254 100644
--- a/spec/factories/items.rb
+++ b/spec/factories/items.rb
@@ -25,6 +25,20 @@
lend_status { :pending_return }
owner { create(:user).id }
end
+ factory :lent, class: 'Item' do
+ name { "MyName3" }
+ category { "MyCategory" }
+ location { "MyLocation" }
+ description { "MyDescription" }
+ image { Rack::Test::UploadedFile.new('spec/testimages/test_image.png', 'image/png') }
+ price_ct { 1 }
+ rental_duration_sec { 1 }
+ rental_start { "2022-11-18 15:32:07" }
+ return_checklist { "MyChecklist" }
+ lend_status { :lent }
+ holder { create(:user).id }
+ owner { create(:user).id }
+ end
factory :item_book, class: 'Item' do
name { "Ruby on Rails by Example" }
category { "Books" }
diff --git a/spec/features/items/show.html.erb_spec.rb b/spec/features/items/show.html.erb_spec.rb
index bf2afd0a..ba2a2e90 100644
--- a/spec/features/items/show.html.erb_spec.rb
+++ b/spec/features/items/show.html.erb_spec.rb
@@ -9,6 +9,12 @@
item.waitlist.item = item
item
end
+ let(:item_lent) do
+ item_lent = create(:lent, owner: owner.id)
+ item_lent.waitlist = create(:waitlist_with_item)
+ item_lent.waitlist.item = item_lent
+ item_lent
+ end
it "renders without rental start and duration set" do
sign_in user
@@ -38,9 +44,9 @@
expect(page).to have_text(item.description)
end
- it "has enter waitlist button when not on list" do
+ it "has enter waitlist button when not on list and item not available" do
sign_in user
- visit item_path(item)
+ visit item_path(item_lent)
expect(page).to have_text("Enter Waitlist")
end
@@ -53,39 +59,39 @@
it "has leave waitlist button when on list" do
sign_in user
- item.waitlist.add_user(user)
- visit item_path(item)
+ item_lent.waitlist.add_user(user)
+ visit item_path(item_lent)
expect(page).to have_text("Leave Waitlist")
end
it "adds user to waitlist when clicking add to waitlist button" do
sign_in user
- visit item_path(item)
+ visit item_path(item_lent)
find(:button, "Enter Waitlist").click
- expect(Item.find(item.id).waitlist.users).to include(user)
+ expect(Item.find(item_lent.id).waitlist.users).to include(user)
end
it "removes user from waitlist when clicking remove from waitlist button" do
sign_in user
- item.waitlist.add_user(user)
- visit item_path(item)
+ item_lent.waitlist.add_user(user)
+ visit item_path(item_lent)
find(:button, "Leave Waitlist").click
- expect(Item.find(item.id).waitlist.users).not_to include(user)
+ expect(Item.find(item_lent.id).waitlist.users).not_to include(user)
end
it "creates added to waitlist notification when adding user to waitlist" do
sign_in user
- visit item_path(item)
+ visit item_path(item_lent)
find(:button, "Enter Waitlist").click
- notification = AddedToWaitlistNotification.find_by(user: user, item: item)
+ notification = AddedToWaitlistNotification.find_by(user: user, item: item_lent)
expect(notification).not_to be_nil
end
it "creates move up on waitlist notification when removing user from waitlist" do
- sign_in item.waitlist.users[0]
- visit item_path(item)
+ sign_in item_lent.waitlist.users[0]
+ visit item_path(item_lent)
find(:button, "Leave Waitlist").click
- notification = MoveUpOnWaitlistNotification.find_by(user: Item.find(item.id).waitlist.users[0], item: item)
+ notification = MoveUpOnWaitlistNotification.find_by(user: Item.find(item_lent.id).waitlist.users[0], item: item_lent)
expect(notification).not_to be_nil
end
diff --git a/spec/features/notifications/return_request_notifications_spec.rb b/spec/features/notifications/return_request_notifications_spec.rb
index 01be1a1a..9275d398 100644
--- a/spec/features/notifications/return_request_notifications_spec.rb
+++ b/spec/features/notifications/return_request_notifications_spec.rb
@@ -8,6 +8,7 @@
sign_in user
FactoryBot.reload
@notification = build(:return_request_notification, user: user)
+ @notification.item.waitlist = Waitlist.new
@notification.save
end
diff --git a/spec/models/added_to_waitlist_notification_spec.rb b/spec/models/added_to_waitlist_notification_spec.rb
deleted file mode 100644
index 5d6d941a..00000000
--- a/spec/models/added_to_waitlist_notification_spec.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-require 'rails_helper'
-
-RSpec.describe AddedToWaitlistNotification, type: :model do
- pending "add some examples to (or delete) #{__FILE__}"
-end
diff --git a/spec/models/move_up_on_waitlist_notification_spec.rb b/spec/models/move_up_on_waitlist_notification_spec.rb
deleted file mode 100644
index 7ec90afc..00000000
--- a/spec/models/move_up_on_waitlist_notification_spec.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-require 'rails_helper'
-
-RSpec.describe MoveUpOnWaitlistNotification, type: :model do
- pending "add some examples to (or delete) #{__FILE__}"
-end
diff --git a/spec/requests/items_spec.rb b/spec/requests/items_spec.rb
index 6374c95a..92579421 100644
--- a/spec/requests/items_spec.rb
+++ b/spec/requests/items_spec.rb
@@ -36,6 +36,7 @@
describe "GET /show" do
it "renders a successful response" do
item = create(:item)
+ item.waitlist = create(:waitlist_with_item)
get item_url(item)
expect(response).to be_successful
end
From 292a95c1dc4213eafa33655b62de6185a69ef6be Mon Sep 17 00:00:00 2001
From: LinoH5
Date: Tue, 13 Dec 2022 15:16:55 +0100
Subject: [PATCH 22/27] rubocop automatic corrections
---
app/models/user.rb | 2 +-
spec/features/items/show.html.erb_spec.rb | 3 ++-
2 files changed, 3 insertions(+), 2 deletions(-)
diff --git a/app/models/user.rb b/app/models/user.rb
index 03d9d076..0edf6621 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -14,7 +14,7 @@ class User < ApplicationRecord
has_many :owned_groups, through: :ownerships, source: :group
has_and_belongs_to_many :waitlists
-
+
def email_parts
email.split("@")[0].split(".")
end
diff --git a/spec/features/items/show.html.erb_spec.rb b/spec/features/items/show.html.erb_spec.rb
index ba2a2e90..3fd23d14 100644
--- a/spec/features/items/show.html.erb_spec.rb
+++ b/spec/features/items/show.html.erb_spec.rb
@@ -91,7 +91,8 @@
sign_in item_lent.waitlist.users[0]
visit item_path(item_lent)
find(:button, "Leave Waitlist").click
- notification = MoveUpOnWaitlistNotification.find_by(user: Item.find(item_lent.id).waitlist.users[0], item: item_lent)
+ notification = MoveUpOnWaitlistNotification.find_by(user: Item.find(item_lent.id).waitlist.users[0],
+ item: item_lent)
expect(notification).not_to be_nil
end
From a584763372a0b7a1b368198ab191a51b1f53c0e8 Mon Sep 17 00:00:00 2001
From: LinoH5
Date: Tue, 13 Dec 2022 16:29:48 +0100
Subject: [PATCH 23/27] rubocop fixes
---
app/controllers/items_controller.rb | 49 ++++++++++++-------
app/models/added_to_waitlist_notification.rb | 1 +
app/models/item.rb | 2 +-
.../move_up_on_waitlist_notification.rb | 1 +
app/models/waitlist.rb | 1 +
5 files changed, 34 insertions(+), 20 deletions(-)
diff --git a/app/controllers/items_controller.rb b/app/controllers/items_controller.rb
index cf1d461e..909aea20 100644
--- a/app/controllers/items_controller.rb
+++ b/app/controllers/items_controller.rb
@@ -1,3 +1,4 @@
+# rubocop:disable Metrics/ClassLength
class ItemsController < ApplicationController
before_action :set_item, only: %i[ show edit update destroy ]
@@ -24,15 +25,7 @@ def create
@item = Item.new(item_params)
@item.waitlist = Waitlist.new
- respond_to do |format|
- if @item.save
- format.html { redirect_to item_url(@item), notice: t("models.item.created") }
- format.json { render :show, status: :created, location: @item }
- else
- format.html { render :new, status: :unprocessable_entity }
- format.json { render json: @item.errors, status: :unprocessable_entity }
- end
- end
+ create_create_response
end
# PATCH/PUT /items/1 or /items/1.json
@@ -62,16 +55,7 @@ def add_to_waitlist
@item = Item.find(params[:id])
@user = current_user
- respond_to do |format|
- if @item.add_to_waitlist(@user) && @item.save
- format.html do
- redirect_to item_url(@item),
- notice: t("models.waitlist.added_to_waitlist", position: @item.waitlist.position(@user) + 1)
- end
- else
- format.html { redirect_to item_url(@item), alert: t("models.waitlist.failed_adding_to_waitlist") }
- end
- end
+ create_add_to_waitlist_response
end
def leave_waitlist
@@ -121,6 +105,31 @@ def deny_return
private
+ def create_create_response
+ respond_to do |format|
+ if @item.save
+ format.html { redirect_to item_url(@item), notice: t("models.item.created") }
+ format.json { render :show, status: :created, location: @item }
+ else
+ format.html { render :new, status: :unprocessable_entity }
+ format.json { render json: @item.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ def create_add_to_waitlist_response
+ respond_to do |format|
+ if @item.add_to_waitlist(@user) && @item.save
+ format.html do
+ redirect_to item_url(@item),
+ notice: t("models.waitlist.added_to_waitlist", position: @item.waitlist.position(@user) + 1)
+ end
+ else
+ format.html { redirect_to item_url(@item), alert: t("models.waitlist.failed_adding_to_waitlist") }
+ end
+ end
+ end
+
# Use callbacks to share common setup or constraints between actions.
def set_item
@item = Item.find(params[:id])
@@ -132,3 +141,5 @@ def item_params
:rental_start, :return_checklist, :owner, :holder, :waitlist_id, :lend_status)
end
end
+
+# rubocop:enable Metrics/ClassLength
diff --git a/app/models/added_to_waitlist_notification.rb b/app/models/added_to_waitlist_notification.rb
index 0451358a..bb8cc4b4 100644
--- a/app/models/added_to_waitlist_notification.rb
+++ b/app/models/added_to_waitlist_notification.rb
@@ -1,3 +1,4 @@
+# class of a notification send to user, when he/her has been added to a waitlist.
class AddedToWaitlistNotification < ApplicationRecord
acts_as :notification
diff --git a/app/models/item.rb b/app/models/item.rb
index d3f5caf1..968708cd 100644
--- a/app/models/item.rb
+++ b/app/models/item.rb
@@ -1,7 +1,7 @@
# class of a basic item.
class Item < ApplicationRecord
has_one_attached :image
- has_one :waitlist
+ has_one :waitlist, dependent: :destroy
has_many :lend_request_notifications, dependent: :destroy
has_and_belongs_to_many :users, join_table: "wishlist"
diff --git a/app/models/move_up_on_waitlist_notification.rb b/app/models/move_up_on_waitlist_notification.rb
index a7c750e6..a10fe41d 100644
--- a/app/models/move_up_on_waitlist_notification.rb
+++ b/app/models/move_up_on_waitlist_notification.rb
@@ -1,3 +1,4 @@
+# class of a notification send to user, when position in a waitlist changes.
class MoveUpOnWaitlistNotification < ApplicationRecord
acts_as :notification
diff --git a/app/models/waitlist.rb b/app/models/waitlist.rb
index 18f15e53..b2e06921 100644
--- a/app/models/waitlist.rb
+++ b/app/models/waitlist.rb
@@ -1,3 +1,4 @@
+# Waitlist model for handling adding and removing of users from items waitlist and corresponding side effects.
class Waitlist < ApplicationRecord
belongs_to :item
has_and_belongs_to_many :users
From e5c645cf91b1b24ead8a2ecdbaa6e416c3768c0a Mon Sep 17 00:00:00 2001
From: LinoH5
Date: Tue, 13 Dec 2022 16:39:41 +0100
Subject: [PATCH 24/27] test fix
---
spec/models/waitlist_spec.rb | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/spec/models/waitlist_spec.rb b/spec/models/waitlist_spec.rb
index 391787f5..032db5c3 100644
--- a/spec/models/waitlist_spec.rb
+++ b/spec/models/waitlist_spec.rb
@@ -31,7 +31,7 @@
it "does not add item owner to waitlist" do
waitlist = create(:waitlist_with_item)
- owner = Item.find_by(owner: waitlist.item.owner)
+ owner = User.find(waitlist.item.owner)
expect(waitlist.add_user(owner)).to be(false)
expect(waitlist.users).not_to include(owner)
end
From abcca7e3334b96227a24f8893a33191eb1030a60 Mon Sep 17 00:00:00 2001
From: Alexander Ungefug <82446024+AlexanderUngefug@users.noreply.github.com>
Date: Wed, 14 Dec 2022 12:05:16 +0100
Subject: [PATCH 25/27] set status to lent bei Item creation if holder is set
---
app/controllers/items_controller.rb | 1 +
app/models/item.rb | 4 ++++
2 files changed, 5 insertions(+)
diff --git a/app/controllers/items_controller.rb b/app/controllers/items_controller.rb
index 909aea20..83f8df2d 100644
--- a/app/controllers/items_controller.rb
+++ b/app/controllers/items_controller.rb
@@ -24,6 +24,7 @@ def edit
def create
@item = Item.new(item_params)
@item.waitlist = Waitlist.new
+ @item.set_status_lent unless @item.holder.nil?
create_create_response
end
diff --git a/app/models/item.rb b/app/models/item.rb
index 968708cd..216cc453 100644
--- a/app/models/item.rb
+++ b/app/models/item.rb
@@ -40,6 +40,10 @@ def deny_return
# TODO
end
+ def set_status_lent
+ self.lend_status = :lent
+ end
+
def price_in_euro=(euros)
self.price_ct = euros * 100
end
From b0a76029639a1ebbda71809a54fe609997d712a0 Mon Sep 17 00:00:00 2001
From: Alexander Ungefug <82446024+AlexanderUngefug@users.noreply.github.com>
Date: Wed, 14 Dec 2022 15:18:00 +0100
Subject: [PATCH 26/27] button notification style changed, rudimentary
Lendbutton implemented
---
app/controllers/items_controller.rb | 16 +++++++++++++++-
.../_lend_request_notification.html.erb | 2 +-
.../_return_request_notification.html.erb | 2 +-
app/views/notifications/index.html.erb | 14 +++-----------
config/routes.rb | 1 +
5 files changed, 21 insertions(+), 14 deletions(-)
diff --git a/app/controllers/items_controller.rb b/app/controllers/items_controller.rb
index 83f8df2d..5707e765 100644
--- a/app/controllers/items_controller.rb
+++ b/app/controllers/items_controller.rb
@@ -68,6 +68,13 @@ def leave_waitlist
end
def request_lend
+ @item = Item.find(params[:id])
+ @user = current_user
+ @item.holder = @user.id
+ @item.set_status_lent
+ @item.save
+
+ redirect_to item_url(@item)
end
def request_return
@@ -80,10 +87,17 @@ def request_return
borrower: @user)
@notification.save
end
-
redirect_to item_url(@item)
end
+ def accept_lend
+ @item = Item.find(params[:id])
+ @notification = LendRequestNotification.find_by(item: @item)
+ @item.set_status_lent
+ @item.holder = @notification.borrower.id
+ @item.save
+ end
+
def accept_return
@item = Item.find(params[:id])
@notification = ReturnRequestNotification.find_by(item: @item)
diff --git a/app/views/notifications/_lend_request_notification.html.erb b/app/views/notifications/_lend_request_notification.html.erb
index d0f8dca1..2d690f77 100644
--- a/app/views/notifications/_lend_request_notification.html.erb
+++ b/app/views/notifications/_lend_request_notification.html.erb
@@ -18,7 +18,7 @@
diff --git a/app/views/notifications/_return_request_notification.html.erb b/app/views/notifications/_return_request_notification.html.erb
index ab61e541..562f8919 100644
--- a/app/views/notifications/_return_request_notification.html.erb
+++ b/app/views/notifications/_return_request_notification.html.erb
@@ -1,6 +1,6 @@
diff --git a/app/views/notifications/index.html.erb b/app/views/notifications/index.html.erb
index cd05a71e..9192cd4a 100644
--- a/app/views/notifications/index.html.erb
+++ b/app/views/notifications/index.html.erb
@@ -8,18 +8,10 @@
<% notifications.each do |notification|%>
-
-
+
+
<%= render_if_exists notification.custom_partial, notification: notification %>
-
-
-
- <%= notification.title %>
-
-
- <%= notification.description %>
-
-
+
<% end %>
diff --git a/config/routes.rb b/config/routes.rb
index 67b8576d..e4c505e7 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -18,6 +18,7 @@
post 'accept_return/:id', to: 'items#accept_return', as: 'accept_return'
post 'deny_return/:id', to: 'items#deny_return', as: 'deny_return'
post 'request_lend/:id', to: 'items#request_lend', as: 'request_lend'
+ post 'accept_lend/:id', to: 'items#accept_lend', as: 'accept_lend'
resources :groups
# Defines the root path route ("/")
From 6efc13653de21280592c2255241b23e22b0e783e Mon Sep 17 00:00:00 2001
From: Alexander Ungefug <82446024+AlexanderUngefug@users.noreply.github.com>
Date: Wed, 14 Dec 2022 15:27:18 +0100
Subject: [PATCH 27/27] removed button styling by return request
---
app/views/notifications/_return_request_notification.html.erb | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/app/views/notifications/_return_request_notification.html.erb b/app/views/notifications/_return_request_notification.html.erb
index 562f8919..ab61e541 100644
--- a/app/views/notifications/_return_request_notification.html.erb
+++ b/app/views/notifications/_return_request_notification.html.erb
@@ -1,6 +1,6 @@