Class: Admin::PropertiesController

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

Overview

Controller for managing properties.

Instance Method Summary collapse

Instance Method Details

#archivedObject

GET /properties/archived Retrieves archived properties.



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/controllers/admin/properties_controller.rb', line 34

def archived
  system_id = 2
  is_archived = params[:is_archived]
  inspection_finished = params[:inspection_finished]
  property_name = params[:property_name]

  @properties = Property.where('property_name ILIKE ?', "%#{property_name}%")
                        .where("(properties->>'is_archived' ILIKE ? ) is not null", "%#{is_archived}%")
                        .where("properties->>'inspection_finished' ILIKE ?", "%#{inspection_finished}%")
                        .where('system_Id = ?',system_id)
                        .order(updated_at: :desc)
  render json: @properties
end

#buildingsObject

GET /properties/buildings Retrieves buildings for a property.



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

def buildings
  property_id = params[:property_id]
  @buildings = Building.where(property_id: property_id)
  render json: @buildings
end

#clean_property_profileObject

POST /properties/clean_property_profile Cleans a property's inspection profile.



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'app/controllers/admin/properties_controller.rb', line 215

def clean_property_profile
  respond_to do |format|
    begin
      InspectionDetail.where(property_id: @property.id).delete_all

      buildings = Building.where(property_id: @property.id)
      buildings.each do |building|
        old_building_units = Unit.where(building_id: building.old_id)
        if old_building_units.present?
          old_building_units.each do |old_unit|
            old_unit.building_id = building.id
            old_unit.save
          end
        end
        building["properties"]["uninspectable"] = 'None'
        building["properties"]["comment"] = ''
        building.inspection_date = ''
        building.app_id = nil
        building.inspectable_units = building.units.count
        building.save
      end

      units = Unit.where(property_id: @property.id)
      units.each do |unit|
        unit["properties"]["uninspectable"] = 'None'
        unit["properties"]["comment"] = ''
        unit["occupied"] = true
        unit.app_id = nil
        unit.save
      end

      @property["properties"]["inspection_start_date"] = ''
      @property["properties"]["inspection_end_date"] = ''
      @property["properties"]["inspection_date"] = ''
      @property["properties"]["is_archived"] = 0
      @property["properties"]["inspection_finished"] = 0
      @property["properties"]["comment"] = ''
      @property["inspection_date"] = ''
      @property.inspectable_units = @property.units.count
      @property.total_building = @property.buildings.count
      @property.system_id = 2
      @property.save

      format.html { redirect_to admin_property_path(@property), notice: 'Property Profile Cleared Successfully!' }
      format.json { render :show, status: :ok, location: @property }
    rescue => exception
      format.html { redirect_to admin_properties_url, notice: 'Error Occured while Cleaning Property Profile!' }
      format.json { head :no_content, status: :unprocessable_entity }
    end
  end
end

#createObject

POST /properties POST /properties.json Creates a new property.

Parameters:

  • property_params (Hash)

    The parameters for creating the property.



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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
# File 'app/controllers/admin/properties_controller.rb', line 142

def create

  @property = Property.new(property_params.merge(
    properties: properties_params.tap do |u|
      u[:inspection_finished] = u[:inspection_finished].to_i
      u[:is_archived] = u[:is_archived].to_i
    end,
    created_by_id: current_user.id
  ))
  @property.inspectable_units = 0
  old_property_id = params["property"]["old_property_id"]
  if old_property_id.present?
    old_property = Property.find(old_property_id)
    @property.inspectable_units = Unit.total_inspectable_units(old_property_id)
    @property.property_code = old_property.property_code
  end
  respond_to do |format|
    if @property.save
      if old_property_id.present?
        old_property = Property.find(old_property_id)
        @property.inspectable_units = Unit.total_inspectable_units(old_property_id)
        old_buildings = Building.where(property_id: old_property_id)
        if old_buildings.present?
          old_buildings.each do |old_bld|
            new_bld = Building.new(old_bld.attributes.except("id","property_id", "created_at"))
            new_bld.property_id = @property.id
            new_bld.save
            old_units = Unit.where(property_id: old_property_id).where(building_id: old_bld.id)
            if old_units.present?
              old_units.each do |old_unit|
                new_unit = Unit.new(old_unit.attributes.except("id","property_id","building_id", "created_at"))
                new_unit.property_id = @property.id
                new_unit.building_id = new_bld.id
                new_unit.save
              end
            end
          end
        end
      end
      format.html { redirect_to admin_property_path(@property), notice: 'Property was successfully created.' }
      format.json { render :show, status: :created, location: @property }
    else
      format.html { render :new }
      format.json { render json: @property.errors, status: :unprocessable_entity }
    end
  end
end

#destroyObject

DELETE /properties/1 DELETE /properties/1.json Deletes an existing property.



270
271
272
273
274
275
276
277
# File 'app/controllers/admin/properties_controller.rb', line 270

def destroy
  User.unassign_property(@property.updated_by_id, @property.id)
  @property.destroy
  respond_to do |format|
    format.html { redirect_to admin_properties_url, notice: 'Property was successfully destroyed.' }
    format.json { head :no_content }
  end
end

#duplicate_propertyObject

POST /properties/duplicate_property Duplicates a property along with its buildings and units.



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/controllers/admin/properties_controller.rb', line 119

def duplicate_property
  respond_to do |format|
    begin
      old_property = Property.find(params[:property_id])
      @new_property = old_property.duplicate_with_buildings_and_units
      format.html { redirect_to admin_property_path(@new_property), notice: 'Property Duplicated Successfully!' }
      format.json { render :show, status: :ok, location: @new_property }
    rescue => exception
      format.html { redirect_to admin_properties_url, notice: 'Error Occured while Duplicating the Property!' }
      format.json { head :no_content, status: :unprocessable_entity }
    end
  end
end

#editObject

GET /properties/1/edit Displays a form for editing a property.



135
# File 'app/controllers/admin/properties_controller.rb', line 135

def edit; end

#indexObject

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



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

def index
  respond_to do |format|
    format.html do
      @properties = Property.search(params[:system_id],params[:property_name], params[:inspection_completed],
        params[:is_archived]).page params[:page]

      render action: :index, layout: request.xhr?.nil?
    end
    format.xlsx do
      response.headers[
        'Content-Disposition'
      ] = "attachment; filename='Properties.xlsx'"
    end
  end
end

#newObject

GET /properties/new Displays a form for creating a new property.



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/controllers/admin/properties_controller.rb', line 102

def new
  @property = Property.new
  if params[:property_id]
    old = Property.find(params[:property_id])
    @property.property_name = old.property_name
    @property.address = old.address
    @property.city = old.city
    @property.state = old.state
    @property.zipcode = old.zipcode
    @property.total_building = old.total_building
    @duplicate = true
    @old_property_id = old.id
  end
end

#property_searchObject

GET /properties/property_search Searches for properties by property name.



50
51
52
53
54
# File 'app/controllers/admin/properties_controller.rb', line 50

def property_search
  property_name = params[:property_name]
  @properties = Property.where('property_name ILIKE ?', "%#{property_name}%").order(updated_at: :desc)
  render json: @properties
end

#send_emailObject

POST /properties/send_email Sends an email for property-related actions.

Parameters:

  • params (Hash)

    The email parameters including property_id, email, and date.



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

def send_email
  property_id = params[:property_id]
  email = params[:email]
  date = params[:date]
  UserMailer.with(property_id: property_id, email: email, date: date ).lt_defect_report.deliver_now

  redirect_to admin_properties_path, notice: 'Email sent.'
end

#send_lt_defect_reportObject

GET /properties/send_lt_defect_report Sends the LT defect report for a property.



23
24
25
26
27
28
29
30
# File 'app/controllers/admin/properties_controller.rb', line 23

def send_lt_defect_report
  property_id = params[:property_id]
  if property_id.nil?
    return redirect_to admin_properties_path
  end
  @property = Property.find(property_id)
  @email = @property.properties["client_email"]
end

#showObject

GET /properties/1 GET /properties/1.json Displays the details of a specific property.



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/controllers/admin/properties_controller.rb', line 86

def show
  @buildings = Building.where('property_id = ?', @property.id.to_s)
  respond_to do |format|
    format.xlsx do
      response.headers[
        'Content-Disposition'
      ] = "attachment; filename='Buildings.xlsx'"
    end
    format.html do
      render action: :show, layout: request.xhr?.nil?
    end
  end
end

#updateObject

PATCH/PUT /properties/1 PATCH/PUT /properties/1.json Updates an existing property.

Parameters:

  • property_params (Hash)

    The parameters for updating the property.



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'app/controllers/admin/properties_controller.rb', line 195

def update
  respond_to do |format|
    if @property.update(
      property_params.merge(
        properties: properties_params.tap{ |u| u[:inspection_finished] = u[:inspection_finished].to_i },
        updated_by_id: current_user.id,
        inspectable_units: Unit.total_inspectable_units(@property.id)
      )
    )
      format.html { redirect_to admin_property_path(@property), notice: 'Property was successfully updated.' }
      format.json { render :show, status: :ok, location: @property }
    else
      format.html { render :edit }
      format.json { render json: @property.errors, status: :unprocessable_entity }
    end
  end
end