constant_table_saver 4.0.0 → 4.1.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.
- checksums.yaml +4 -4
- data/lib/constant_table_saver/version.rb +1 -1
- data/lib/constant_table_saver.rb +21 -13
- data/test/database.yml +12 -1
- data/test/test_helper.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 40e0df9904accfe6d49e57b9e3e034fecf0176d7
|
4
|
+
data.tar.gz: 05b44aebb7aa1789b8a91977dd7b3985a7c8fe32
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d625dda9b46ec8a8d6aae3f90f6eb3dbf8815c370429aa57a593f0867203891e64a472fc80b697cb25fdb8affbf8fa247f2f37fa496cc25fbba20cbd47a2519a
|
7
|
+
data.tar.gz: 5b3ee90f3f1505c264605a2514cc6a53f68259f3968ad2422d7b55bc212ae9f84ac53563b6aa27203e862816ac789c7c1969b985144cc1adb7b0d710bedcaeeb
|
data/lib/constant_table_saver.rb
CHANGED
@@ -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
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
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
|
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
|
-
|
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'] ||= '
|
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.
|
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-
|
11
|
+
date: 2014-08-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|