Class: ItemsController

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

Instance Method Summary collapse

Instance Method Details

#createObject

POST /items POST /items.json



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'app/controllers/items_controller.rb', line 211

def create
  @item = Item.new(item_params)
  manifestation = Manifestation.find(@item.manifestation_id)

  respond_to do |format|
    if @item.save
      @item.manifestation = manifestation
      Item.transaction do
        if defined?(EnjuCirculation)
          if @item.reserved?
            flash[:message] = t('item.this_item_is_reserved')
            @item.retain(current_user)
          end
        end
      end
      format.html { redirect_to(@item, notice: t('controller.successfully_created', model: t('activerecord.models.item'))) }
      format.json { render json: @item, status: :created, location: @item }
    else
      prepare_options
      format.html { render action: "new" }
      format.json { render json: @item.errors, status: :unprocessable_entity }
    end
  end
end

#destroyObject

DELETE /items/1 DELETE /items/1.json



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'app/controllers/items_controller.rb', line 253

def destroy
  manifestation = @item.manifestation
  @item.destroy

  respond_to do |format|
    flash[:notice] = t('controller.successfully_deleted', model: t('activerecord.models.item'))
    if @item.manifestation
      format.html { redirect_to items_url(manifestation_id: manifestation.id) }
      format.json { head :no_content }
    else
      format.html { redirect_to items_url }
      format.json { head :no_content }
    end
  end
end

#editObject

GET /items/1/edit



198
199
200
201
202
203
204
205
206
207
# File 'app/controllers/items_controller.rb', line 198

def edit
  @item.library_id = @item.shelf.library_id
  @manifestation = @item.manifestation
  if defined?(EnjuCirculation)
    unless @item.use_restriction
      @item.build_item_has_use_restriction
      @item.item_has_use_restriction.use_restriction = UseRestriction.where(name: 'Not For Loan').first
    end
  end
end

#indexObject

GET /items GET /items.json



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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/controllers/items_controller.rb', line 15

def index
  query = params[:query].to_s.strip
  per_page = Item.default_per_page
  @count = {}
  if user_signed_in?
    if current_user.has_role?('Librarian')
      if params[:format] == 'text'
        per_page = 65534
      elsif params[:mode] == 'barcode'
        per_page = 40
      end
    end
  end

  if defined?(InventoryFile)
    if @inventory_file
      if user_signed_in?
        if current_user.has_role?('Librarian')
          case params[:inventory]
          when 'not_in_catalog'
            mode = 'not_in_catalog'
          else
            mode = 'not_on_shelf'
          end
          order = 'items.id'
          @items = Item.inventory_items(@inventory_file, mode).order(order).page(params[:page]).per(per_page)
        else
          access_denied
          return
        end
      else
        redirect_to new_user_session_url
        return
      end
    end
  end

  unless @inventory_file
    search = Sunspot.new_search(Item)
    selected_attributes = [
      :id, :item_identifier, :call_number, :manifestation_id, :acquired_at,
      :binding_item_identifier, :binding_call_number, :binded_at,
      :include_supplements, :url, :note,
      :circulation_status_id, :shelf_id,
      :created_at, :updated_at
    ]
    selected_attributes += [
      :memo, :required_role_id, :budget_type_id, :bookstore_id, :price
    ] if current_user.try(:has_role?, 'Librarian')
    search.data_accessor_for(Item).select = selected_attributes
    set_role_query(current_user, search)

    @query = query.dup
    if query.present?
      search.build do
        fulltext query
      end
    end

    agent = @agent
    manifestation = @manifestation
    shelf = @shelf
    unless params[:mode] == 'add'
      search.build do
        with(:agent_ids).equal_to agent.id if agent
        with(:manifestation_id).equal_to manifestation.id if manifestation
        with(:shelf_id).equal_to shelf.id if shelf
        with(:circulation_status).equal_to params[:circulation_status] if params[:circulation_status].present?
        facet :circulation_status if defined?(EnjuCirculation)
      end
    end

    search.build do
      order_by(:created_at, :desc)
    end

    role = current_user.try(:role) || Role.default_role
    search.build do
      with(:required_role_id).less_than_or_equal_to role.id
    end

    if params[:acquired_from].present?
      begin
        acquired_from = Time.zone.parse(params[:acquired_from]).beginning_of_day
        @acquired_from = acquired_from.strftime('%Y-%m-%d')
      rescue ArgumentError
      rescue NoMethodError
      end
    end
    if params[:acquired_until].present?
      begin
        acquired_until = @acquired_until = Time.zone.parse(params[:acquired_until]).end_of_day
        @acquired_until = acquired_until.strftime('%Y-%m-%d')
      rescue ArgumentError
      rescue NoMethodError
      end
    end
    search.build do
      with(:acquired_at).greater_than_or_equal_to acquired_from.beginning_of_day if acquired_from
      with(:acquired_at).less_than acquired_until.tomorrow.beginning_of_day if acquired_until
    end

    page = params[:page] || 1
    search.query.paginate(page.to_i, per_page)
    result = search.execute
    @circulation_status_facet = result.facet(:circulation_status).rows if defined?(EnjuCirculation)
    @items = result.results
    @count[:total] = @items.total_entries
  end

  if defined?(EnjuBarcode)
    if params[:mode] == 'barcode'
      render action: 'barcode', layout: false
      return
    end
  end

  flash[:page_info] = { page: page, query: query }

  respond_to do |format|
    format.html # index.html.erb
    format.json
    format.text
    format.atom
  end
end

#newObject

GET /items/new GET /items/new.json



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'app/controllers/items_controller.rb', line 157

def new
  if Shelf.real.blank?
    flash[:notice] = t('item.create_shelf_first')
    redirect_to libraries_url
    return
  end
  unless @manifestation
    flash[:notice] = t('item.specify_manifestation')
    redirect_to manifestations_url
    return
  end
  if @manifestation.series_master?
    flash[:notice] = t('item.specify_manifestation')
    redirect_to manifestations_url(parent_id: @manifestation.id)
    return
  end
  @item = Item.new
  @item.shelf = @library.shelves.first
  @item.manifestation = @manifestation
  if defined?(EnjuCirculation)
    @circulation_statuses = CirculationStatus.where(
      name: [
        'In Process',
        'Available For Pickup',
        'Available On Shelf',
        'Claimed Returned Or Never Borrowed',
        'Not Available']
    ).order(:position)
    @item.circulation_status = CirculationStatus.where(name: 'In Process').first
    @item.checkout_type = @manifestation.carrier_type.checkout_types.first
    @item.item_has_use_restriction = ItemHasUseRestriction.new
    @item.item_has_use_restriction.use_restriction = UseRestriction.where(name: 'Not For Loan').first
  end

  respond_to do |format|
    format.html # new.html.erb
    format.json { render json: @item }
  end
end

#showObject

GET /items/1 GET /items/1.json



144
145
146
147
148
149
150
151
152
153
# File 'app/controllers/items_controller.rb', line 144

def show
  #@item = Item.find(params[:id])
  @item = @item.versions.find(@version).item if @version
  @manifestation = @item.manifestation unless @manifestation

  respond_to do |format|
    format.html # show.html.erb
    format.json
  end
end

#updateObject

PUT /items/1 PUT /items/1.json



238
239
240
241
242
243
244
245
246
247
248
249
# File 'app/controllers/items_controller.rb', line 238

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