Class: ProMonitor::Change

Inherits:
Object
  • Object
show all
Defined in:
lib/pro_monitor/change.rb

Instance Method Summary collapse

Constructor Details

#initialize(action, model) ⇒ Change

Returns a new instance of Change.



3
4
5
# File 'lib/pro_monitor/change.rb', line 3

def initialize(action, model)
  @action, @model = action, model
end

Instance Method Details

#actionObject



13
14
15
# File 'lib/pro_monitor/change.rb', line 13

def action
  @action.to_sym
end

#attribute_update_details(attribute_name, values) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/pro_monitor/change.rb', line 54

def attribute_update_details(attribute_name, values)
  i18n_args = {
    attribute_name: pretty_attribute_name(attribute_name),
    from: pretty_attribute(attribute_name, values.first),
    to: pretty_attribute(attribute_name, values.last),
    scope: :pro_monitor
  }
  i18n(:attribute_change, i18n_args)
end

#changed_attribute_namesObject



36
37
38
# File 'lib/pro_monitor/change.rb', line 36

def changed_attribute_names
  changes.keys
end

#changesObject



17
18
19
# File 'lib/pro_monitor/change.rb', line 17

def changes
  model.changes.with_indifferent_access
end

#date_formatObject



72
73
74
# File 'lib/pro_monitor/change.rb', line 72

def date_format
  '%m/%d/%Y'
end

#default_pretty_actionObject



94
95
96
97
98
99
100
# File 'lib/pro_monitor/change.rb', line 94

def default_pretty_action
  case action
    when :create then "created"
    when :update then "updated"
    when :destroy then "deleted"
  end
end

#descriptionObject



40
41
42
# File 'lib/pro_monitor/change.rb', line 40

def description
  "#{pretty_model_type} #{model_name} #{pretty_action}"
end

#detailsObject



44
45
46
# File 'lib/pro_monitor/change.rb', line 44

def details
  return update_details if update?
end

#i18n(key, options = {}) ⇒ Object



106
107
108
# File 'lib/pro_monitor/change.rb', line 106

def i18n(key, options = {})
  I18n.t(key, options)
end

#model_nameObject



25
26
27
28
29
30
# File 'lib/pro_monitor/change.rb', line 25

def model_name
  ProMonitor.config.name_attributes.each do |name_attribute|
    return model.send(name_attribute) if model.respond_to?(name_attribute)
  end
  model.id.to_s
end

#model_name_i18n_keyObject



64
65
66
# File 'lib/pro_monitor/change.rb', line 64

def model_name_i18n_key
  model_type.underscore
end

#model_typeObject



21
22
23
# File 'lib/pro_monitor/change.rb', line 21

def model_type
  model.class.name
end

#pretty_actionObject



102
103
104
# File 'lib/pro_monitor/change.rb', line 102

def pretty_action
  i18n(action, default: default_pretty_action)
end

#pretty_attribute(attribute_name, value) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/pro_monitor/change.rb', line 80

def pretty_attribute(attribute_name, value)
  if value.is_a?(Date)
    I18n.l(value, format: date_format)
  elsif value.is_a?(DateTime)
    I18n.l(value, format: time_format)
  else
    value.to_s
  end
end

#pretty_attribute_name(attribute_name) ⇒ Object



90
91
92
# File 'lib/pro_monitor/change.rb', line 90

def pretty_attribute_name(attribute_name)
  i18n(attribute_name, default: attribute_name.humanize)
end

#pretty_model_typeObject



68
69
70
# File 'lib/pro_monitor/change.rb', line 68

def pretty_model_type
  i18n(model_name_i18n_key, default: model_name_i18n_key.humanize)
end

#time_formatObject



76
77
78
# File 'lib/pro_monitor/change.rb', line 76

def time_format
  '%m/%d/%Y %I:%M:%S %p'
end

#update_detailsObject



48
49
50
51
52
# File 'lib/pro_monitor/change.rb', line 48

def update_details
  changes.map do |attribute_name, values|
    attribute_update_details(attribute_name, values)
  end
end