greatest_updated_at 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/greatest_updated_at/calculations.rb +40 -12
- metadata +46 -48
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b4972e75f678551d7572c46d2679fb160dbfa395
|
4
|
+
data.tar.gz: 17d201b2f70d4123fbfee160637ab560c3651043
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4c1184bab68f4ec25cddd1218de294e3158bdda49693cd49f1419c4edd527f0bc2b34790b0a0cc2b7f4c65aa3f0729eb28cead7d28244b7bf389180ad8c09f5f
|
7
|
+
data.tar.gz: 82b418b5ac5272b5006ecdd5b7309caf03c6bffab8001bc34a1201c571518bfdc4b817f835adc67b88a68db69ce872236fd7f63be8a5a79435da41ae3c493d72
|
@@ -3,24 +3,52 @@ module GreatestUpdatedAt
|
|
3
3
|
# a dumb method for getting the most recently updated date from the named tables.
|
4
4
|
# Note that if the tables are not selected in your query as named you will receive an error.
|
5
5
|
def greatest_updated_at_from_tables(table_names)
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
self.pluck(greatest_select_val(table_names)).first
|
7
|
+
end
|
8
|
+
|
9
|
+
# Make an attempt to get the most recently updated record out of all the
|
10
|
+
# included (aka preloaded) records
|
11
|
+
def greatest_updated_at
|
12
|
+
# Force everything included to be in the query via LEFT OUTER JOIN
|
13
|
+
relation = self.eager_load(self.includes_values)
|
9
14
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
15
|
+
# Using this to get a list of tables included via join statements, we don't want to calculate based on those
|
16
|
+
alias_tracker = ActiveRecord::Associations::AliasTracker.create(relation.klass.connection, relation.joins_values)
|
17
|
+
join_tables = alias_tracker.aliases.keys
|
18
|
+
|
19
|
+
if relation.eager_loading?
|
20
|
+
relation.send(:find_with_associations) { |rel| relation = rel };nil
|
14
21
|
end
|
22
|
+
relation.select_values = ['1'] #placeholder while we generate the initial arel
|
23
|
+
arel = relation.arel
|
24
|
+
|
25
|
+
# collect the list of table names we want to get the max updated at from
|
26
|
+
join_nodes = arel.join_sources.reject{ |join| join.kind_of?(Arel::Nodes::StringJoin) }
|
27
|
+
table_names = join_nodes.collect{ |join| join.left.name }
|
28
|
+
|
29
|
+
# remove the join tables and make sure the root table is present
|
30
|
+
table_names = (table_names - join_tables) | [relation.klass.table_name]
|
15
31
|
|
16
|
-
|
32
|
+
# Clear the arel projection, it used to be a placeholder
|
33
|
+
arel.projections = []
|
34
|
+
|
35
|
+
arel.project greatest_select_val(table_names)
|
17
36
|
|
37
|
+
relation.klass.connection.select_value(arel, 'SQL', arel.bind_values + relation.bind_values)
|
38
|
+
|
18
39
|
end
|
19
40
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
41
|
+
protected
|
42
|
+
def greatest_select_val(table_names)
|
43
|
+
max_calcs = Array(table_names).collect{|name|
|
44
|
+
"MAX(#{connection.quote_table_name(name.to_s)}.#{connection.quote_column_name("updated_at")})"
|
45
|
+
}
|
46
|
+
|
47
|
+
if max_calcs.length == 1
|
48
|
+
max_calcs.first
|
49
|
+
else
|
50
|
+
"GREATEST(#{max_calcs.join(", ")})"
|
51
|
+
end
|
24
52
|
end
|
25
53
|
|
26
54
|
end
|
metadata
CHANGED
@@ -1,70 +1,68 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: greatest_updated_at
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 0
|
9
|
-
- 1
|
10
|
-
version: 0.0.1
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
11
5
|
platform: ruby
|
12
|
-
authors:
|
6
|
+
authors:
|
13
7
|
- James Prior
|
14
8
|
autorequire:
|
15
9
|
bindir: bin
|
16
10
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
11
|
+
date: 2015-02-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.2'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '5'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.2'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '5'
|
33
|
+
description: An AR extension to get the most recent updated_at from an active record
|
34
|
+
relation and it's included records
|
35
|
+
email:
|
24
36
|
- j.prior@asee.org
|
25
37
|
executables: []
|
26
|
-
|
27
38
|
extensions: []
|
28
|
-
|
29
39
|
extra_rdoc_files: []
|
30
|
-
|
31
|
-
files:
|
32
|
-
- lib/greatest_updated_at/calculations.rb
|
33
|
-
- lib/greatest_updated_at.rb
|
40
|
+
files:
|
34
41
|
- LICENSE
|
35
|
-
|
42
|
+
- lib/greatest_updated_at.rb
|
43
|
+
- lib/greatest_updated_at/calculations.rb
|
36
44
|
homepage: https://github.com/asee/greatest_updated_at
|
37
|
-
licenses:
|
38
|
-
|
45
|
+
licenses:
|
46
|
+
- GPL v2
|
47
|
+
metadata: {}
|
39
48
|
post_install_message:
|
40
49
|
rdoc_options: []
|
41
|
-
|
42
|
-
require_paths:
|
50
|
+
require_paths:
|
43
51
|
- lib
|
44
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
-
|
46
|
-
requirements:
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
47
54
|
- - ">="
|
48
|
-
- !ruby/object:Gem::Version
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
version: "0"
|
53
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
-
none: false
|
55
|
-
requirements:
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
56
59
|
- - ">="
|
57
|
-
- !ruby/object:Gem::Version
|
58
|
-
|
59
|
-
segments:
|
60
|
-
- 0
|
61
|
-
version: "0"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
62
|
requirements: []
|
63
|
-
|
64
63
|
rubyforge_project:
|
65
|
-
rubygems_version:
|
64
|
+
rubygems_version: 2.2.2
|
66
65
|
signing_key:
|
67
|
-
specification_version:
|
66
|
+
specification_version: 4
|
68
67
|
summary: Find the greatest updated_at from all included records in an AR scope
|
69
68
|
test_files: []
|
70
|
-
|