Initial commit
This commit is contained in:
0
app/models/concerns/.keep
Normal file
0
app/models/concerns/.keep
Normal file
21
app/models/concerns/data_is_json_validator.rb
Normal file
21
app/models/concerns/data_is_json_validator.rb
Normal 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
|
||||
25
app/models/concerns/encryptable.rb
Normal file
25
app/models/concerns/encryptable.rb
Normal 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
|
||||
11
app/models/concerns/iso8601_time_format.rb
Normal file
11
app/models/concerns/iso8601_time_format.rb
Normal 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
|
||||
23
app/models/concerns/model_caching.rb
Normal file
23
app/models/concerns/model_caching.rb
Normal 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
|
||||
13
app/models/concerns/trusty_agent_validator.rb
Normal file
13
app/models/concerns/trusty_agent_validator.rb
Normal 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
|
||||
Reference in New Issue
Block a user