This is my current Rails file structure:
hello-app
app
assets
controller
helpers
models
application_record.rb
short_link.rb
obfuscate.rb
PROBLEM: The obfuscate.rb file is located next to the short_link.rb file where it doesn't belong.
GOAL: I want to move the obfuscate.rb file to a different location, see new structure below:
hello-app
app
assets
controller
helpers
models
application_record.rb
short_link.rb
services
models
obfuscate.rb
And import the obfuscate.rb module into the short_link.rb model.
Here is my current short_link.rb model file:
require 'obfuscate'
class ShortLink < ApplicationRecord
include Obfuscate
def to_param
encrypt(id)
end
end
And my obfuscate.rb file:
require 'openssl'
require 'base64'
module Obfuscate
def self.included(base)
base.extend self
end
def cipher
OpenSSL::Cipher::Cipher.new('aes-256-cbc')
end
def cipher_key
'custom_cipher_key'
end
def decrypt(value)
c = cipher.decrypt
c.key = Digest::SHA256.digest(cipher_key)
c.update(Base64.urlsafe_decode64(value.to_s)) + c.final
end
def encrypt(value)
c = cipher.encrypt
c.key = Digest::SHA256.digest(cipher_key)
Base64.urlsafe_encode64(c.update(value.to_s) + c.final)
end
end
Also, is it a good practice to move the obfuscate.rb file to hello-app>>app>>services>>models>>obfuscate.rb? Or would there be a better location for this extra logic?