Initial commit

This commit is contained in:
Yaser
2026-08-13 19:56:46 +03:30
commit de1d57a67b
474 changed files with 43185 additions and 0 deletions

View File

View File

@@ -0,0 +1,21 @@
# frozen_string_literal: true
# Data json validation
class DataIsJsonValidator < ActiveModel::EachValidator
def validate_each(record, attribute, data)
return if data.nil?
unless validate_data_is_json!(data)
record.errors.add(attribute, :invalid_format, message: 'data is not json compatible string')
end
end
def validate_data_is_json!(data)
begin
JSON.parse(data)
true
rescue JSON::ParserError => e
false
end
end
end

View File

@@ -0,0 +1,25 @@
# frozen_string_literal: true
module Encryptable
extend ActiveSupport::Concern
class_methods do
def attr_encrypted(*attributes)
attributes.each do |attribute|
define_method("#{attribute}=".to_sym) do |value|
return if value.nil?
self.public_send(
"#{attribute}_encrypted=".to_sym,
EncryptionService.encrypt(value)
)
end
define_method(attribute) do
value = self.public_send("#{attribute}_encrypted".to_sym)
EncryptionService.decrypt(value) if value.present?
end
end
end
end
end

View File

@@ -0,0 +1,11 @@
# frozen_string_literal: true
# Convert time to utc 8601
module Iso8601TimeFormat
extend ActiveSupport::Concern
def format_iso8601_time(time)
utc_time = time.respond_to?(:utc) ? time.utc : time
utc_time&.iso8601
end
end

View File

@@ -0,0 +1,23 @@
module ModelCaching
extend ActiveSupport::Concern
def write_cache(action, value, expiration_time)
Rails.cache.write(cache_key_generator(action), value, expires_in: expiration_time.seconds)
end
def read_cache(action)
Rails.cache.read(cache_key_generator(action))
end
def delete_cache(action)
Rails.cache.delete(cache_key_generator(action))
end
private
def cache_key_generator(action)
class_name = self.class.name.to_s
object_id = id.to_s
"#{class_name}_#{object_id}_#{action}"
end
end

View File

@@ -0,0 +1,13 @@
# frozen_string_literal: true
# User agent validator
class TrustyAgentValidator < ActiveModel::EachValidator
def validate_each(record, _attribute, value)
browser = Browser.new(value)
return if browser.known?
return record.data = { note: 'Detected suspicious browser' }.to_json if record.data.nil?
record.data = JSON.parse(record.data).merge(note: 'Detected suspicious browser').to_json
end
end