Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed Oct 13, 2023
1 parent 6cecb57 commit eeb7993
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,15 @@ You can set your custom reporter by defining a class responding to `#report` met
```ruby
class MyReporter
def report(title, created_records)
# do actual reporting
title # => "controller#action"
created_records # => [#<ColumnsTrace::CreatedRecord>]
created_records.each do |record|
record.model # class of ActiveRecord model
record.accessed_fields # array of accessed fields
record.unused_fields # array of unused fields
record.backtrace # array of strings
record.record # ActiveRecord model instance
end
end
end

Expand Down
23 changes: 21 additions & 2 deletions lib/columns_trace/created_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,37 @@
module ColumnsTrace
# Class that is used to store metadata about created ActiveRecord records.
class CreatedRecord
attr_reader :model, :record, :backtrace
# Model class
# @return [Class]
#
attr_reader :model

# Model instance
# @return [ActiveRecord::Base]
#
attr_reader :record

# Backtrace where the instance was created
# @return [Array<String>]
#
attr_reader :backtrace

def initialize(record, backtrace)
@model = record.class
@record = record
@backtrace = backtrace
end

# Get accessed fields on model instance
# @return [Array<String>]
#
def accessed_fields
record.accessed_fields
@accessed_fields ||= record.accessed_fields
end

# Get unused fields on model instance
# @return [Array<String>]
#
def unused_fields
# We need to store this into local variable, because `record.attributes`
# will access all attributes.
Expand Down

0 comments on commit eeb7993

Please sign in to comment.