Module: Server::Helpers

Defined in:
lib/server.rb

Instance Method Summary collapse

Instance Method Details

#changedObject



110
111
112
# File 'lib/server.rb', line 110

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

#changed_elements(scope, opts) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/server.rb', line 123

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? &&
        @diff[scope].try(:common).try(:attributes).try(:[], "extracted")
    )
  end
  changed
end

#diff_to_object(diff) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/server.rb', line 174

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



163
164
165
166
167
168
169
170
171
172
# File 'lib/server.rb', line 163

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



101
102
103
# File 'lib/server.rb', line 101

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


77
78
79
80
81
82
83
# File 'lib/server.rb', line 77

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

#offset_class(first_col) ⇒ Object



105
106
107
108
# File 'lib/server.rb', line 105

def offset_class(first_col)
  return "" if first_col
  "col-md-offset-6"
end

#only_in_aObject



93
94
95
# File 'lib/server.rb', line 93

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

#only_in_bObject



97
98
99
# File 'lib/server.rb', line 97

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

#pluralize_scope(object, singular, plural) ⇒ Object



114
115
116
# File 'lib/server.rb', line 114

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

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



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

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



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

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

#repository_changesObject



118
119
120
121
# File 'lib/server.rb', line 118

def repository_changes
  klass = @diff["repositories"].changed.first.first.class
  changed_elements("repositories", attributes: klass.attributes, key: klass.key)
end

#safe_length(object, attribute) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/server.rb', line 85

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

#scope_help(scope) ⇒ Object



60
61
62
63
# File 'lib/server.rb', line 60

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

#scope_info(scope) ⇒ Object



65
66
67
# File 'lib/server.rb', line 65

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

#scope_initials(scope) ⇒ Object



73
74
75
# File 'lib/server.rb', line 73

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

#scope_meta_info(scope) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/server.rb', line 52

def scope_meta_info(scope)
  return "" unless @description[scope]

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

#scope_title(scope) ⇒ Object



69
70
71
# File 'lib/server.rb', line 69

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