Class: Admin::BuildingsController

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

Overview

Controller for managing buildings.

Instance Method Summary collapse

Instance Method Details

#createHTML/JSON

POST /buildings POST /buildings.json Creates a new building based on the provided parameters.

Parameters:

  • building_params (Hash)

    The parameters for creating the building.

Returns:

  • (HTML/JSON)

    Redirects to the property page if successful or shows errors.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/controllers/admin/buildings_controller.rb', line 68

def create
  params = building_params.merge(properties: properties_params, created_by_id: current_user.id)
  @building = Building.new(params)
  respond_to do |format|
    @property = Property.find_by_id(params[:property_id])
    if @building.save
      format.html { redirect_to admin_property_path(@property), notice: 'Building was successfully created.' }
      format.json { render :show, status: :created, location: @building }
    else
      format.html { render :new }
      format.json { render json: @building.errors, status: :unprocessable_entity }
    end
  end
end

#destroyHTML/JSON

DELETE /buildings/1 DELETE /buildings/1.json Deletes an existing building.

Returns:

  • (HTML/JSON)

    Redirects to the property page after deleting the building.



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

def destroy
  property_id = @building.property_id
  @building.destroy
  respond_to do |format|
    format.html { redirect_to admin_property_url(Property.find(property_id)), notice: 'Building was successfully destroyed.' }
    format.json { head :no_content }
  end
end

#editObject

GET /buildings/1/edit Displays a form for editing a building.



60
# File 'app/controllers/admin/buildings_controller.rb', line 60

def edit; end

#indexObject

GET /buildings GET /buildings.json Displays a list of buildings based on search criteria.

Parameters:

  • building_name (String)

    The name of the building to search for.

  • property_id (Integer)

    The ID of the property to filter by.



26
27
28
29
# File 'app/controllers/admin/buildings_controller.rb', line 26

def index
  @buildings = Building.search(params[:building_name], params[:property_id]).page params[:page]
  render action: :index, layout: request.xhr?.nil?
end

#newObject

GET /buildings/new Displays a form for creating a new building.



53
54
55
56
# File 'app/controllers/admin/buildings_controller.rb', line 53

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

#showHTML

GET /buildings/1 GET /buildings/1.json Displays the details of a specific building.

Returns:

  • (HTML)

    The HTML view of the building details.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/admin/buildings_controller.rb', line 36

def show
  @property = @building.property
  @units = @building.units
  respond_to do |format|
    format.xlsx do
      response.headers[
        'Content-Disposition'
      ] = "attachment; filename='Units.xlsx'"
    end
    format.html do
      render action: :show, layout: request.xhr?.nil?
    end
  end
end

#unitsJSON

GET /buildings/units GET /buildings/units.json Returns the units of a building by its ID in JSON format.

Parameters:

  • building_id (Integer)

    The ID of the building.

Returns:

  • (JSON)

    The units of the building in JSON format.



14
15
16
17
18
# File 'app/controllers/admin/buildings_controller.rb', line 14

def units
  building_id = params[:building_id]
  @units = Building.find_by_old_id(building_id).units
  render json: @units
end

#updateHTML/JSON

PATCH/PUT /buildings/1 PATCH/PUT /buildings/1.json Updates an existing building based on the provided parameters.

Parameters:

  • building_params (Hash)

    The parameters for updating the building.

Returns:

  • (HTML/JSON)

    Redirects to the property page if successful or shows errors.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/controllers/admin/buildings_controller.rb', line 89

def update
  respond_to do |format|
    # return render json: building_params[:construction_date]
    params = building_params.merge(properties: properties_params, updated_by_id: current_user.id)
    @property = Property.find_by_id(@building.property_id)

    if @building.update(params)
      format.html { redirect_to admin_property_path(@property), notice: 'Building was successfully updated.' }
      format.json { render :show, status: :ok, location: @building }
    else
      format.html { render :edit }
      format.json { render json: @building.errors, status: :unprocessable_entity }
    end
  end
end