Module: Server::Helpers

Defined in:
lib/server.rb

Instance Method Summary collapse

Instance Method Details

#changedObject



83
84
85
# File 'lib/server.rb', line 83

def changed
  "<h3>In both with different attributes:</h3>"
end

#changed_elements(scope, opts) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/server.rb', line 91

def changed_elements(scope, opts)
  optional_attributes = opts[:optional_attributes] || []

  changed = []
  @diff[scope].changed.each do |change|
    changes = []
    relevant_attributes = if opts[:attributes]
      opts[:attributes].dup
    else
      change[0].attributes.keys & change[1].attributes.keys
    end

    (1..optional_attributes.length).each do |i|
      if change[0][optional_attributes[i - 1]] ==
          change[1][optional_attributes[i - 1]]
        relevant_attributes.push(optional_attributes[i])
      else
        break
      end
    end
    relevant_attributes.each do |attribute|
      if change[0][attribute] != change[1][attribute]
        changes.push(
          attribute + ": " + human_readable_attribute(change[0], attribute) + "" +
            human_readable_attribute(change[1], attribute)
        )
      end
    end

    changed.push(
      id:       change[0][opts[:key]],
      change:   "(" + changes.join(", ") + ")",
      diffable: change[0].is_a?(UnmanagedFile) && change[0].is_a?(UnmanagedFile) &&
        change[0].file? && change[1].file?
    )
  end
  changed
end

#diff_to_object(diff) ⇒ Object



141
142
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/server.rb', line 141

def diff_to_object(diff)
  diff = Machinery.scrub(diff)
  lines = diff.lines[2..-1]
  diff_object = {
    file: diff[/--- a(.*)/, 1],
    additions: lines.select { |l| l.start_with?("+") }.length,
    deletions: lines.select { |l| l.start_with?("-") }.length
  }

  original_line_number = 0
  new_line_number = 0
  diff_object[:lines] = lines.map do |line|
    line = ERB::Util.html_escape(line.chomp).
      gsub("\\", "&#92;").
      gsub("\t", "&nbsp;" * 8)
    case line
    when /^@.*/
      entry = {
        type: "header",
        content: line
      }
      original_line_number = line[/-(\d+)/, 1].to_i
      new_line_number = line[/\+(\d+)/, 1].to_i
    when /^ .*/, ""
      entry = {
        type: "common",
        new_line_number: new_line_number,
        original_line_number: original_line_number,
        content: line[1..-1]
      }
      new_line_number += 1
      original_line_number += 1
    when /^\+.*/
      entry = {
        type: "addition",
        new_line_number: new_line_number,
        content: line[1..-1]
      }
      new_line_number += 1
    when /^\-.*/
      entry = {
        type: "deletion",
        original_line_number: original_line_number,
        content: line[1..-1]
      }
      original_line_number += 1
    end

    entry
  end

  diff_object
end

#human_readable_attribute(object, attribute) ⇒ Object



130
131
132
133
134
135
136
137
138
139
# File 'lib/server.rb', line 130

def human_readable_attribute(object, attribute)
  value = object[attribute]

  case object
  when Machinery::SystemFile
    value = number_to_human_size(value) if attribute == "size"
  end

  value.to_s
end

#in_bothObject



79
80
81
# File 'lib/server.rb', line 79

def in_both
  "<h3>In both descriptions:</h3>"
end


55
56
57
58
59
60
61
# File 'lib/server.rb', line 55

def nav_class(scope)
  if @description
    return @description[scope] ? "" : "disabled"
  elsif @description_a && @description_b
    return @description_a[scope] && @description_b[scope] ? "" : "disabled"
  end
end

#only_in_aObject



71
72
73
# File 'lib/server.rb', line 71

def only_in_a
  "<h3>Only in '#{@description_a.name}':</h3>"
end

#only_in_bObject



75
76
77
# File 'lib/server.rb', line 75

def only_in_b
  "<h3>Only in '#{@description_b.name}':</h3>"
end

#pluralize_scope(object, singular, plural) ⇒ Object



87
88
89
# File 'lib/server.rb', line 87

def pluralize_scope(object, singular, plural)
  object.length.to_s + " " + Machinery.pluralize(object.length, singular, plural)
end

#render_partial(partial, locals = {}) ⇒ Object



21
22
23
24
# File 'lib/server.rb', line 21

def render_partial(partial, locals = {})
  source = File.read(File.join(Machinery::ROOT, "html/partials/#{partial}.html.haml"))
  haml source, locals: locals
end

#render_scope(scope) ⇒ Object



26
27
28
# File 'lib/server.rb', line 26

def render_scope(scope)
  render_partial scope, scope => @description[scope]
end

#safe_length(object, attribute) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/server.rb', line 63

def safe_length(object, attribute)
  if collection = object.try(attribute)
    collection.length
  else
    0
  end
end

#scope_help(scope) ⇒ Object



38
39
40
41
# File 'lib/server.rb', line 38

def scope_help(scope)
  text = scope_info(scope)[:description]
  Kramdown::Document.new(text).to_html
end

#scope_info(scope) ⇒ Object



43
44
45
# File 'lib/server.rb', line 43

def scope_info(scope)
  YAML.load(File.read(File.join(Machinery::ROOT, "plugins", "#{scope}/#{scope}.yml")))
end

#scope_initials(scope) ⇒ Object



51
52
53
# File 'lib/server.rb', line 51

def scope_initials(scope)
  scope_info(scope)[:initials].upcase
end

#scope_meta_info(scope) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/server.rb', line 30

def scope_meta_info(scope)
  return "" if !@description[scope]

  " (" \
  "inspected host: '#{@description[scope].meta.hostname}', " \
  "at: #{DateTime.parse(@description[scope].meta.modified).strftime("%F %T")})"
end

#scope_title(scope) ⇒ Object



47
48
49
# File 'lib/server.rb', line 47

def scope_title(scope)
  scope_info(scope)[:name]
end