Module: GreatestUpdatedAt::Calculations

Defined in:
lib/greatest_updated_at/calculations.rb

Instance Method Summary collapse

Instance Method Details

#greatest_updated_atObject

Make an attempt to get the most recently updated record out of all the included (aka preloaded) records



22
23
24
# File 'lib/greatest_updated_at/calculations.rb', line 22

def greatest_updated_at
  greatest_updated_at_from_tables(self.includes_values.collect{|relation| Array(relation)}.flatten << self.table_name)
end

#greatest_updated_at_from_tables(table_names) ⇒ Object

a dumb method for getting the most recently updated date from the named tables. Note that if the tables are not selected in your query as named you will receive an error.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/greatest_updated_at/calculations.rb', line 5

def greatest_updated_at_from_tables(table_names)
  max_calcs = Array(table_names).collect{|name|
    "MAX(#{connection.quote_table_name(name.to_s.pluralize)}.#{connection.quote_column_name("updated_at")})"
  }
  
  if max_calcs.length == 1
    pluck_val = max_calcs.first
  else
    pluck_val = "GREATEST(#{max_calcs.join(",")})"
  end
  
  self.pluck(pluck_val).first

end