@@ -28,6 +28,8 @@ class Hotel < CouchbaseOrm::Base
2828 before_destroy :do_something_before_destroy
2929 after_destroy :do_something_after_destroy
3030
31+ before_save :encrypt_address
32+
3133 private
3234
3335 def do_something_before_create
@@ -64,7 +66,7 @@ def do_something_after_destroy
6466
6567 attribute :title , :string
6668 attribute :name , :string
67- attribute :address , :string
69+ attribute :address , :encrypted
6870 attribute :directions , :string
6971 attribute :phone , :string
7072 attribute :tollfree , :string
@@ -99,19 +101,35 @@ def do_something_after_destroy
99101 validates :url , format : { with : URI ::DEFAULT_PARSER . make_regexp , message : 'must be a valid URL' }
100102 validates :title , :name , :address , :city , :state , :country , length : { maximum : 255 }
101103 # validates :title, :name, uniqueness: true
102- validates :price , numericality : { greater_than_or_equal_to : 0 }
104+ # validates :price, numericality: { greater_than_or_equal_to: 0 } # its a string
103105 validates :vacancy , inclusion : { in : [ true , false ] }
104106 validates :public_likes , exclusion : { in : [ nil ] }
105107
106108 validate :custom_validation
107109
110+ n1ql :find_by_name_n1ql , 'SELECT * FROM hotels WHERE name = $1'
111+
108112 def custom_validation
109- errors . add ( :title , 'cannot be funny' )
113+ if title . include? ( 'funny' )
114+ errors . add ( :title , 'cannot be funny' )
115+ end
110116 end
111117
112118 def set_timestamps
113119 current_time = Time . now
114120 self . created_at = current_time if new_record?
115121 self . updated_at = current_time
116122 end
123+
124+ def encrypt_address
125+ self . address = encrypt ( self . address ) if address_changed?
126+ end
127+
128+ def encrypt ( data )
129+ # Implement your encryption logic here
130+ # For example, using a simple XOR encryption
131+ key = "secret_key"
132+ encrypted_data = data . chars . map { |c | ( c . ord ^ key . ord ) . chr } . join
133+ encrypted_data
134+ end
117135end
0 commit comments