Module: AuditTracker
- Defined in:
- lib/audit_tracker.rb,
lib/audit_tracker/dsl.rb,
lib/audit_tracker/railtie.rb,
lib/audit_tracker/tracker.rb,
lib/audit_tracker/version.rb,
lib/audit_tracker/model_helper.rb
Overview
AuditTracker helps you centralize data tracking configuration to be used accross different models
Defined Under Namespace
Modules: DSL, ModelHelper Classes: Railtie, Tracker
Constant Summary collapse
- VERSION =
"0.1.0"
Class Method Summary collapse
-
.apply(model, options) ⇒ Object
Enables the given trackers in the given model.
-
.setup(&block) ⇒ Object
Setup ‘AuditTracker::Tracker`’s.
-
.trackers ⇒ Hash<Symbol,::AuditTracker::Tracker>
Trackers setup in AuditTracker.
Class Method Details
.apply(model, options) ⇒ Object
Enables the given trackers in the given model
I.e.:
The following would create two trackers (user and user_department), but only apply the
former to Lead:
AuditTracker.setup do
tracker(:user) do
update :updated_by, foreign_key: "updated_by_id", class_name: "::User"
value { User.current }
end
tracker(:user_department) do
update :updated_by_department, foreign_key: "updated_by_department_id", class_name: "::Department"
value { User.current&.department }
end
end
AuditTracker.apply(::Lead, user: true)
85 86 87 88 89 90 91 |
# File 'lib/audit_tracker.rb', line 85 def self.apply(model, ) @trackers.each do |key, tracker| next unless [key] tracker.apply(model, [key] == true ? {} : [key]) end end |
.setup(&block) ⇒ Object
Setup ‘AuditTracker::Tracker`’s
setup entry point for data trackers. Multiple calls to this method are cumulative, and trackers with the same key override each other depending on load order.
I.e.:
AuditTracker.setup do
tracker :user do
value { ::Internal::Current.user }
create :created_by, foreign_key: :created_by_id, class_name: "::Internal::User"
update :updated_by, foreign_key: :updated_by_id, class_name: "::Internal::User"
end
tracker :user_department do
value { ::Internal::Current.user&.department }
create(
:created_by_department,
foreign_key: :created_by_department_id,
class_name: "::Internal::Department"
)
update(
:updated_by_department,
foreign_key: :updated_by_department_id,
class_name: "::Internal::Department"
)
end
end
Trackers will track a specific value, so the ‘value“ is always required to be defined. Each line after that define a different event. `create` and `update` are helper methods to create events.
‘update` is tied to the `:save` event of activerecord. That means that the value will be tracked before create and before update.
Each event defined will generate an active record relation, and will update that relation before the event (i.e.: ‘before_update`, `before_create`).
58 59 60 |
# File 'lib/audit_tracker.rb', line 58 def self.setup(&block) ::AuditTracker::DSL.build(&block) end |
.trackers ⇒ Hash<Symbol,::AuditTracker::Tracker>
Trackers setup in AuditTracker
16 17 18 |
# File 'lib/audit_tracker.rb', line 16 def self.trackers @trackers ||= {} end |