Module: Models::Auditable

Extended by:
ActiveSupport::Concern
Included in:
Account, Activity, ActivityAgenda, ActivityChainType, ActivityResultType, ActivityType, ActivityTypeAssignmentQueue, ActivityTypeRule, AdditionalCallCredit, Address, Agreement, AgreementParticipant, AmazonAPlusContent, AmazonSchema, AmazonTransparencyCode, Article, ArticlePage, ArticleRevision, AssignmentQueue, AssortmentInstruction, Audience, AudienceMember, Authentication, AverageMonthlyTemperature, Bank, BankAccount, BankBalanceStatement, Budget, BudgetGroup, BudgetRule, BusinessUnit, BuyingGroup, CallBlock, CallRecord, Campaign, CampaignAction, CarrierPickup, Catalog, CatalogItem, Ceiling, CeilingInsulationType, CeilingType, Certification, Charity, Check, CommissionRate, CommissionStructure, CommissionStructureEntry, CommissionTier, Communication, CommunicationRecipient, Company, ContactPoint, Coupon, CouponSerialNumber, Course, CourseCategory, CourseEnrollment, CreditApplication, CreditCardVault, CreditMemo, CustomerDropEvent, CustomerFilter, CustomerOutletPurchase, CustomerRecord, CycleCount, DataDictionarySet, Delivery, DesignToolFixture, DigitalAsset, Discount, DoNotCall, DoorCoreType, DoorFrameType, DoorInsulationType, DoorType, EdiCommunicationLog, ElementPoleAssignment, EmailPreference, EmailTemplate, EmployeeEvent, EmployeeGoal, EmployeePhoneStatus, EmployeeRecord, EmployeeReview, ExchangeRate, ExportedCatalogItemPacket, Feed, FloorPlanDisplay, FraudReport, HeatingElementProductLineOption, IdentificationNumber, ImageProfile, InventoryCommit, Invoice, Item, ItemDemandForecastAddition, ItemProductLine, ItemRelation, Kpi, LandedCost, LedgerAccount, LedgerBeginningBalance, LedgerClosingPeriod, LedgerCompanyAccount, LedgerDetailAccount, LedgerDetailProject, LedgerEntry, LedgerProject, LedgerTransaction, LiabilityInsurance, LineItem, LiveEvent, LocatorBlackListParty, LocatorRecord, LocatorWhiteListParty, MailActivity, Mailing, Manifest, NotificationChannel, OauthCredential, Opportunity, Order, OrderTransaction, OutgoingPayment, OutgoingPaymentItem, Packaging, Packing, Party, PartyTopic, Payment, PhoneQueueRotation, PostComment, Praise, PriceThreshold, ProductCategory, ProductFilter, ProductLine, ProductSpecification, Profile, PublicationItem, PurchaseOrder, PurchaseOrderItem, PurchaseOrderShipment, QuickEstimator, Quote, Receipt, ReceiptDetail, Rma, RmaItem, RoomConfiguration, SalesCommission, SalesCommissionNetBase, SalesCommissionNetBaseDetail, SalesForecast, SalesGoal, SalesRepQueue, SalesRepQueueEntry, SalesRepWeight, SerialNumber, ServiceJob, Setting, Shipment, ShipmentItem, ShipmentReceipt, ShipmentReceiptItem, ShippingAccountNumber, Showcase, Skylight, SmsMessage, Source, Spiff, SpiffEnrollment, SpiffReward, SpiffRewardThreshold, SqlRepo, StandaloneDelivery, StatementOfAccount, Store, StoreItem, StoreTransfer, Supplier, SupplierItem, SupplierItemPrice, SupportCase, SupportCaseParticipant, Survey, TaxExemption, TaxRate, TimeOffBalance, TimeOffPolicy, TimeOffPolicyAssignment, TimeOffRequest, TimeOffRequestDate, TimeOffType, Topic, TopicCategory, TopicResponse, TradeShow, UnderFloor, Upload, VariableCost, VariantGroup, Voucher, VoucherItem, WarehousePackage, Warranty, WayfairSchema, Window, WorkSchedule, WorkScheduleDay, XrateAverage, Zone
Defined in:
app/concerns/models/auditable.rb

Overview

PaperTrail audit-trail wiring shared by audited models: versions are stored
as RecordVersion rows (create/update/destroy), creator / updater are
stamped from CurrentScope, and models can skip columns or attach stable
reference metadata via the class-level DSL.

See Also:

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

ALWAYS_IGNORED =

Always ignored.

%i[updated_at created_at creator_id updater_id search_text_tsvector].freeze

Belongs to collapse

Instance Method Summary collapse

Instance Method Details

#all_skipped_columnsArray<Symbol>

Every column excluded from versioning: the model's declared
skipped_columns plus ALWAYS_IGNORED.

Returns:

  • (Array<Symbol>)


89
90
91
# File 'app/concerns/models/auditable.rb', line 89

def all_skipped_columns
  (self.class.skipped_columns || []) + ALWAYS_IGNORED
end

#audit_reference_dataHash?

Builds the reference_data metadata hash stored on each version: the
model's declared #audit_reference_data fields merged with request-level
context (e.g. Sunny conversation metadata) set via
PaperTrail.request.controller_info in background workers.

Returns:

  • (Hash, nil)

    merged metadata, or nil when empty



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/concerns/models/auditable.rb', line 107

def audit_reference_data
  model_data = if self.class.reference_data.present?
                 self.class.reference_data.index_with { |e| try(e) }.compact
               else
                 {}
               end

  # Merge request-level context (e.g. Sunny conversation metadata)
  # set via PaperTrail.request.controller_info in background workers.
  request_ctx = PaperTrail.request.controller_info || {}
  merged = model_data.merge(request_ctx)
  merged.presence
end

#creatorParty?

Returns the party that created the record.

Returns:

  • (Party, nil)

    the party that created the record



27
# File 'app/concerns/models/auditable.rb', line 27

belongs_to :creator, class_name: 'Party', optional: true

#should_not_save_versionBoolean

PaperTrail :unless guard — suppresses a version when PaperTrail is
disabled and every changed column is in #all_skipped_columns.

Returns:

  • (Boolean)


97
98
99
# File 'app/concerns/models/auditable.rb', line 97

def should_not_save_version
  !PaperTrail.enabled? && changes.keys.all? { |k| k.to_sym.in?(all_skipped_columns) }
end

#stamp_recordvoid

This method returns an undefined value.

before_save callback — stamps creator_id / updater_id / visit_id
from CurrentScope when the record has those columns.



77
78
79
80
81
82
83
# File 'app/concerns/models/auditable.rb', line 77

def stamp_record
  user_id = CurrentScope.user_id
  visit_id = CurrentScope.visit_id
  self.creator_id ||= user_id if respond_to? :creator_id=
  self.updater_id = user_id if respond_to? :updater_id=
  self.visit_id ||= visit_id if respond_to? :visit_id=
end

#updaterParty?

Returns the party that last updated the record.

Returns:

  • (Party, nil)

    the party that last updated the record



29
# File 'app/concerns/models/auditable.rb', line 29

belongs_to :updater, class_name: 'Party', optional: true