Class: Admin::InspectableItemsController

Inherits:
AdminController
  • Object
show all
Defined in:
app/controllers/admin/inspectable_items_controller.rb

Overview

Controller for managing inspectable items.

Instance Method Summary collapse

Instance Method Details

#createObject

POST /inspectable_items POST /inspectable_items.json Creates a new inspectable item.

Parameters:

  • inspectable_item_params (Hash)

    The parameters for creating the inspectable item.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/admin/inspectable_items_controller.rb', line 48

def create
  params = inspectable_item_params
  params[:created_by_id] = current_user.id
  params[:updated_by_id] = current_user.id
  properties = {
    'item_weight' => params['item_weight']
  }
  params[:properties] = properties
  params.delete(:item_weight)
  @inspectable_item = InspectableItem.new(params)

  respond_to do |format|
    if @inspectable_item.save
      format.html { redirect_to admin_inspectable_items_path, notice: 'Inspectable item was successfully created.' }
      format.json { render :show, status: :created, location: @inspectable_item }
    else
      format.html { render :new }
      format.json { render json: @inspectable_item.errors, status: :unprocessable_entity }
    end
  end
end

#destroyObject

DELETE /inspectable_items/1 DELETE /inspectable_items/1.json Deletes an existing inspectable item.



97
98
99
100
101
102
103
# File 'app/controllers/admin/inspectable_items_controller.rb', line 97

def destroy
  @inspectable_item.destroy
  respond_to do |format|
    format.html { redirect_to admin_inspectable_items_url, notice: 'Inspectable item was successfully destroyed.' }
    format.json { head :no_content }
  end
end

#editObject

GET /inspectable_items/1/edit Displays a form for editing an inspectable item.



41
# File 'app/controllers/admin/inspectable_items_controller.rb', line 41

def edit; end

#indexObject

GET /inspectable_items GET /inspectable_items.json Displays a list of inspectable items.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/controllers/admin/inspectable_items_controller.rb', line 11

def index
  respond_to do |format|
    format.xlsx do
      @inspectable_items = InspectableItem.search(params[:area], params[:item_name],
                                                  params[:item_code])
      response.headers[
        'Content-Disposition'
      ] = "attachment; filename='items.xlsx'"
    end
    format.html do
      @inspectable_items = InspectableItem.search(params[:inspection_area_id], params[:item_name],
                                                  params[:item_code]).page params[:page]
      render action: :index, layout: request.xhr?.nil?
    end
  end
end

#inspectable_items_searchObject

GET /inspectable_items/inspectable_items_search Searches for inspectable items based on item name.

Parameters:

  • item_name (String)

    The name of the inspectable item to search for.



109
110
111
112
113
114
115
116
117
118
# File 'app/controllers/admin/inspectable_items_controller.rb', line 109

def inspectable_items_search
  item_name = params[:item_name]
  @inspectable_items = InspectableItem.where('title ILIKE ?', "%#{item_name}%").order(updated_at: :desc)

  items = @inspectable_items.map do |item|
    { id: item.id, title: item.title, area_name: item.inspection_area.area_name }
  end

  render json: items
end

#newObject

GET /inspectable_items/new Displays a form for creating a new inspectable item.



35
36
37
# File 'app/controllers/admin/inspectable_items_controller.rb', line 35

def new
  @inspectable_item = InspectableItem.new
end

#showObject

GET /inspectable_items/1 GET /inspectable_items/1.json Displays the details of a specific inspectable item.



31
# File 'app/controllers/admin/inspectable_items_controller.rb', line 31

def show; end

#updateObject

PATCH/PUT /inspectable_items/1 PATCH/PUT /inspectable_items/1.json Updates an existing inspectable item.

Parameters:

  • inspectable_item_params (Hash)

    The parameters for updating the inspectable item.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/controllers/admin/inspectable_items_controller.rb', line 75

def update
  params = inspectable_item_params
  params[:updated_by_id] = current_user.id
  properties = {
    'item_weight' => params['item_weight']
  }
  params[:properties] = properties
  params.delete(:item_weight)
  respond_to do |format|
    if @inspectable_item.update(params)
      format.html { redirect_to admin_inspectable_items_path, notice: 'Inspectable item was successfully updated.' }
      format.json { render :show, status: :ok, location: @inspectable_item }
    else
      format.html { render :edit }
      format.json { render json: @inspectable_item.errors, status: :unprocessable_entity }
    end
  end
end