Module: Hist::Model

Extended by:
ActiveSupport::Concern
Defined in:
app/models/hist/model.rb

Instance Method Summary collapse

Instance Method Details

#diff_hist(ver: nil, pending: nil, type: :json, exclude: [], include: [], associations: nil, only_diffs: false) ⇒ Object



116
117
118
119
120
121
122
123
124
125
# File 'app/models/hist/model.rb', line 116

def diff_hist(ver:nil, pending: nil, type: :json, exclude: [], include: [], associations: nil, only_diffs: false)
  if ver.present?
    return Hist::ApplicationRecord.diff(obj: self, ver: ver, type: type, exclude: exclude, include: include, associations: associations, only_diffs: only_diffs)
  elsif pending.present?
    return Hist::ApplicationRecord.diff(obj: self, pending: pending, type: type, exclude: exclude, include: include, associations: associations, only_diffs: only_diffs)
  else
    raise 'Error: either ver or pending parameter is required for diff_history'
  end

end

#hist_after_saveObject



55
56
57
58
59
# File 'app/models/hist/model.rb', line 55

def hist_after_save
  self.class.transaction do
    hist_save_actions
  end
end

#hist_around_saveObject



48
49
50
51
52
53
# File 'app/models/hist/model.rb', line 48

def hist_around_save
  self.class.transaction do
    yield
    self.hist_save_actions
  end
end

#hist_created_atObject



164
165
166
# File 'app/models/hist/model.rb', line 164

def hist_created_at
  @hist_created_at
end

#hist_created_at=(value) ⇒ Object



160
161
162
# File 'app/models/hist/model.rb', line 160

def hist_created_at=(value)
  @hist_created_at = value
end

#hist_extraObject



182
183
184
# File 'app/models/hist/model.rb', line 182

def hist_extra
  @hist_extra
end

#hist_extra=(value) ⇒ Object



186
187
188
189
# File 'app/models/hist/model.rb', line 186

def hist_extra=(value)
  @hist_whodunnit_extra_set = true
  @hist_extra = value
end

#hist_json(exclude: [], include: [], associations: nil) ⇒ Object



139
140
141
# File 'app/models/hist/model.rb', line 139

def hist_json(exclude: [], include: [], associations: nil)
  Hist::ApplicationRecord.to_json(obj: self, exclude: exclude, include: include, associations: associations)
end

#hist_save_actionsObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/hist/model.rb', line 26

def hist_save_actions
  Hist::Pending.find(self.pending_id).discard unless self.pending_id.nil?

  # Need to fix associations... won't update properly on parent save cause rails is baka.
  if self.pending_id.present? || self.ver_id.present?
    current = ApplicationRecord.fix_save_associations(obj: self)
  else
    current = self
  end

  # Does this happen  after reload?
  u = self.hist_whodunnit if self.record_hist_whodunnit?
  e = self.hist_extra if self.record_hist_extra?
  current.record_version(user: u, extra: e) if self.class.base_class.hist_config.auto_version

  if self.pending_id.present? || self.ver_id.present?
    self.reload
  else
    self.reload_hist
  end
end

#hist_whodunnitObject



168
169
170
# File 'app/models/hist/model.rb', line 168

def hist_whodunnit
  @hist_whodunnit
end

#hist_whodunnit=(value) ⇒ Object



172
173
174
175
# File 'app/models/hist/model.rb', line 172

def hist_whodunnit=(value)
  @hist_whodunnit_user_set = true
  @hist_whodunnit = value
end

#pending_idObject



152
153
154
# File 'app/models/hist/model.rb', line 152

def pending_id
  @pending_id
end

#pending_id=(value) ⇒ Object



156
157
158
# File 'app/models/hist/model.rb', line 156

def pending_id=(value)
  @pending_id = value
end

#pendings(user: nil, extra: nil, only: 'kept') ⇒ Object



111
112
113
114
# File 'app/models/hist/model.rb', line 111

def pendings(user: nil, extra: nil, only: 'kept')
  @pendings ||= ::Hist::Pending.get(obj: self, user: nil, extra: nil, only: only)
  @pendings
end

#raw_pendings(user: nil, extra: nil, only: 'kept') ⇒ Object



106
107
108
109
# File 'app/models/hist/model.rb', line 106

def raw_pendings(user: nil, extra: nil, only: 'kept')
  @raw_pendings ||= ::Hist::Pending.raw_get(obj: self, user: nil, extra: nil, only: only)
  @raw_pendings
end

#raw_versions(user: nil, extra: nil, only: 'kept') ⇒ Object



96
97
98
99
# File 'app/models/hist/model.rb', line 96

def raw_versions(user: nil, extra: nil, only: 'kept')
  @raw_versions ||= ::Hist::Version.raw_get(obj: self, user: nil, extra: nil, only: only)
  @raw_versions
end

#record_hist_extra?Boolean

Returns:

  • (Boolean)


200
201
202
# File 'app/models/hist/model.rb', line 200

def record_hist_extra?
  @hist_whodunnit_extra_set
end

#record_hist_whodunnit?Boolean

Returns:

  • (Boolean)


196
197
198
# File 'app/models/hist/model.rb', line 196

def record_hist_whodunnit?
  @hist_whodunnit_user_set
end

#record_pending(user: nil, extra: nil) ⇒ Object



66
67
68
# File 'app/models/hist/model.rb', line 66

def record_pending(user: nil, extra: nil)
  ::Hist::Pending.put(obj: self, user: user, extra: extra)
end

#record_version(user: nil, extra: nil) ⇒ Object



62
63
64
# File 'app/models/hist/model.rb', line 62

def record_version(user: nil, extra: nil)
  ::Hist::Version.put(obj: self, user: user, extra: extra)
end

#reloadObject



134
135
136
137
# File 'app/models/hist/model.rb', line 134

def reload
  reload_hist
  super
end

#reload_histObject



127
128
129
130
131
132
# File 'app/models/hist/model.rb', line 127

def reload_hist
  @versions = nil
  @raw_versions = nil
  @pendings = nil
  @raw_pendings = nil
end

#system_hist_extra=(value) ⇒ Object



191
192
193
194
# File 'app/models/hist/model.rb', line 191

def system_hist_extra=(value)
  @hist_whodunnit_extra_set = false
  @hist_extra = value
end

#system_hist_whodunnit=(value) ⇒ Object



177
178
179
180
# File 'app/models/hist/model.rb', line 177

def system_hist_whodunnit=(value)
  @hist_whodunnit_user_set = false
  @hist_whodunnit = value
end

#ver_idObject

Attributes essentially… defined as methods as just don’t want to save this data as part of the JSON hash



144
145
146
# File 'app/models/hist/model.rb', line 144

def ver_id
  @ver_id
end

#ver_id=(value) ⇒ Object



148
149
150
# File 'app/models/hist/model.rb', line 148

def ver_id=(value)
  @ver_id = value
end

#version_at(date) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/models/hist/model.rb', line 83

def version_at(date)
  @versions ||= ::Hist::Version.get(obj: self)
  @versions.each do |ver|
    #raise "Date.parse: " + Date.parse(ver.created_at.to_s).to_s + " and date: " + date.to_s + " And equals: " + (Date.parse(ver.created_at.to_s) <= date).to_s + " and ver_id: " + ver.id.to_s
    if Date.parse(ver.hist_created_at.to_s) <= date
      #raise ver.reify.ver_id.to_s
      return ver
    end
  end
  return @versions.last if @version.present?
  return self
end

#version_at_temp(date) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/models/hist/model.rb', line 70

def version_at_temp(date)
  @versions ||= ::Hist::Version.raw_get(obj: self)
  @versions.each do |ver|
    #raise "Date.parse: " + Date.parse(ver.created_at.to_s).to_s + " and date: " + date.to_s + " And equals: " + (Date.parse(ver.created_at.to_s) <= date).to_s + " and ver_id: " + ver.id.to_s
    if Date.parse(ver.created_at.to_s) <= date
      #raise ver.reify.ver_id.to_s
      return ver.reify
    end
  end
  return @versions.last.reify if @version.present?
  return self
end

#versions(user: nil, extra: nil, only: 'kept') ⇒ Object



101
102
103
104
# File 'app/models/hist/model.rb', line 101

def versions(user: nil, extra: nil, only: 'kept')
  @versions ||= ::Hist::Version.get(obj: self, user: nil, extra: nil, only: only)
  @versions
end