activerecord-cached_at 2.0.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d2078ad2e295f56f2163f9f813cc3bd87796c0bf
4
- data.tar.gz: 90fb34c29372d3cb83ed043777ac3cb137c79961
3
+ metadata.gz: 4c42dbd764f16740cea245628f15f8b9129496e5
4
+ data.tar.gz: 196b05aaefbe91f7ec3f8391c29222592f905858
5
5
  SHA512:
6
- metadata.gz: 6507b89f69f5172ee7d1e8c0f30b64e72764ad6b4a3f853765eeb139df0ae55612f8671acb11fed693593fd7b39d1a4bfb17c4b51c4da67640bd7e0f61ba6969
7
- data.tar.gz: 35b31f8fcafc9a939328da0b163fdf964ceec28d1f627e80c2d0581de642515d19bb11f1300e4e84e59d8fbd51253faf8bda8cd44795c57ca8e96128ff085af8
6
+ metadata.gz: 2ef7c98033ad0dd16327b86f171a4964e213dffdf3eaf3724c46675b3dec52284029bccf883131508207a0bef75bb833578d7f7aeee3a2ebbac7834f11e5ca4c
7
+ data.tar.gz: cac649ad4254af80454ed404ca285934e7095c91adde2693bed64aac5dee4645f4db3c48f8dc4c16250e4bd7c9de883d0f4c03b3a8af53b15a9835691826cef0
@@ -31,7 +31,7 @@ module CachedAt
31
31
  end
32
32
 
33
33
  def touch_records_added_cached_at(records, timestamp)
34
- return if records.empty?
34
+ return if owner.new_record? || records.empty?
35
35
 
36
36
  using_reflection = reflection.parent_reflection || reflection
37
37
 
@@ -45,6 +45,7 @@ module CachedAt
45
45
  cache_column = "#{using_reflection.inverse_of.name}_cached_at"
46
46
  ids = records.inject([]) { |a, o| a += [o.send(klass.primary_key), o.send("#{klass.primary_key}_was")] }.compact.uniq
47
47
  query = klass.where(klass.primary_key => ids)
48
+ records.each { |r| r.raw_write_attribute(cache_column, timestamp) }
48
49
  query.update_all({ cache_column => timestamp })
49
50
  traverse_relationships(klass, using_reflection.options[:cached_at], query, cache_column, timestamp)
50
51
  end
@@ -70,6 +71,7 @@ module CachedAt
70
71
  cache_column = "#{inverse_reflection.name}_cached_at"
71
72
  ids = records.inject([]) { |a, o| a += [o.send(klass.primary_key), o.send("#{klass.primary_key}_was")] }.compact.uniq
72
73
  query = klass.where(klass.primary_key => ids)
74
+ records.each { |r| r.raw_write_attribute(cache_column, timestamp) }
73
75
  query.update_all({ cache_column => timestamp })
74
76
  traverse_relationships(klass, reflection.options[:cached_at], query, cache_column, timestamp)
75
77
  end
@@ -78,20 +80,23 @@ module CachedAt
78
80
  cache_column = "#{using_reflection.name}_cached_at"
79
81
  owner.raw_write_attribute(cache_column, timestamp)
80
82
  ids = records.inject([]) { |a, o| a += [o.send(klass.primary_key), o.send("#{klass.primary_key}_was")] }.compact.uniq
81
-
83
+
82
84
  arel_table = if inverse_reflection.is_a?(ActiveRecord::Reflection::HasAndBelongsToManyReflection)
83
85
  inverse_reflection.klass.const_get(:"HABTM_#{using_reflection.name.to_s.camelize}").arel_table
84
86
  else
85
87
  using_reflection.inverse_of.klass._reflections[using_reflection.inverse_of.options[:through].to_s].klass.arel_table
86
88
  end
87
- query = if inverse_reflection.is_a?(ActiveRecord::Reflection::HasAndBelongsToManyReflection)
88
- using_reflection.inverse_of.klass.joins(inverse_reflection.join_table)
89
+ query = nil
90
+ if inverse_reflection.is_a?(ActiveRecord::Reflection::HasAndBelongsToManyReflection)
91
+ query = using_reflection.inverse_of.klass.joins(inverse_reflection.inverse_of.options[:through])
92
+ query = query.where(arel_table[inverse_reflection.foreign_key].in(ids))
89
93
  else
90
- using_reflection.inverse_of.klass.joins(using_reflection.inverse_of.options[:through])
94
+ query = using_reflection.inverse_of.klass.joins(using_reflection.inverse_of.options[:through])
95
+ query = query.where(arel_table[using_reflection.foreign_key].in(ids))
91
96
  end
92
-
93
- query.where(arel_table[using_reflection.inverse_of.foreign_key].in(ids))
97
+
94
98
  query.update_all({ cache_column => timestamp })
99
+ owner.raw_write_attribute(cache_column, timestamp)
95
100
  traverse_relationships(klass, reflection.options[:cached_at], query, cache_column, timestamp)
96
101
  end
97
102
  end
@@ -42,7 +42,19 @@ module CachedAt
42
42
  timestamp ||= current_time_from_proper_timezone
43
43
 
44
44
  self._reflections.each do |name, reflection|
45
- association(name.to_sym).touch_cached_at(timestamp, method)
45
+ begin
46
+ association(name.to_sym).touch_cached_at(timestamp, method)
47
+ rescue ActiveRecord::HasManyThroughAssociationNotFoundError,
48
+ ActiveRecord::HasOneAssociationPolymorphicThroughError,
49
+ ActiveRecord::HasManyThroughAssociationPolymorphicThroughError,
50
+ ActiveRecord::HasManyThroughSourceAssociationNotFoundError,
51
+ ActiveRecord::HasManyThroughAssociationPointlessSourceTypeError,
52
+ ActiveRecord::HasManyThroughAssociationPolymorphicSourceError,
53
+ ActiveRecord::HasOneThroughCantAssociateThroughCollection
54
+ # these error get raised if the reflection is invalid... so we'll
55
+ # skip them. Should warn the user, but this casuse the Rails test
56
+ # to fail....
57
+ end
46
58
  end
47
59
  end
48
60
 
@@ -1,3 +1,3 @@
1
1
  module CachedAt
2
- VERSION = '2.0.0'
2
+ VERSION = '2.0.1'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-cached_at
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jon Bracy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-04 00:00:00.000000000 Z
11
+ date: 2017-01-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord