Module: Plugins::PapertrailHelper

Defined in:
app/helpers/plugins/papertrail_helper.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) filter_out_attributes(attributes)

Returns a new hash of attributes that doesn't have unwanted key/value pair in them, currently filters out hashes with keys of “id” “created_at” “created_by_id” “updated_by_id” “project_id”



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'app/helpers/plugins/papertrail_helper.rb', line 204

def filter_out_attributes attributes
  attributes_filter = ["id", "created_at", "created_by_id", "updated_by_id", "project_id"]
  filtered_attributes = {}

  # Make the "updated_at" attribute the first hash for formatting purposes
  filtered_attributes["updated_at"] = 0

  attributes.each do |key, value|
    if !attributes_filter.include?(key)
      filtered_attributes[key] = value
    end
  end

  return filtered_attributes
end

- (Object) get_diffs(version_new, version_old)

Snipped from @merfoo's helper

Let curr_index_new and curr_index_old loop from 0 to new string length and old string length respectively
  if curr_index_new < new strings length and curr_index_old < old string length
    if characters from both strings at their respective indexes match
      increase both index by 1
      continue to next iteration
    else if characters from both strings dont match
      Let new_index_new = curr_index_new
      Let new_index_old loop from curr_index_old to old string length
        if new_index_new > new string length
          add rest of new string to added strings and add rest of old string to deleted strings
          break out of loop
        if character of old string at newOldIndex == character of new string at new_index_new
          if (new_index_new - curr_index_new) > 0
            add substring of new string from curr_index_new to new_index_new to added strings
          if (newOldIndex - currOldIndex) > 0
            add substring of old string from curr_index_old to new_index_old to deleted strings
        if new_index_old > old string length
          set new_index_old to curr_index_old
          increase new_index_new by 1
  else if either of curr_index_new or curr_index_old > their respective strings
    if curr_index_new < new string
      add rest of new string to added strings
    else if curr_index_old < old string length
      add rest of old string to deleted strings


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/helpers/plugins/papertrail_helper.rb', line 42

def get_diffs version_new, version_old
  added_strings = []
  added_strings_indices = []
  deleted_strings = []
  deleted_strings_indices = []

  curr_index_new = 0
  curr_index_old = 0

  while curr_index_new < version_new.length || curr_index_old < version_old.length
    if curr_index_new < version_new.length && curr_index_old < version_old.length
      if version_new[curr_index_new] == version_old[curr_index_old]
        curr_index_new += 1
        curr_index_old += 1
      else
        new_index_new = curr_index_new
        new_index_old = curr_index_old

        while new_index_old < version_old.length
          if new_index_new > version_new.length || version_old[new_index_old] == version_new[new_index_new]
            if curr_index_new != new_index_new
              added_strings.push(version_new[curr_index_new...new_index_new])
              added_strings_indices.push(curr_index_new)
            end

            if curr_index_old != new_index_old
              deleted_strings.push(version_old[curr_index_old...new_index_old])
              deleted_strings_indices.push(curr_index_old)
            end

            curr_index_new = new_index_new
            curr_index_old = new_index_old
            break
          elsif new_index_old == version_old.length - 1
            new_index_old = curr_index_old;
            new_index_new += 1 
            next
          end

          new_index_old += 1;
        end
      end
    else
      if curr_index_new < version_new.length
        added_strings.push(version_new[curr_index_new...version_new.length])
        added_strings_indices.push(curr_index_new)
      elsif curr_index_old < version_old.length
        deleted_strings.push(version_old[curr_index_old...version_old.length])
        deleted_strings_indices.push(curr_index_old)
      end

      break
    end
  end

  return {
    "added_strings" => added_strings,
    "added_strings_indices" => added_strings_indices, 
    "deleted_strings" => deleted_strings,
    "deleted_strings_indices" => deleted_strings_indices
  }
end

- (Object) get_diffs_date(version_new, version_old)

For getting diffs between strings where the length are the same for each



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'app/helpers/plugins/papertrail_helper.rb', line 106

def get_diffs_date version_new, version_old
  added_strings = []
  added_strings_indices = []
  deleted_strings = []
  deleted_strings_indices = []

  start_index = 0
  end_index = 0
  found_mismatch = false

  for end_index in 0...version_new.length
    if version_new[end_index] == version_old[end_index]
      if found_mismatch
        found_mismatch = false
        added_strings.push(version_new[start_index...end_index])
        deleted_strings.push(version_old[start_index...end_index])
        added_strings_indices.push(start_index)
        deleted_strings_indices.push(start_index)
      end
    else
      if !found_mismatch
        found_mismatch = true
        start_index = end_index
      end
    end
  end

  return {
    "added_strings" => added_strings,
    "added_strings_indices" => added_strings_indices, 
    "deleted_strings" => deleted_strings,
    "deleted_strings_indices" => deleted_strings_indices
  }
end

- (Object) get_highlighted_words(words, highlighted_words, highlighted_words_indices, style_class)

Returns an html string of <p> where a <p> is given the class of style_class if it is in the highlighted_words array



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'app/helpers/plugins/papertrail_helper.rb', line 143

def get_highlighted_words words, highlighted_words, highlighted_words_indices, style_class
  start_index = 0
  end_index = 0
  html_string = ""

  for highlighted_index in 0...highlighted_words_indices.length
    while end_index < highlighted_words_indices[highlighted_index]
      end_index += 1
    end
    
    if start_index != end_index
      html_string += "<span>"
      html_string += words[start_index...end_index]
      html_string += "</span>"
    end    

    start_index = end_index
    end_index += highlighted_words[highlighted_index].length

    html_string += "<span class=\"#{style_class}\">"
    html_string += words[start_index...end_index]
    html_string += "</span>"

    start_index = end_index
  end

  if end_index < words.length
    html_string += "<span>"
    html_string += words[end_index...words.length]
    html_string += "</span>"
  end

  return html_string.html_safe
end

- (Object) get_unique_authors(version_list)

Returns a hash of all the unique authors for a papertrail obj hash key is the author email, hash value is the author name



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'app/helpers/plugins/papertrail_helper.rb', line 180

def get_unique_authors version_list
  unique_authors = {}

  # Iterate over each version
  version_list.each do |version|
    version_author = User.find(version.whodunnit)

    # If the current version author email is not present in the
    # unique_authors hash, add it
    if !unique_authors.key?(version_author.email)
      unique_authors[version_author.email] = version_author.name
    end
  end

  return unique_authors
end

- (Object) paper_trail_version_tag(paper_trail_version)



3
4
5
6
# File 'app/helpers/plugins/papertrail_helper.rb', line 3

def paper_trail_version_tag(paper_trail_version)
  return nil if paper_trail_version.nil?
  "Revision #{paper_trail_version.index}" 
end


8
9
10
# File 'app/helpers/plugins/papertrail_helper.rb', line 8

def papertrail_link_tag(object)
  (:li, link_to('Papertrail', papertrail_path(object_type: object.class, object_id: object.id))) if object.respond_to?(:versions) 
end