Module: EstoreConventions::InstanceMethods

Defined in:
lib/estore_conventions.rb

Instance Method Summary collapse

Instance Method Details

#archived_attribute(attribute, time_frame = (30.days)) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/estore_conventions.rb', line 40

def archived_attribute(attribute, time_frame=(30.days))
  # normalize time since we are returning today as a whole day
  time_frame = (DateTime.now + 1.day).beginning_of_day - time_frame

  # transform papertrail objects to reify objects
  a = self.versions.map {|v| v.reify }

  # get rid of nil first version
  a.compact!

  # add the current object to the array
  a << self

  # weed out old entries
  a.delete_if {|x| x.rails_updated_at <= time_frame }

  # sort hash based on key  
  a.sort_by!{|x| x.rails_updated_at }

  # transform reify objects into hash of {date => value}
  a.reduce({}) do |hsh,val|
    hsh[val.rails_updated_at.strftime('%Y-%m-%d')] = val.send(attribute)
    hsh
  end
end