Skip to content

Commit

Permalink
fixed issue 16 and 20
Browse files Browse the repository at this point in the history
  • Loading branch information
Claudiu Tarta committed Jan 4, 2016
1 parent 290e305 commit 7af657e
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 19 deletions.
8 changes: 7 additions & 1 deletion app/controllers/items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,18 @@ def create_bulk


def return
item = Item.find_by_barcode(params[:loan][:barcode])
if params.has_key?(:loan)
item = Item.find_by_barcode(params[:loan][:barcode])
else
item = Item.find_by_barcode(params[:id])
end

respond_to do |format|
# mark the loan oldest loan for this user and item as returned
if user_active? && item.return(current_user)
format.json { render json: {success: true, error:nil} }
elsif params.has_key?(:user) && item.return(User.find(params[:user]))
format.html {redirect_to loans_path}
else
format.json { render json: {error: "error"}}
end
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/loans_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ class LoansController < ApplicationController
def index
if user_active?
@search = Loan.ransack({"loan_user_search"=> current_user.id})
@loans = @search.result.order("returned_on DESC").paginate(:page => params[:page], :per_page => 25)
@loans = @search.result.order(:due_date).paginate(:page => params[:page], :per_page => 25)
elsif admin_signed_in?
@search = Loan.ransack(params[:q])
@loans = @search.result.order("returned_on DESC").paginate(:page => params[:page], :per_page => 25)
@loans = @search.result.order(:user_id,:due_date).paginate(:page => params[:page], :per_page => 25)
else
session_active?
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def index
# GET /users/1
# GET /users/1.json
def show
@loans = Loan.where(user_id: @user.id).order( 'returned_on DESC' )
@loans = Loan.where(user_id: @user.id).order( 'due_date' )
end

# GET /users/new
Expand Down
2 changes: 1 addition & 1 deletion app/models/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def to_label
end

def return(user)
loans = self.loans.where(returned_on: nil, user: user).order('created_at ASC')
loans = self.loans.where(returned_on: nil, user: user).order('due_date')
if loans.nil? || loans.empty?
false
else
Expand Down
8 changes: 8 additions & 0 deletions app/models/loan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ def self.loan_user_search(searchstr)
end
end

def overdue
if self.due_date < Time.now && self.returned_on.nil?
return true
else
return false
end
end

def return_dates
due_date = self.due_date
dates = []
Expand Down
7 changes: 5 additions & 2 deletions app/views/items/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
%th
= sort_link @search, :barcode, "Barcode"
%th
= sort_link @search, :location, "Location"
= sort_link @search, :location_id, "Location"
%th
= sort_link @search, :category_id, "Category"
%th
Expand Down Expand Up @@ -60,7 +60,10 @@
%td= item.title
-if Rails.env.development? || admin_signed_in?
%td= item.barcode
%td= item.location.to_label
- if item.location
%td= item.location.to_label
-else
%td Location missing
%td
= item.category.name.humanize unless item.category.nil?
%td= item.publisher
Expand Down
11 changes: 9 additions & 2 deletions app/views/layouts/application.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,15 @@
%a{:href => categories_path}=fa_icon('clipboard', text: "Categories")
%li
%a{:href => locations_path}=fa_icon('building', text: "Locations")
%li
%a{:href => users_path}=fa_icon('user', text: "Borrowers")
%li.dropdown
%a.dropdown-toggle{'data-toggle' => 'dropdown'}
=fa_icon('newspaper-o', text: "Reports")
%span.caret
%ul#dashboard-dropdown.dropdown-menu{role: 'dashboardMenu'}
%li
%a{:href => users_path}=fa_icon('user', text: "Borrowers")
%li
%a{:href => loans_path}=fa_icon('calendar', text: "Loans")
%li
%a{:href => item_request_index_path}=fa_icon('question-circle', text: "Requests")
-if admin_signed_in?
Expand Down
19 changes: 10 additions & 9 deletions app/views/loans/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,19 @@
%th Returned on
-if admin_signed_in?
%th
%th
%th

%tbody
-if admin_signed_in?
= search_form_for @search,:builder => SimpleForm::FormBuilder do |f|
= f.input :loan_user_search,:label => "User", collection: User.all,include_blank: true
= f.submit "Search",class: "btn btn-info search"
- @loans.each do |loan|
%tr
%td= loan.user.to_label
- if loan.overdue
- info= 'danger'
- else
- info= ''
%tr{class:info}
%td= link_to loan.user.to_label, user_path(loan.user_id)
%td= loan.item.to_label
%td= loan.due_date
- if loan.returned_on
Expand All @@ -32,11 +34,10 @@
%th
%p
Active
-if admin_signed_in?
%td= link_to 'Show', loan
%td= link_to 'Edit', edit_loan_path(loan)
%td= link_to 'Destroy', loan, :method => :delete, :data => { :confirm => 'Are you sure?' }

-if admin_signed_in? && loan.returned_on.nil?
%td= link_to 'Return', return_item_path(loan.item.barcode,:user => loan.user), :method => :post, :data => { :confirm => 'Are you sure?' }
-else
%td
%br
= will_paginate @loans
-#= link_to 'New Loan', new_loan_path, { :class=>"btn btn-success btn-lg" }
Expand Down
6 changes: 5 additions & 1 deletion app/views/users/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@
%th Returned on
%tbody
- @loans.each do |loan|
%tr
- if loan.overdue
- info= 'danger'
- else
- info= ''
%tr{class:info}
%td= loan.item.title
-if admin_signed_in?
%td= loan.item.barcode
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
post :fImport
end
member do
post :return
get :request_item
end
end
Expand Down

0 comments on commit 7af657e

Please sign in to comment.