1- # frozen_string_literal: true
2-
3- # app/models/hotel.rb
41class Hotel < CouchbaseOrm ::Base
52 attribute :title , :string
63 attribute :name , :string
@@ -31,7 +28,17 @@ class Hotel < CouchbaseOrm::Base
3128 attribute :created_at , :datetime , precision : 6
3229 attribute :updated_at , :datetime , precision : 6
3330
31+ validates :name , presence : true , uniqueness : true
32+ validates :address , presence : true
33+ validates :phone , presence : true
34+ validates :type , inclusion : { in : [ 'hotel' , 'motel' , 'resort' ] }
35+ validates :url , format : { with : URI . regexp , message : 'must be a valid URL' }
36+ validates :description , length : { maximum : 500 }
37+
38+ before_create :set_alias
3439 before_save :set_timestamps
40+ after_create :send_welcome_email
41+ before_destroy :check_reviews
3542
3643 private
3744
@@ -40,19 +47,15 @@ def set_timestamps
4047 self . created_at = current_time if new_record?
4148 self . updated_at = current_time
4249 end
43- end
4450
45- # app/models/geo_coordinates.rb
46- class GeoCoordinates < CouchbaseOrm ::NestedDocument
47- attribute :lat , :float
48- attribute :lon , :float
49- attribute :accuracy , :string
50- end
51+ def send_welcome_email
52+ # Code to send welcome email after hotel creation
53+ end
5154
52- # app/models/review.rb
53- class Review < CouchbaseOrm :: NestedDocument
54- attribute :content , :string
55- attribute :ratings , :hash
56- attribute :author , :string
57- attribute :date , :datetime
55+ def check_reviews
56+ if reviews . any?
57+ errors . add ( :base , 'Cannot delete hotel with existing reviews' )
58+ throw :abort
59+ end
60+ end
5861end
0 commit comments