Class: Admin::UnitsController

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

Overview

Controller for managing units.

Instance Method Summary collapse

Instance Method Details

#createObject

POST /units POST /units.json Creates a new unit.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/admin/units_controller.rb', line 43

def create
  @unit = Unit.new(unit_params.merge(properties: properties_params, created_by_id: current_user.id))
  respond_to do |format|
    if @unit.save
      ApplicationHelper.update_units_details_in_property(@unit.property_id)
      @building = Building.find(@unit.building_id)
      format.html { redirect_to admin_building_path(@building), notice: 'Unit was successfully created.' }
      format.json { render :show, status: :created, location: @unit }
    else
      format.html { render :new }
      format.json { render json: @unit.errors, status: :unprocessable_entity }
    end
  end
end

#destroyObject

DELETE /units/1 DELETE /units/1.json Deletes an existing unit.



77
78
79
80
81
82
83
84
85
86
# File 'app/controllers/admin/units_controller.rb', line 77

def destroy
  building_id = @unit.building_id
  property_id = @unit.property_id
  @unit.destroy
  ApplicationHelper.update_units_details_in_property(property_id)
  respond_to do |format|
    format.html { redirect_to admin_building_path(Building.find(building_id)), notice: 'Unit was successfully destroyed.' }
    format.json { head :no_content }
  end
end

#editObject

GET /units/1/edit Displays a form for editing a unit.



38
# File 'app/controllers/admin/units_controller.rb', line 38

def edit; end

#indexObject

GET /units GET /units.json Displays a list of units.



11
12
13
14
15
16
17
18
19
# File 'app/controllers/admin/units_controller.rb', line 11

def index
  @units = Unit.search(params[:property_id]).page params[:page]

  if request.xhr?
    render action: :index ,layout: false
  else
    render action: :index, layout: true
  end
end

#newObject

GET /units/new Displays a form for creating a new unit.



28
29
30
31
32
33
34
# File 'app/controllers/admin/units_controller.rb', line 28

def new
  @unit = Unit.new
  if params[:unit]
    @property = Property.find(params[:unit][:property_id])
    @building = Building.find(params[:unit][:building_id])
  end
end

#showObject

GET /units/1 GET /units/1.json Displays the details of a specific unit.



24
# File 'app/controllers/admin/units_controller.rb', line 24

def show; end

#updateObject

PATCH/PUT /units/1 PATCH/PUT /units/1.json Updates an existing unit.



61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/controllers/admin/units_controller.rb', line 61

def update
  respond_to do |format|
    if @unit.update(unit_params.merge(properties: properties_params, updated_by_id: current_user.id))
      @building = Building.find(@unit.building_id)
      format.html { redirect_to admin_building_path(@building), notice: 'Unit was successfully updated.' }
      format.json { render :show, status: :ok, location: @unit }
    else
      format.html { render :edit }
      format.json { render json: @unit.errors, status: :unprocessable_entity }
    end
  end
end