Module: Mongoid::Historicals

Extended by:
ActiveSupport::Concern
Defined in:
lib/mongoid/historicals.rb,
lib/mongoid/historicals/record.rb,
lib/mongoid/historicals/version.rb

Defined Under Namespace

Modules: ClassMethods Classes: Record

Constant Summary collapse

VERSION =
"0.1.3"

Instance Method Summary collapse

Instance Method Details

#historical(attr_or_label, label = nil) ⇒ Object

Retrieve the historical record or a single attribute value from the specified label. If only one argument is given, it will return the entire record with that label. If two arguments are given, the first argument will specify the attribute and the second argument is the label to use.

Parameters:

  • attr_or_label (String, #to_datetime)

    If only one argument is given, this is the label for the record to be returned. If two arguments are given, this should be the symbol of the attribute whose value should be returned.

Returns:

  • With one argument given, the historical record or nil if none exists. With two arguments given, it should return the value of the requested attribute for the specified label.



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mongoid/historicals.rb', line 48

def historical(attr_or_label, label = nil)
  if label.nil?
    self.historicals.where(:'_label' => labelize(attr_or_label)).first
  else
    record = self.historicals.where(:'_label' => labelize(label)).first

    if record
      record[attr_or_label]
    else
      nil
    end
  end
end

#historical_difference(attr, label, options = {}) ⇒ Object

Return the difference between the current value of the attribute and the value of attribute from the label historical record.

Parameters:

  • attr (Symbol)

    The attribute on which you are calculating the difference

  • label (String, Symbol, #to_datetime)

    The label as a String, Symbol or Datetime for the historical record against which you are comparing

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :default (Object)

    The value you want returned if there is no historical for comparison (default: 0)



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/mongoid/historicals.rb', line 69

def historical_difference(attr, label, options = {})
  opts = {
    default: 0
  }.merge(options)

  begin
    self[attr] - historical(attr, label)
  rescue # Pokemon exception handling, but actually seems appropriate here
    opts[:default]
  end
end

#record!(label = nil) ⇒ Record

Save the current values as a historical record

Parameters:

  • label (String, #to_datetime) (defaults to: nil)

    The label as a String, Symbol or DateTime that the historical record will be saved as. If you pass a DateTime object, it will use the :frequency option from historicals to save as a date/time.

Returns:

  • (Record)

    the historical record that is saved



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mongoid/historicals.rb', line 22

def record!(label = nil)
  label ||= Time.now
  record = historical(label) || self.historicals.build(:'_label' => labelize(label))

  self.class.historical_attributes.each do |attr|
    record[attr] = self.send(attr)
  end

  record.save!
  destroy_old_historicals!
  record
end