This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
task "assets:precompile" do | |
exec("JEKYLL_ENV=production jekyll build") | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
NewerOlder