staugaard-magic_cache_keys 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :patch: 0
3
3
  :major: 0
4
- :minor: 5
4
+ :minor: 6
@@ -26,11 +26,19 @@ module ActiveRecord
26
26
 
27
27
  class << self
28
28
  def cache_key(options = {})
29
- order = options.delete(:order) || scope(:find, :order)
30
- opts = {:select => "MD5(CONCAT(GROUP_CONCAT(CONV(#{quoted_table_name}.#{connection.quote_column_name(primary_key)},10,36)#{ ' ORDER BY ' + order unless order.blank?}), MAX(#{table_name}.updated_at))) as cached_key"}.reverse_merge(options)
31
-
32
29
  connection.execute('SET group_concat_max_len = 1048576')
33
- "#{model_name.cache_key}/#{connection.select_value(construct_finder_sql(opts)) || 'empty'}"
30
+ if options[:conditions] && options[:conditions].is_a?(String) && options[:conditions].strip.upcase.starts_with?('SELECT')
31
+ hash_sql = options[:conditions].sub(/SELECT (\/\*.*?\*\/ )?(.*)\bFROM\b/im) { "SELECT #{cache_key_select} FROM" }
32
+ else
33
+ options[:select] = cache_key_select(options.delete(:order) || scope(:find, :order))
34
+ hash_sql = construct_finder_sql(options)
35
+ end
36
+
37
+ "#{model_name.cache_key}/#{connection.select_value(hash_sql) || 'empty'}"
38
+ end
39
+
40
+ def cache_key_select(order = nil)
41
+ "MD5(CONCAT(GROUP_CONCAT(CONV(#{quoted_table_name}.#{connection.quote_column_name(primary_key)},10,36)#{ ' ORDER BY ' + order unless order.blank?}), MAX(#{quoted_table_name}.#{connection.quote_column_name('updated_at')}))) as cached_key"
34
42
  end
35
43
 
36
44
  def has_many_with_cache_key(association_id, options = {}, &extension)
@@ -16,6 +16,7 @@ class Blog < ActiveRecord::Base
16
16
  has_many :posts_orderd1, :class_name => 'Post', :order => 'title DESC'
17
17
  has_many :posts_orderd2, :class_name => 'Post', :order => 'title ASC'
18
18
  has_many :posts_conditioned, :class_name => 'Post', :conditions => {:title => 'This post has a few comments'}
19
+ has_many :posts_by_sql, :class_name => "Post", :finder_sql => 'SELECT * FROM posts WHERE blog_id = #{id}'
19
20
  end
20
21
 
21
22
  class MagicCacheKeysTest < ActiveSupport::TestCase
@@ -112,4 +113,11 @@ class MagicCacheKeysTest < ActiveSupport::TestCase
112
113
  key2 = blogs(:a_blog).cache_key(:posts, :posts_orderd1)
113
114
  assert_equal(key1, key2)
114
115
  end
116
+
117
+ test "has_many associations with finder_sql generates a cache key" do
118
+ key1 = blogs(:a_blog).posts_by_sql.cache_key
119
+ assert_not_nil(key1)
120
+ key2 = blogs(:a_blog).posts.cache_key
121
+ assert_equal(key1, key2)
122
+ end
115
123
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: staugaard-magic_cache_keys
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mick Staugaard