Class: CheckoutsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/checkouts_controller.rb

Instance Method Summary collapse

Instance Method Details

#destroyObject

DELETE /checkouts/1 DELETE /checkouts/1.json



141
142
143
144
145
146
147
148
149
150
151
# File 'app/controllers/checkouts_controller.rb', line 141

def destroy
  user = @checkout.user
  @checkout.operator = current_user
  @checkout.user_id = nil
  @checkout.save!

  respond_to do |format|
    format.html { redirect_to checkouts_url(user_id: user.username), notice: t('controller.successfully_deleted', model: t('activerecord.models.checkout')) }
    format.json { head :no_content }
  end
end

#editObject

GET /checkouts/1/edit



118
119
120
# File 'app/controllers/checkouts_controller.rb', line 118

def edit
  @new_due_date = @checkout.get_new_due_date
end

#indexObject

GET /checkouts GET /checkouts.json



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/controllers/checkouts_controller.rb', line 10

def index
  if params[:icalendar_token].present?
    icalendar_user = Profile.where(checkout_icalendar_token: params[:icalendar_token]).first.try(:user)
    if icalendar_user.blank?
      raise ActiveRecord::RecordNotFound
    else
      @checkouts = icalendar_user.checkouts.not_returned.order('checkouts.id DESC')
    end
  else
    unless current_user
      access_denied; return
    end
  end

  if ['txt', 'rss'].include?(params[:format].to_s.downcase)
    per_page = 500
    page = 1
  else
    per_page = Checkout.default_per_page
    page = params[:page] || 1
  end

  unless icalendar_user
    search = Checkout.search
    if @user
      user = @user
      if current_user.try(:has_role?, 'Librarian')
        search.build do
          with(:username).equal_to user.username
        end
      else
        if current_user == user
          redirect_to checkouts_url(format: params[:format])
          return
        else
          access_denied
          return
        end
      end
    else
      unless current_user.try(:has_role?, 'Librarian')
        search.build do
          with(:username).equal_to current_user.username
          unless current_user.profile.save_checkout_history?
            with(:checked_in_at).equal_to nil
          end
        end
      end
    end

    if @item
      item = @item
      search.build do
        with(:item_identifier).equal_to item.item_identifier
      end
    end

    if params[:days_overdue].present?
      days_overdue = params[:days_overdue].to_i
      date = days_overdue.days.ago.beginning_of_day
      search.build do
        with(:due_date).less_than date
        with(:checked_in_at).equal_to nil
      end
    end

    if params[:reserved].present?
      if params[:reserved] == 'true'
        @reserved = reserved = true
      elsif params[:reserved] == 'false'
        @reserved = reserved = false
      end
      search.build do
        with(:reserved).equal_to reserved
      end
    end

    search.build do
      order_by :created_at, :desc
      facet :reserved
    end
    search.query.paginate(page.to_i, per_page)
    @checkouts = search.execute!.results
    @checkouts_facet = search.facet(:reserved).rows
  end

  @days_overdue = days_overdue if days_overdue

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @checkouts }
    format.rss  { render layout: false }
    format.ics
    format.txt
    format.atom
  end
end

#remove_allObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'app/controllers/checkouts_controller.rb', line 153

def remove_all
  if @user
    unless current_user.has_role?('Librarian')
      if @user != current_user
        access_denied; return
      end
    end
    Checkout.remove_all_history(@user)
  end

  respond_to do |format|
    format.html { redirect_to checkouts_url, notice: t('controller.successfully_deleted', model: t('activerecord.models.checkout')) }
    format.json { head :no_content }
  end
end

#showObject

GET /checkouts/1 GET /checkouts/1.json



110
111
112
113
114
115
# File 'app/controllers/checkouts_controller.rb', line 110

def show
  respond_to do |format|
    format.html # show.html.erb
    format.json { render json: @checkout }
  end
end

#updateObject

PUT /checkouts/1 PUT /checkouts/1.json



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'app/controllers/checkouts_controller.rb', line 124

def update
  @checkout.assign_attributes(checkout_params)
  @checkout.checkout_renewal_count += 1

  respond_to do |format|
    if @checkout.save
      format.html { redirect_to @checkout, notice: t('controller.successfully_updated', model: t('activerecord.models.checkout')) }
      format.json { head :no_content }
    else
      format.html { render action: "edit" }
      format.json { render json: @checkout.errors, status: :unprocessable_entity }
    end
  end
end