Class: ApplicationRecord

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/application_record.rb

Overview

Base class for all models in the application.

This class provides methods for defining decrypted attributes.

Direct Known Subclasses

AppOwner, BankAccount, Brand, BrandCategory, Chat, ChatMessage, City, Color, CommentsFiles, Company, CompanyAgreement, CompanyAssignedUser, CompanyIntegration, CompanyParameter, Contact, Country, Currency, Customer, CustomerByCompany, CustomersByCompanyView, Edi204, Edi204At8, Edi204B2, Edi204B2a, Edi204G62, Edi204Isa, Edi204L11, Edi204L5, Edi204N1NameAddress, Edi204N7, Edi204Nte, Edi204Oid, Edi204S5, Edi214StatusMessage, Edi850Header, Edi990LoadTenderResponses, EdiDocument, EdiInvoice, EdiInvoiceLine, EdiInvoiceName, EdiOutLog, EdiPartner, EdiPartner::View, EdiPaymentTermsTranslation, EdiShippingCompanyTranslation, EdiSkuTranslation, EdiTransactionQualifier, EdiUomTranslation, Employee, EmployeePayrollCalculation, EmployeePayrollTransaction, EmployeeRole, EmployeeTimesheet, EmployeeTimesheet::View, ErpUser, EventNotificationConfiguration, EventNotificationDelivery, FreightTerm, Group, Integration, IntegrationReference, Invoice, Marketplace, NavigationConfiguration, NavigationGroup, NavigationHeader, NavigationItem, NavigationRecordPermission, NavigationRecordPermissionsRecord, Parameter, PartnerTransaction, PaymentTerm, PaymentTerms, PayrollCalculationTemplate, PayrollDeductionRate, PayrollFederalProvincialDeduction, PayrollFederalProvincialDeduction::View, PayrollFee, PayrollHoliday, PayrollInvoiceDetails, PayrollInvoiceHeader, PayrollInvoiceHeaderView, PayrollParameter, PayrollPeriod, PayrollReconciliation, PayrollTransactionDetail, PayrollTransactionHeader, PayrollTransactionHeader::View, PayrollTransactionView, PayrollVirtualWalletTransaction, PayrollVirtualWalletTransaction::View, Permission, PermissionGroup, PermissionGroupDetails, ProductCategory, ProductFamily, ProductMaster, Province, PurchaseOrder, PurchaseOrderDetails, QuickbooksInvoiceDetail, QuickbooksInvoiceHeader, RateLimitRecord, ReferenceEvent, ReferenceEventConfiguration, Report, RessourceRequiredPermission, RessourceTag, RevokedToken, Royalty, SaleOrderDetail, SaleOrderHeader, Season, SegmentField, ShippingCompany, SkuGroup, SkuMaster, SkuMasterView, SkuSalePrice, SkuSubGroup, SkuVariant, SpecialParameter, SubGroup, Supplier, SupplierByCompany, SupplierByCompany::View, Tag, Tenant, TransactionSegment, TypesMaster, UnitOfMeasurement, UserLoginActivity, UserPermissionGroup, Warehouse

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.define_decrypted_attribute(attribute_name, prefix = 'decrypted') ⇒ void

This method returns an undefined value.

Defines a decrypted attribute for the model.

Examples:

define_decrypted_attribute(:ssn)

Parameters:

  • attribute_name (String, Symbol)

    the name of the attribute to decrypt

  • prefix (String) (defaults to: 'decrypted')

    the prefix for the decrypted attribute method (default: ‘decrypted’)



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/models/application_record.rb', line 18

def self.define_decrypted_attribute(attribute_name, prefix = 'decrypted')
  define_method("#{prefix}_#{attribute_name}") do
    instance_variable_name = "@#{prefix}_#{attribute_name}"
    return instance_variable_get(instance_variable_name) if instance_variable_defined?(instance_variable_name)

    if changes.key?(attribute_name)
      instance_variable_set(instance_variable_name, attributes[attribute_name])
      return attributes[attribute_name]
    end

    sql = <<-SQL
      OPEN SYMMETRIC KEY PaieHubSymmetricKey DECRYPTION BY CERTIFICATE PaieHubCertificate;
      SELECT CONVERT(NVARCHAR(300), DecryptByKey(#{self.class.table_name}.#{attribute_name})) AS decrypted_name
      FROM #{self.class.table_name}
      WHERE #{self.class.primary_key} = ?;
      CLOSE SYMMETRIC KEY PaieHubSymmetricKey;
    SQL

    sanitized_query = ActiveRecord::Base.send(:sanitize_sql_array, [sql, id])
    result = ActiveRecord::Base.connection.exec_query(sanitized_query).to_a
    decrypted_value = result.dig(0, 'decrypted_name')

    encrypted_value_unencryptable = decrypted_value.nil? && (encrypted_value = attributes&.dig(attribute_name)).present?
    if encrypted_value_unencryptable
      instance_variable_set(instance_variable_name, encrypted_value)
      encrypted_value
    else
      instance_variable_set(instance_variable_name, decrypted_value)
      decrypted_value
    end
  end

  define_method(attribute_name) do
    # disable decryption in test environment
    # if Rails.env.test?
    #   super()
    # else
    # send("#{prefix}_#{attribute_name}")
    # end
    send("#{prefix}_#{attribute_name}")
  rescue StandardError => e
    raise e unless Rails.env.test?

    super()
  end
end

.define_decrypted_attributes(encrypted_columns) ⇒ string

Defines decrypted attributes for the model.

Examples:

define_decrypted_attributes(%w[ssn phone_no])

Parameters:

  • encrypted_columns (Array<String, Symbol>)

    the names of the attributes to decrypt

Returns:

  • (string)


71
72
73
74
75
# File 'app/models/application_record.rb', line 71

def self.define_decrypted_attributes(encrypted_columns)
  encrypted_columns.each do |field|
    define_decrypted_attribute(field)
  end
end

.primary_connectionObject



88
89
90
# File 'app/models/application_record.rb', line 88

def primary_connection
  Parameter.connection # Any primary db table would work
end

.special_connectionObject



84
85
86
# File 'app/models/application_record.rb', line 84

def special_connection
  SpecialParameter.connection # any special db table would work
end

Instance Method Details

#errors_in_bullet_pointsObject



77
78
79
80
81
# File 'app/models/application_record.rb', line 77

def errors_in_bullet_points
  errors.map do |error|
    "#{error.attribute} #{error.message}"
  end.join("\n")
end