class PaperTrailScrapbook::UserJournal

Class UserJournal provides history for a user, optionally scoped by object class and/or date range

@author Jason Dinsmore <jason@hint.io>

Attributes

ends[R]
options[R]
starts[R]
user[R]
user_id[R]
versions[R]

Public Class Methods

new(user, opts = {}) click to toggle source
# File lib/paper_trail_scrapbook/user_journal.rb, line 12
def initialize(user, opts = {})
  @user     = user
  @user_id  = String(user.id)
  @options  = opts
  @starts   = options[:start] || Time.at(0).in_time_zone
  @ends     = options[:end] || Time.current.in_time_zone

  @versions = PaperTrail::Version.where(query_params)
end

Public Instance Methods

story() click to toggle source

Retries textual historical analysis of versions

@return [String] analyzed versions

# File lib/paper_trail_scrapbook/user_journal.rb, line 26
def story
  s = versions.map do |v|
    JournalEntry.new(v).story
  end.compact

  s.reverse! if PaperTrailScrapbook.config.recent_first

  s.join("\n\n")

  "#{preface}#{s.presence || 'No history'}"
end

Private Instance Methods

preface() click to toggle source
# File lib/paper_trail_scrapbook/user_journal.rb, line 40
def preface
  "Between #{when_range}, #{user} made the following #{what} changes:\n\n"
    .squeeze(' ')
end
query_params() click to toggle source
# File lib/paper_trail_scrapbook/user_journal.rb, line 45
def query_params
  params = { whodunnit: user_id }
  return params if options.empty?

  params.merge(item_type:  what,
               created_at: starts..ends)
        .delete_if { |_, v| v.presence.nil? }
end
time_format() click to toggle source
# File lib/paper_trail_scrapbook/user_journal.rb, line 54
def time_format
  PaperTrailScrapbook.config.time_format
end
what() click to toggle source
# File lib/paper_trail_scrapbook/user_journal.rb, line 58
def what
  String(options.fetch(:klass, nil)).presence
end
when_range() click to toggle source
# File lib/paper_trail_scrapbook/user_journal.rb, line 62
def when_range
  "#{starts.strftime(time_format)} and #{ends.strftime(time_format)}"
end