• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pulibrary / pdc_describe / 95ff1d10-5774-4415-8b66-ffb4f4f58024

pending completion
95ff1d10-5774-4415-8b66-ffb4f4f58024

Pull #725

circleci

Carolyn Cole
Updating manifest not to include the javascript for edit This seems to do nothing, so I am removing it
Pull Request #725: Updating manifest not to include the javascript for edit

1638 of 1684 relevant lines covered (97.27%)

102.75 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.0
/app/models/work_activity.rb
1
# frozen_string_literal: true
2

3
require_relative "../lib/diff"
1✔
4

5
# rubocop:disable Metrics/ClassLength
6
class WorkActivity < ApplicationRecord
1✔
7
  belongs_to :work
1✔
8

9
  has_many :work_activity_notifications, dependent: :destroy
1✔
10

11
  USER_REFERENCE = /@[\w]*/.freeze # e.g. @xy123
1✔
12

13
  def self.add_system_activity(work_id, message, user_id, activity_type: "SYSTEM")
1✔
14
    activity = WorkActivity.new(
173✔
15
      work_id: work_id,
16
      activity_type: activity_type,
17
      message: message,
18
      created_by_user_id: user_id
19
    )
20
    activity.save!
173✔
21
    activity.notify_users
173✔
22
    activity
173✔
23
  end
24

25
  # Log notifications for each of the users references on the activity
26
  def notify_users
1✔
27
    users_referenced.each do |uid|
177✔
28
      user_id = User.where(uid: uid).first&.id
24✔
29
      if user_id.nil?
24✔
30
        Rails.logger.info("Message #{id} for work #{work_id} referenced an non-existing user: #{uid}")
3✔
31
      else
32
        WorkActivityNotification.create(work_activity_id: id, user_id: user_id)
21✔
33
      end
34
    end
35
  end
36

37
  # Returns the `uid` of the users referenced on the activity (without the `@` symbol)
38
  def users_referenced
1✔
39
    message.scan(USER_REFERENCE).map { |at_uid| at_uid[1..-1] }
201✔
40
  end
41

42
  def created_by_user
1✔
43
    return nil if created_by_user_id.nil?
15✔
44
    User.find(created_by_user_id)
10✔
45
  end
46

47
  def message_html
1✔
48
    if activity_type == "CHANGES"
12✔
49
      metadata_changes_html
×
50
    elsif activity_type == "FILE-CHANGES"
12✔
51
      file_changes_html
5✔
52
    else
53
      comment_html
7✔
54
    end
55
  end
56

57
  private
1✔
58

59
    def comment_html
1✔
60
      # convert user references to user links
61
      text = message.gsub(USER_REFERENCE) do |at_uid|
7✔
62
        uid = at_uid[1..-1]
4✔
63
        user = User.where(uid: uid).first
4✔
64
        user_info = user&.display_name_safe || uid
4✔
65
        "<a class='comment-user-link' title='#{user_info}' href='{USER-PATH-PLACEHOLDER}/#{uid}'>#{at_uid}</a>"
4✔
66
      end
67
      # allow ``` for code blocks (Kramdown only supports ~~~)
68
      text = text.gsub("```", "~~~")
7✔
69
      parsed_document = Kramdown::Document.new(text)
7✔
70
      parsed_document.to_html
7✔
71
    end
72

73
    # Returns the message formatted to display _file_ changes that were logged as an activity
74
    def file_changes_html
1✔
75
      changes = JSON.parse(message)
5✔
76
      changes_html = changes.map do |change|
5✔
77
        icon = if change["action"] == "deleted"
10✔
78
                 '<i class="bi bi-file-earmark-minus-fill file-deleted-icon"></i>'
×
79
               else
80
                 '<i class="bi bi-file-earmark-plus-fill file-added-icon"></i>'
10✔
81
               end
82
        "<tr><td>#{icon}</td><td>#{change['action']}</td> <td>#{change['filename']}</td>"
10✔
83
      end
84
      "<p><b>Files updated:</b></p><table>#{changes_html.join}</table>"
5✔
85
    end
86

87
    # Returns the message formatted to display _metadata_ changes that were logged as an activity
88
    def metadata_changes_html
1✔
89
      text = ""
×
90
      changes = JSON.parse(message)
×
91
      changes.keys.each do |field|
×
92
        values = changes[field].map { |value| change_value_html(value) }.join
×
93
        text += "<p><b>#{field}</b>: #{values}</p>"
×
94
      end
95
      text
×
96
    end
97

98
    def change_value_html(value)
1✔
99
      if value["action"] == "added"
×
100
        change_added_html(value["value"])
×
101
      elsif value["action"] == "removed"
×
102
        change_removed_html(value["value"])
×
103
      else
104
        change_set_html(value["from"], value["to"])
×
105
      end
106
    end
107

108
    def change_added_html(value)
1✔
109
      <<-HTML
×
110
        <i>(added)</i> <span>#{value}</span><br/>
111
      HTML
112
    end
113

114
    def change_removed_html(value)
1✔
115
      <<-HTML
×
116
        <i>(removed)</i> <span>#{value}</span><br/>
117
      HTML
118
    end
119

120
    def change_set_html(from, to)
1✔
121
      SimpleDiff.new(from, to).to_html
×
122
    end
123
end
124
# rubocop:enable Metrics/ClassLength
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc