Class: Activity::CompactNotes

Inherits:
Object
  • Object
show all
Defined in:
app/services/activity/compact_notes.rb

Overview

Service object: compact notes.

Instance Method Summary collapse

Instance Method Details

#process(limit: nil) ⇒ void

This method returns an undefined value.

Strips the repeated "Time lock was overdue" boilerplate from activity notes.

Parameters:

  • limit (Integer, nil) (defaults to: nil)

    optional cap on how many activities to process



7
8
9
10
11
12
13
14
15
16
17
# File 'app/services/activity/compact_notes.rb', line 7

def process(limit: nil)
  activities ||= Activity.where("notes like '%Time lock was overdue%'")
  activities = activities.limit(limit) if limit
  activities.find_each do |activity|
    new_note = trim_notes(activity.notes, activity.id)
    # puts "#{activity.notes}"
    # puts "---"
    puts "[#{activity.id}] : #{new_note}" if new_note.present?
    activity.update_columns(notes: new_note)
  end
end

#trim_notes(notes, _id = nil) ⇒ String?

Removes the boilerplate sentences and compacts whitespace in a note body.

Parameters:

  • notes (String)

    raw HTML note text

  • _id (Integer, nil) (defaults to: nil)

    unused (kept for debugging output)

Returns:

  • (String, nil)

    cleaned note text, or nil when nothing remains



23
24
25
26
27
28
29
30
31
32
# File 'app/services/activity/compact_notes.rb', line 23

def trim_notes(notes, _id = nil)
  html_note = Nokogiri::HTML(notes)
  new_note = html_note.text
  new_note = new_note.gsub(/\[.*\]\s*Time lock was overdue and moved to next working day for assigned employee/, '')
  new_note = new_note.squeeze(' ')
  new_note = new_note.squeeze('.')
  new_note = new_note.split(/(\[.*\])(\S*)/).map(&:strip).filter_map(&:presence).map { |s| s[0] == '.' ? s[1..] : s }.join("<br>")
  new_note = new_note.split('.').join(".\n")
  new_note.squish.presence
end