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.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/controllers/admin/buildings_controller.rb', line 73

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
      ApplicationHelper.update_buildings_details_in_property(@property.id)
      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.



116
117
118
119
120
121
122
123
124
# File 'app/controllers/admin/buildings_controller.rb', line 116

def destroy
  property_id = @building.property_id
  @building.destroy
  ApplicationHelper.update_buildings_units_details_in_property(property_id)
  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.



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

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
30
31
32
33
# File 'app/controllers/admin/buildings_controller.rb', line 26

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

#newObject

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



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

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.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/admin/buildings_controller.rb', line 40

def show
  @property = @building.property
  units = @building.units.to_a
  @units = ApplicationHelper.custom_sort(units, :unit_name)
  respond_to do |format|
    format.xlsx do
      response.headers[
        'Content-Disposition'
      ] = "attachment; filename='Units.xlsx'"
    end
    format.html do
      render action: :show
    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.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/controllers/admin/buildings_controller.rb', line 95

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