Skip to content

Instantly share code, notes, and snippets.

View kusumandaru's full-sized avatar

Angga Kusumandaru kusumandaru

View GitHub Profile
require 'benchmark'
RSpec.describe User, type: :model do
context '#benchmark create' do
let(:users) { FactoryBot.create_list(:user, 5_000) }
it 'return benchmark' do
benchmark = Benchmark.measure {
users
}
@kusumandaru
kusumandaru / Rakefile
Created January 1, 2021 12:01
heroku jekyll
task "assets:precompile" do
exec("JEKYLL_ENV=production jekyll build")
end
class MigratesersPgcrypto < ActiveRecord::Migration[6.0]
def up
# activate extension on postgres
enable_extension 'pgcrypto'
rename_column :users, :email, :email_decrypted
add_column :users, :email, :binary
encrypt_data_user
remove_column :users, :email_decrypted, :string
end
@kusumandaru
kusumandaru / pgcryptoable_example.rb
Created May 16, 2020 08:22
PgCryptoable Example
RSpec.shared_examples_for 'pgcryptoable model' do
it { is_expected.to be_kind_of(PgCryptoable) }
end
RSpec::Matchers.define :have_pgcryptoable_attribute do |expected|
match do |actual|
dec_attribute = "decrypted_#{expected}"
actual.class.respond_to?(dec_attribute.to_sym)
end
@kusumandaru
kusumandaru / pg_cryptoable.rb
Last active May 16, 2020 04:27
Pg Cryptoable concern
module PgCryptoable
extend ActiveSupport::Concern
class_methods do
def attr_pgcrypto(*attributes)
attributes.each do |attribute|
define_singleton_method("decrypted_#{attribute}".to_sym) do
ActiveRecord::PGCrypto::SymmetricCoder
.decrypted_arel_text(arel_table[attribute])
end
class EncryptionService
ENCRYPT_KEY_BASE = ENV['ENCRYPT_KEY_BASE']
KEY_LEN = ActiveSupport::MessageEncryptor.key_len.freeze
delegate :encrypt_and_sign, :decrypt_and_verify, to: :encryptor
def initialize(salt, value)
@value = value
key = ActiveSupport::KeyGenerator.new(ENCRYPT_KEY_BASE).generate_key(salt, KEY_LEN)
@kusumandaru
kusumandaru / encryptable.rb
Created May 2, 2020 14:22
Encrypt concern
# adding attribute defined by attr_encrypted parameters
# column "encrypted_" attribute name must be created on db
# method attributed will be created as decrypted from column "encrypted_" attribute name
# example attr_encrypted :password will save encrypted password on column encrypted_password
# also create salt if not exist
module Encryptable
extend ActiveSupport::Concern
included do
def build_salt
self.salt = SecureRandom.random_bytes(
class AddSaltAndEncryptedAttributeToUsers < ActiveRecord::Migration[6.0]
def change
# example column is mother_name
add_column :users, :salt, :binary
add_column :users, :encrypted_mother_name, :string
encrypt_password
remove_column :user, :mother_name, :string
end
module Bigquery
class Base
require 'google/cloud/bigquery'
include ActiveModel::Validations
def initialize
Google::Cloud::Bigquery.configure do |config|
config.project_id = ENV['PROJECT_ID']
config.credentials = ENV['BIGQUERY_CREDENTIAL_PATH']
end
module Bigquery
class UserStreamJob < ApplicationJob
queue_as :default
def perform(user_id)
user = User.find_by(id: user_id)
return if user.blank?
service = Bigquery::UserService.new
service.insert_rows(user)