constant_table_saver 4.0.0 → 4.1.0

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: 250b25d7f3fefffc880e4f1ed9bd349aaa9ed601
4
- data.tar.gz: bc75e2890206f99c0b0bdd049921afca4a55b492
3
+ metadata.gz: 40e0df9904accfe6d49e57b9e3e034fecf0176d7
4
+ data.tar.gz: 05b44aebb7aa1789b8a91977dd7b3985a7c8fe32
5
5
  SHA512:
6
- metadata.gz: 7d580056718eac74024e54d0cca28c0219551b9407e6d2540fa753117fd4c8e41d77c99180c009197c7a3e26432a2efaa05894c3a3d4c5de62a5717708be1237
7
- data.tar.gz: baebaf8845f8ca7de92ea243bca01e5e69624d79f293b6b9415e80db9da39c2acbb54d9acf03baf700e2933a40bca563eabcf14de5f9eccd00f3c2aacf98a1a1
6
+ metadata.gz: d625dda9b46ec8a8d6aae3f90f6eb3dbf8815c370429aa57a593f0867203891e64a472fc80b697cb25fdb8affbf8fa247f2f37fa496cc25fbba20cbd47a2519a
7
+ data.tar.gz: 5b3ee90f3f1505c264605a2514cc6a53f68259f3968ad2422d7b55bc212ae9f84ac53563b6aa27203e862816ac789c7c1969b985144cc1adb7b0d710bedcaeeb
@@ -1,3 +1,3 @@
1
1
  module ConstantTableSaver
2
- VERSION = '4.0.0'
2
+ VERSION = '4.1.0'
3
3
  end
@@ -51,18 +51,16 @@ module ConstantTableSaver
51
51
 
52
52
  module ActiveRecord4ClassMethods
53
53
  def find_by_sql(sql, binds = [])
54
- @find_by_sql ||= {}
55
- @find_by_sql[:all] ||= all.to_sql
56
- @find_by_sql[:id] ||= relation.where(relation.table[primary_key].eq(connection.substitute_at(columns_hash[primary_key], 1))).limit(1).to_sql
57
- @find_by_sql[:oldid] ||= relation.where(relation.table[primary_key].eq(connection.substitute_at(columns_hash[primary_key], 1))).
58
- order(relation.table[primary_key].asc).limit(1).to_sql # used by 4.0
59
- @find_by_sql[:first] ||= relation.order(relation.table[primary_key].asc).limit(1).to_sql
60
- @find_by_sql[:last] ||= relation.order(relation.table[primary_key].desc).limit(1).to_sql
61
-
62
- _sql = sanitize_sql(sql)
63
- _sql = _sql.to_sql if sql.respond_to?(:to_sql)
54
+ @find_by_sql ||= {
55
+ :all => all.to_sql,
56
+ :id => relation.where(relation.table[primary_key].eq(connection.substitute_at(columns_hash[primary_key], 1))).limit(1),
57
+ :first => relation.order(relation.table[primary_key].asc).limit(1).to_sql,
58
+ :last => relation.order(relation.table[primary_key].desc).limit(1).to_sql,
59
+ }
64
60
 
65
61
  if binds.empty?
62
+ _sql = _to_sql_with_binds(sql, binds)
63
+
66
64
  if _sql == @find_by_sql[:all]
67
65
  return @cached_records ||= super(relation.to_sql).each(&:freeze)
68
66
  elsif _sql == @find_by_sql[:first]
@@ -71,10 +69,10 @@ module ConstantTableSaver
71
69
  return [relation.to_a.last].compact
72
70
  end
73
71
 
74
- elsif (_sql == @find_by_sql[:id] || _sql == @find_by_sql[:oldid]) &&
75
- binds.length == 1 &&
72
+ elsif binds.length == 1 &&
76
73
  binds.first.first.is_a?(ActiveRecord::ConnectionAdapters::Column) &&
77
- binds.first.first.name == primary_key
74
+ binds.first.first.name == primary_key &&
75
+ _to_sql_with_binds(sql, binds) == _to_sql_with_binds(@find_by_sql[:id], binds) # we have to late-render the find(id) SQL because mysql2 on 4.1 and later requires the bind variables to render the SQL, and errors out with a nil dereference otherwise
78
76
  @cached_records_by_id ||= relation.to_a.index_by {|record| record.id.to_param}
79
77
  return [@cached_records_by_id[binds.first.last.to_param]].compact
80
78
  end
@@ -82,6 +80,16 @@ module ConstantTableSaver
82
80
  super
83
81
  end
84
82
 
83
+ def _to_sql_with_binds(sql, binds)
84
+ if sql.respond_to?(:to_sql)
85
+ # an arel object
86
+ connection.to_sql(sql, binds)
87
+ else
88
+ # a plain string
89
+ sql
90
+ end
91
+ end
92
+
85
93
  def relation
86
94
  super.tap do |s|
87
95
  class << s
data/test/database.yml CHANGED
@@ -1,3 +1,14 @@
1
- test:
1
+ sqlite3:
2
2
  adapter: sqlite3
3
3
  database: test/constant_table_saver.db
4
+ mysql:
5
+ adapter: mysql
6
+ database: constant_table_saver_test
7
+ username: root
8
+ mysql2:
9
+ adapter: mysql2
10
+ database: constant_table_saver_test
11
+ username: root
12
+ postgresql:
13
+ adapter: postgresql
14
+ database: constant_table_saver_test
data/test/test_helper.rb CHANGED
@@ -11,7 +11,7 @@ require 'active_record'
11
11
  require 'active_record/fixtures'
12
12
  require 'byebug' rescue nil
13
13
 
14
- RAILS_ENV = ENV['RAILS_ENV'] ||= 'test'
14
+ RAILS_ENV = ENV['RAILS_ENV'] ||= 'sqlite3'
15
15
 
16
16
  ActiveRecord::Base.configurations = YAML::load(IO.read(File.join(File.dirname(__FILE__), "database.yml")))
17
17
  ActiveRecord::Base.establish_connection ActiveRecord::Base.configurations[ENV['RAILS_ENV']]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: constant_table_saver
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.0
4
+ version: 4.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Will Bryant
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-18 00:00:00.000000000 Z
11
+ date: 2014-08-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord