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
-
#historical(attr_or_label, label = nil) ⇒ Object
Retrieve the historical record or a single attribute value from the specified label.
-
#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. -
#record!(label = nil) ⇒ Record
Save the current values as a historical record.
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.
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.
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/mongoid/historicals.rb', line 69 def historical_difference(attr, label, = {}) opts = { default: 0 }.merge() 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
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 |