Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Cl/owner return #245

Merged
merged 5 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions app/controllers/items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ def borrow
@item.cancel_reservation_for(@user)

respond_to do |format|
if @lending.save
if @lending&.save
format.html { redirect_to @item, notice: msg }
format.json { render :index, status: :ok, location: @item }
else
format.html { render :show, status: :unprocessable_entity, notice: msg }
format.html { render @item, status: :unprocessable_entity, notice: msg }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just out of curiosity, what was the reason for this change?

format.json { render json: @lending.errors, status: :unprocessable_entity }
end
end
Expand All @@ -123,16 +123,20 @@ def give_back
@lending = Lending.where(item_id: @item.id, user_id: @user.id, completed_at: nil).first
@lending.completed_at = Time.current
msg = I18n.t("items.messages.successfully_returned")
elsif @item.borrowed? && @user.can_manage?(@item)
@lending = Lending.where(item_id: @item.id, completed_at: nil).first
@lending.completed_at = Time.current
msg = I18n.t("items.messages.successfully_returned-by-owner")
else
msg = I18n.t("items.messages.lending_error")
end

respond_to do |format|
if @lending.save
if @lending&.save
format.html { redirect_to @item, notice: msg }
format.json { render :index, status: :ok, location: @item }
else
format.html { render :show, status: :unprocessable_entity, notice: msg }
format.html { redirect_to @item, status: :unprocessable_entity, notice: msg }
format.json { render json: @lending.errors, status: :unprocessable_entity }
end
end
Expand All @@ -154,7 +158,7 @@ def reserve
format.html { redirect_to @item, notice: msg }
format.json { render @item, status: :ok, location: @item }
else
format.html { render :show, status: :unprocessable_entity }
format.html { render :show, status: :unprocessable_entity, notice: msg }
format.json { render json: @reservation.errors, status: :unprocessable_entity }
end
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ def can_manage?(item)
false
end

def can_return_as_owner?(item)
!item.borrowed_by?(self) && item.borrowed? && can_manage?(item)
end

def items
# get all items where the user is a manager of any group
Item.joins(:manager_groups).where(groups: { id: groups })
Expand Down
8 changes: 8 additions & 0 deletions app/views/items/_item_largescreen.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@
)%>
<% end %>

<% if current_user.can_return_as_owner?(@item)%>
<%= render(ItemBorrowButton.new(
text: I18n.t("items.buttons.owner-return"),
action: item_give_back_path(@item),
bootstrap_classes: "mx-auto btn btn-primary")
)%>
<% end %>

<% if @item.borrowed_by?(current_user) && @src_is_qrcode %>
<%= render(ItemBorrowButton.new(
text: I18n.t("items.buttons.return"),
Expand Down
10 changes: 9 additions & 1 deletion app/views/items/_item_smallscreen.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,15 @@
bootstrap_classes: "btn btn-secondary item-action-button")
)%>
<% end %>


<% if current_user.can_return_as_owner?(@item)%>
<%= render(ItemBorrowButton.new(
text: I18n.t("items.buttons.owner-return"),
action: item_give_back_path(@item),
bootstrap_classes: "mx-auto btn btn-primary")
)%>
<% end %>

<% if @item.borrowable_by?(current_user) && @src_is_qrcode %>
<%= render(ItemBorrowButton.new(
text: I18n.t("items.buttons.borrow"),
Expand Down
2 changes: 2 additions & 0 deletions config/locales/views/landing_page/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ de:
successfully_destroyed: "Item wurde erfolgreich zerstört."
successfully_borrowed: "Item wurde erfolgreich ausgeliehen"
successfully_returned: "Item wurde erfolgreich zurückgegeben"
successfully_returned-by-owner: "Item wurde erfolgreich vom Besitzer zurückgegeben"
successfully_reserved: "Item wurde erfolgreich reserviert"
unsuccessfully_reserved: "Item Reservierung fehlgeschlagen"
lending_error: "Ausleihsystem Fehler"
Expand All @@ -37,6 +38,7 @@ de:
back: "Zurück zur Items Übersicht"
borrow: "Ausleihen"
return: "Zurückgeben"
owner-return: "Als Besitzer zurückgeben"
reserve: "Reservieren"
download_qrcode: "QR-Code herunterladen"
status_badge:
Expand Down
2 changes: 2 additions & 0 deletions config/locales/views/landing_page/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ en:
successfully_destroyed: "Item was successfully destroyed."
successfully_borrowed: "Item was successfully borrowed"
successfully_returned: "Item was successfully returned"
successfully_returned-by-owner: "Item was successfully returned by owner"
successfully_reserved: "Item was successfully reserved"
unsuccessfully_reserved: "Item reservation failed"
lending_error: "Lending error"
Expand All @@ -36,6 +37,7 @@ en:
reserve: "Reserve"
borrow: "Borrow"
return: "Return"
owner-return: "Return as owner"
download_qrcode: "Download QR-Code"
status_badge:
available: "Available"
Expand Down
33 changes: 33 additions & 0 deletions spec/features/items/return_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require "rails_helper"

describe "returining process", type: :feature do
let(:item) { FactoryBot.create(:item) }
let(:password) { 'password' }
let(:user) { FactoryBot.create(:user, email: '[email protected]', password: password) }
let(:user2) { FactoryBot.create(:user, password: password) }

before do
manage_group = FactoryBot.create(:group)
borrow_group = FactoryBot.create(:group)
FactoryBot.create(:membership, user: user, group: borrow_group)
FactoryBot.create(:membership, user: user2, group: manage_group)
FactoryBot.create(:membership, user: user2, group: borrow_group)
FactoryBot.create(:permission, item: item, group: borrow_group, permission_type: :can_borrow)
FactoryBot.create(:permission, item: item, group: manage_group, permission_type: :can_manage)
end

context "with two users - one with management rights -" do

it "does return the correct item, when it is returned by the owner" do
time = Time.zone.now
FactoryBot.create(:lending, item_id: item.id, user_id: user.id, started_at: time, completed_at: nil,
due_at: 2.days.from_now)
sign_in user2
visit item_path(item)
expect(page).to have_text(:visible, I18n.t("items.buttons.owner-return"))
page.find_button(I18n.t("items.buttons.owner-return"), match: :first).click
lending = Lending.where(item_id: item.id, user_id: user.id).where(["completed_at > :time", { time: time }]).first
expect(lending).not_to be_nil
end
end
end
15 changes: 15 additions & 0 deletions spec/features/items/show_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,19 @@
expect(page).not_to have_text(:visible, I18n.t("items.buttons.delete"))
expect(page).not_to have_text(:visible, I18n.t("items.buttons.download_qrcode"))
end

it "does not display owner-return button if item is not borrowed" do
sign_in @user
visit item_path(@item)
expect(page).not_to have_text(:visible, I18n.t("items.buttons.owner-return"))
end

it "does not display owner-return button if item is borrowed by the owner himself" do
sign_in @user
FactoryBot.create(:lending, item_id: @item.id, user_id: @user.id, started_at: Time.zone.now, completed_at: nil,
due_at: 2.days.from_now)
visit "#{item_path(@item)}?src=qrcode"
expect(page).not_to have_text(:visible, I18n.t("items.buttons.owner-return"))
expect(page).to have_text(:visible, I18n.t("items.buttons.return"))
end
end
38 changes: 37 additions & 1 deletion spec/models/item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@
it "does not allow reservation or borrowing by user2 if user has a lending" do
FactoryBot.create(:lending, item_id: item.id, user_id: user.id, started_at: Time.zone.now, completed_at: nil,
due_at: 2.days.from_now)

expect(item.borrowable_by?(user2)).to be false
expect(item.reservable_by?(user2)).to be false
end
Expand All @@ -110,7 +109,44 @@
expect(item.borrowable_by?(user2)).to be false
expect(item.reservable_by?(user2)).to be false
end
end
end

describe "Owner return:" do
let(:user) { FactoryBot.create(:user) }
let(:user2) { FactoryBot.create(:user) }

before do
manage_group = FactoryBot.create(:group)
borrow_group = FactoryBot.create(:group)
FactoryBot.create(:membership, user: user, group: borrow_group)
FactoryBot.create(:membership, user: user2, group: manage_group)
FactoryBot.create(:membership, user: user2, group: borrow_group)
FactoryBot.create(:permission, item: item, group: borrow_group, permission_type: :can_borrow)
FactoryBot.create(:permission, item: item, group: manage_group, permission_type: :can_manage)
end

it "does not allow owner-return if user has no management rights" do
expect(user.can_return_as_owner?(item)).to be false
FactoryBot.create(:lending, item_id: item.id, user_id: user2.id, started_at: Time.zone.now, completed_at: nil,
due_at: 2.days.from_now)
expect(user.can_return_as_owner?(item)).to be false
end

it "does not allow owner-return if item is borrowed by owner himself" do
FactoryBot.create(:lending, item_id: item.id, user_id: user2.id, started_at: Time.zone.now, completed_at: nil,
due_at: 2.days.from_now)
expect(user2.can_return_as_owner?(item)).to be false
end

it "does not allow owner-return if user has management rights but item is not borrowed" do
expect(user2.can_return_as_owner?(item)).to be false
end

it "does allow owner-return if user has management rights and item is borrowed" do
FactoryBot.create(:lending, item_id: item.id, user_id: user.id, started_at: Time.zone.now, completed_at: nil,
due_at: 2.days.from_now)
expect(user2.can_return_as_owner?(item)).to be true
end
end

Expand Down