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) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'app/services/activity/compact_notes.rb', line 4

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) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'app/services/activity/compact_notes.rb', line 16

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