Class: Property

Inherits:
ApplicationRecord show all
Includes:
PgSearch::Model
Defined in:
app/models/property.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.archived_completedObject



28
29
30
31
32
33
# File 'app/models/property.rb', line 28

def self.archived_completed
  where("(properties->>'is_archived' = 'Yes') is not null")
    .where("(properties->>'inspection_finished' = '1') is not null")
    .where(status: 'active')
    .order(property_name: :asc)
end

.search(system_id, property_name, inspection_completed, is_archived) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/property.rb', line 35

def self.search(system_id, property_name, inspection_completed,is_archived)

  is_archived = is_archived.nil? ? '0' : is_archived

  result = Property.where(system_id: system_id ? system_id : 2)
  result = result.where('property_name ILIKE ?', "%#{property_name}%") if property_name.present?
  result = result.where("properties->>'is_archived' = '#{is_archived}'") if is_archived.present?
  result = result.where("properties->>'inspection_finished' = '#{inspection_completed}'") if inspection_completed.present?

  result.class == Class ? all : result

end

Instance Method Details

#duplicate_with_buildings_and_unitsObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/models/property.rb', line 65

def duplicate_with_buildings_and_units
  new_property = self.deep_clone include: {buildings: {units: :fixed_assets}}, validate: false
  new_property.save
  
  Unit.where(building_id: new_property.building_ids).update_all(property_id: new_property.id)

  new_property.buildings.each do |building|
    FixedAsset.where(unit_id: building.unit_ids).update_all(property_id: new_property.id, building_id: building.id)
  end

  new_property.property_name = "#{self.property_name} (Duplicated)"
  new_property.created_by_id = 1
  new_property.updated_by_id = 1
  new_property.save
  new_property
end

#get_inspectors_namesObject



82
83
84
# File 'app/models/property.rb', line 82

def get_inspectors_names
  users.select("CONCAT(first_name, ' ', last_name) AS name").map(&:name).reverse.join(', ')
end

#total_buildingsObject



57
58
59
# File 'app/models/property.rb', line 57

def total_buildings
  Building.where(property_id: self.id).count
end

#total_inspectable_buildingsObject



61
62
63
# File 'app/models/property.rb', line 61

def total_inspectable_buildings
  Building.where(property_id: self.id).where("properties->>'uninspectable' = ? OR properties->>'uninspectable' IS NULL", 'None').count
end

#total_inspectable_unitsObject



52
53
54
55
# File 'app/models/property.rb', line 52

def total_inspectable_units
  inspectable_building_ids = Building.where(property_id: self.id).where("properties->>'uninspectable' = ? OR properties->>'uninspectable' IS NULL", 'None').pluck(:id)
  Unit.where(property_id: self.id).where("properties->>'uninspectable' = ? OR properties->>'uninspectable' IS NULL", 'None').where(building_id: inspectable_building_ids).count
end

#total_unitsObject



48
49
50
# File 'app/models/property.rb', line 48

def total_units
  Unit.where(property_id: self.id).count
end