Class: Stenographer::Change

Inherits:
ApplicationRecord show all
Defined in:
app/models/stenographer/change.rb

Constant Summary collapse

VALID_CHANGE_TYPES =
Stenographer.change_types

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_or_update_by_source_id(change_params) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'app/models/stenographer/change.rb', line 59

def self.create_or_update_by_source_id(change_params)
  source_id = change_params[:source_id]

  if source_id.present? && (change = Change.find_by(source_id: source_id)).present?
    change.environments += ", #{change_params[:environments]}"
    change.save
  else
    Change.create(change_params)
  end
end

Instance Method Details

#environments_to_tagsObject



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/models/stenographer/change.rb', line 45

def environments_to_tags
  tags = []

  environment_array = environments.split(',').map(&:strip)
  environment_array.each do |environment|
    tags << {
      name: environment.capitalize,
      color: environment == 'production' ? 'is-primary' : 'is-light'
    }
  end

  tags
end

#matches_filters(output = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/stenographer/change.rb', line 29

def matches_filters(output = nil)
  return false if output.blank?

  filters = output.filters_hash
  return true if filters.empty?

  filters.none? do |key, value|
    if value.present? && key == :environments
      environments_array = environments.split(',').map(&:strip)
      environments_array.last != value.strip
    else
      value.present? && send(key) != value
    end
  end
end

#sanitize_environmentObject



14
15
16
17
18
19
20
21
22
# File 'app/models/stenographer/change.rb', line 14

def sanitize_environment
  if environments.present?
    environments_array = environments.split(',').map do |environment|
      environment.downcase.strip
    end
    environments_array.uniq!
    environments_array.join(', ')
  end
end

#to_markdown(truncate: nil) ⇒ Object



24
25
26
27
# File 'app/models/stenographer/change.rb', line 24

def to_markdown(truncate: nil)
  msg = truncate ? message.truncate(truncate) : message
  Stenographer.markdown_renderer.render(msg).html_safe
end