Class: Stay::Property

Inherits:
ApplicationRecord show all
Extended by:
FriendlyId
Includes:
ControllerHelpers::Currency, ControllerHelpers::Store, CurrencyHelper
Defined in:
app/models/stay/property.rb

Constant Summary collapse

STATUSES =
%w[active inactive].freeze
ACTIVE_STATUS =
"active".freeze
APPROVED =
"approved".freeze
PROPERTY =
"property".freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ControllerHelpers::Store

#current_store, #ensure_current_store, #store_locale

Methods included from ControllerHelpers::Currency

#currency_param, #current_currency, #supported_currencies, #supported_currencies_for_all_stores, #supported_currency?

Methods included from CurrencyHelper

#currency_options, #currency_presentation, #currency_symbol, #should_render_currency_dropdown?, #supported_currency_options

Class Method Details

.ransackable_associations(auth_object = nil) ⇒ Object



148
149
150
# File 'app/models/stay/property.rb', line 148

def self.ransackable_associations(auth_object = nil)
  %w[rooms amenities property_type]
end

.ransackable_attributes(auth_object = nil) ⇒ Object



144
145
146
# File 'app/models/stay/property.rb', line 144

def self.ransackable_attributes(auth_object = nil)
  %w[active address availability_end availability_start title extra_guest total_rooms total_bathrooms latitude longitude total_bedrooms guest_number]
end

.ransackable_scopes(auth_object = nil) ⇒ Object



140
141
142
# File 'app/models/stay/property.rb', line 140

def self.ransackable_scopes(auth_object = nil)
  %i[with_amenities nearby by_property_type]
end

Instance Method Details

#approvedObject



194
195
196
197
198
# File 'app/models/stay/property.rb', line 194

def approved
  transaction do
    approve!
  end
end

#average_ratingObject



164
165
166
167
168
169
170
171
172
# File 'app/models/stay/property.rb', line 164

def average_rating
  all_reviews = Stay::Review.joins(booking: { rooms: :property }).where(stay_properties: { id: self.id })

  return 0 if all_reviews.empty?
  total_rating = all_reviews.sum(:rating)
  review_count = all_reviews.count

  (total_rating.to_f / review_count).round(2)
end

#combine_addressObject



118
119
120
# File 'app/models/stay/property.rb', line 118

def combine_address
  [ address, city, state, country ].compact.join(" ")
end

#cover_image_urlObject



190
191
192
# File 'app/models/stay/property.rb', line 190

def cover_image_url
  cover_image.attached? ? cover_image.url : nil
end

#default_roomObject



178
179
180
# File 'app/models/stay/property.rb', line 178

def default_room
  master
end

#deleted?Boolean

Returns:

  • (Boolean)


160
161
162
# File 'app/models/stay/property.rb', line 160

def deleted?
  !!deleted_at
end

#has_rooms?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'app/models/stay/property.rb', line 156

def has_rooms?
  rooms.any?
end

#id_with_titleObject



99
100
101
102
103
104
# File 'app/models/stay/property.rb', line 99

def id_with_title
  [
    truncated_title,
    [ truncated_title, truncated_description ]
  ]
end

#place_images_urlsObject



186
187
188
# File 'app/models/stay/property.rb', line 186

def place_images_urls
  place_images.map { |image| image.url }
end

#priceObject



182
183
184
# File 'app/models/stay/property.rb', line 182

def price
  master&.price_per_month.to_f || 0
end

#rejectedObject



200
201
202
203
204
# File 'app/models/stay/property.rb', line 200

def rejected
  transaction do
    reject!
  end
end

#resubmitedObject



206
207
208
209
210
# File 'app/models/stay/property.rb', line 206

def resubmited
  transaction do
    resubmit!
  end
end

#reviews_countObject



174
175
176
# File 'app/models/stay/property.rb', line 174

def reviews_count
  Stay::Review.joins(booking: { rooms: :property }).where(stay_properties: { id: self.id }).count
end

#shared_propertyObject



152
153
154
# File 'app/models/stay/property.rb', line 152

def shared_property
  property_type.present? ? property_type.name.casecmp?("shared property") : false
end

#should_generate_new_friendly_id?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'app/models/stay/property.rb', line 114

def should_generate_new_friendly_id?
  title_changed?
end

#truncated_descriptionObject



110
111
112
# File 'app/models/stay/property.rb', line 110

def truncated_description
  description.split[0..5].join(" ")
end

#truncated_titleObject



106
107
108
# File 'app/models/stay/property.rb', line 106

def truncated_title
  title.split[0..9].join(" ")
end