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



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

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



131
132
133
# File 'app/controllers/checkouts_controller.rb', line 131

def edit
  @new_due_date = @checkout.get_new_due_date
end

#indexObject

GET /checkouts GET /checkouts.json



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
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/controllers/checkouts_controller.rb', line 11

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 params[:format] == 'txt'
    per_page = 65534
  else
    per_page = Checkout.default_per_page
  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
          with(:checked_in_at).equal_to nil unless user.profile.save_checkout_history
        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
        end
      end

      unless current_user.profile.save_checkout_history?
        search.build do
          with(:checked_in_at).equal_to nil
        end
      end
    end

    if current_user.try(:has_role?, 'Librarian')
      if @item
        item = @item
        search.build do
          with(:item_identifier).equal_to item.item_identifier
        end
      end
    else
      if @item
        access_denied; return
      end
    end

    if params[:view] == 'overdue'
      if params[:days_overdue]
        date = params[:days_overdue].to_i.days.ago.beginning_of_day
      else
        date = 1.days.ago.beginning_of_day
      end
      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
    page = params[:page] || 1
    search.query.paginate(page.to_i, Checkout.default_per_page)
    @checkouts = search.execute!.results
    @checkouts_facet = search.facet(:reserved).rows
  end

  @days_overdue = params[:days_overdue] ||= 1

  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



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'app/controllers/checkouts_controller.rb', line 167

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



123
124
125
126
127
128
# File 'app/controllers/checkouts_controller.rb', line 123

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



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

def update
  @checkout.assign_attributes(checkout_params)
  @checkout.due_date = @checkout.due_date.end_of_day
  @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