constant_table_saver 5.1.2 → 5.2.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
- SHA1:
3
- metadata.gz: 952a6f9f15aa9905df69f3dcb279391f55a5abde
4
- data.tar.gz: ac94f3acbf0e194aad02e91fb4c45ed16f2594d3
2
+ SHA256:
3
+ metadata.gz: 047fc14118f3907b7803464acb83fbf2675dcc8888b07dda5f5fdd9849a4fab3
4
+ data.tar.gz: bae7b8bee9a592941efec27891596c638ee8f4fe9dfa5fe37f6573824e07d370
5
5
  SHA512:
6
- metadata.gz: 278138ada54ee955e1a29b58505959e772e4db8468d4835393502912e8d91ecd7766cc1347f40afcfece79892b3419ce93580a15f2a3f6ff57a177a7a42e6dcf
7
- data.tar.gz: 6367660c3f7a614fa90357558fa93f8ced9a3a1b3a26a9486a48c05c496a96ff8a6acf661aac9990d8914220d17d835d3adf8dddf02be15a7601a0b3f3776982
6
+ metadata.gz: 7c664569302baa751ecf4b4fb3aff40fab9f003dce1a965c1d0cb927124faa9ef69c3a041ec243c01f810c307c00b3e37aec2fedf27d7afa35fe2659e06605b5
7
+ data.tar.gz: 5678f53bf835867af37b174ae9250e5457a4ebd0a1b5816ad44947497e69c56d2e5097cf4434aea9a8f99521a4a7ffc455afef1ebfd5806506747bd4a132bf33
data/Dockerfile ADDED
@@ -0,0 +1,46 @@
1
+ # this Dockerfile is used only to get a consistent environment in which to run the tests (test_all.rb),
2
+ # it has nothing to do with the actual gem that gets published.
3
+
4
+ # use an older version of ubuntu so that we get a version of postgresql so is supported by the older rails
5
+ # versions, or we won't be able to test out support for them.
6
+ FROM ubuntu:16.04
7
+
8
+ # this dockerfile builds kitchen sync on ubuntu and runs the test suite.
9
+
10
+ RUN DEBIAN_FRONTEND=noninteractive apt-get update && \
11
+ DEBIAN_FRONTEND=noninteractive apt-get upgrade -y && \
12
+ DEBIAN_FRONTEND=noninteractive apt-get install -y build-essential git \
13
+ mysql-server libmysqlclient-dev postgresql-9.5 libpq-dev libsqlite3-dev \
14
+ ruby ruby-dev && \
15
+ rm -f /etc/apt/apt.conf.d/20auto-upgrades && \
16
+ apt-get clean -y && \
17
+ rm -rf /var/cache/apt/archives/*
18
+
19
+ RUN gem install bundler --no-ri --no-rdoc
20
+
21
+ # install a version of mysql2 that the older versions of rails are compatible with
22
+ RUN gem install mysql2 -v 0.4.10
23
+
24
+ WORKDIR /tmp
25
+ ADD Gemfile Gemfile
26
+ ADD constant_table_saver.gemspec constant_table_saver.gemspec
27
+ ADD lib/constant_table_saver/version.rb lib/constant_table_saver/version.rb
28
+ RUN bundle install -j 4
29
+
30
+ ADD . .
31
+ RUN echo 'starting postgresql' && \
32
+ service postgresql start && \
33
+ echo 'creating postgresql database' && \
34
+ su postgres -c 'createdb --encoding unicode --template template0 constant_table_saver_test' && \
35
+ echo 'creating postgresql user' && \
36
+ su postgres -c 'createuser --superuser root' && \
37
+ echo 'starting mysql' && \
38
+ mkdir -p /var/run/mysqld && \
39
+ chown mysql:mysql /var/run/mysqld && \
40
+ (/usr/sbin/mysqld --skip-grant-tables &) && \
41
+ echo 'waiting for mysql to start' && \
42
+ mysqladmin --wait=30 ping && \
43
+ echo 'creating mysql database' && \
44
+ mysqladmin create constant_table_saver_test && \
45
+ echo 'running tests' && \
46
+ ./test_all.rb
data/README.md CHANGED
@@ -11,7 +11,7 @@ named after the name field you specify.
11
11
  Compatibility
12
12
  =============
13
13
 
14
- Currently tested against Rails 5.1 (up to 5.1.6), 5.0 (up to 5.0.6), and 4.2 (up to 4.2.10), on Ruby 2.3.6, 2.4.3, and 2.5.0.
14
+ Currently tested against Rails 5.2 (5.2.0), 5.1 (up to 5.1.6) and 5.0 (up to 5.0.6) and 4.2 (up to 4.2.10).
15
15
 
16
16
  For earlier versions of Rails, use an older version of the gem.
17
17
 
data/Rakefile CHANGED
@@ -13,4 +13,5 @@ Rake::TestTask.new(:test) do |t|
13
13
  t.libs << 'test'
14
14
  t.pattern = 'test/*_test.rb'
15
15
  t.verbose = true
16
+ t.warning = false
16
17
  end
@@ -16,7 +16,7 @@ named after the name field you specify.
16
16
  Compatibility
17
17
  =============
18
18
 
19
- Currently tested against Rails 5.1 (5.1.0beta2) and 5.0 (up to 5.0.2) and 4.2 (up to 4.2.7), on Ruby 2.3.4.
19
+ Currently tested against Rails 5.2 (5.2.0), 5.1 (up to 5.1.6) and 5.0 (up to 5.0.6) and 4.2 (up to 4.2.10).
20
20
 
21
21
  For earlier versions of Rails, use an older version of the gem.
22
22
  EOF
@@ -12,8 +12,10 @@ module ConstantTableSaver
12
12
 
13
13
  if ActiveRecord::VERSION::MAJOR == 4
14
14
  extend ActiveRecord4ClassMethods
15
- else
15
+ elsif ActiveRecord::VERSION::MINOR == 0 || ActiveRecord::VERSION::MINOR == 1
16
16
  extend ActiveRecord5ClassMethods
17
+ else
18
+ extend ActiveRecord52ClassMethods
17
19
  end
18
20
  extend ClassMethods
19
21
  extend NameClassMethods if constant_table_options[:name]
@@ -52,16 +54,6 @@ module ConstantTableSaver
52
54
  @cached_records = @cached_records_by_id = @constant_record_methods = @cached_blank_scope = @find_by_sql = nil
53
55
  end
54
56
 
55
- def _to_sql_with_binds(sql, binds)
56
- if sql.respond_to?(:to_sql)
57
- # an arel object
58
- connection.to_sql(sql, binds)
59
- else
60
- # a plain string
61
- sql
62
- end
63
- end
64
-
65
57
  def relation
66
58
  super.tap do |s|
67
59
  class << s
@@ -77,6 +69,40 @@ module ConstantTableSaver
77
69
  end
78
70
  end
79
71
 
72
+ module ActiveRecord52ClassMethods
73
+ def find_by_sql(sql, binds = [], preparable: nil, &block)
74
+ @cached_records ||= super(relation.to_sql).each(&:freeze).freeze
75
+
76
+ @cached_results ||= @cached_records.each_with_object({
77
+ # matches .all queries:
78
+ to_sql_and_binds(relation) => @cached_records,
79
+
80
+ # matches .first queries:
81
+ to_sql_and_binds(relation.order(relation.table[primary_key].asc).limit(1).arel) => [@cached_records.first].compact,
82
+
83
+ # matches .last queries:
84
+ to_sql_and_binds(relation.order(relation.table[primary_key].desc).limit(1).arel) => [@cached_records.last].compact,
85
+ }) do |record, results|
86
+ results[to_sql_and_binds(relation.where(relation.table[primary_key].eq(relation.predicate_builder.build_bind_attribute(primary_key, record.id))).limit(1).arel)] = [record]
87
+ end.freeze
88
+
89
+ sql_and_binds = to_sql_and_binds(sql, binds)
90
+ sql_and_binds = [sql_and_binds.first, []] unless connection.prepared_statements
91
+
92
+ if results = @cached_results[sql_and_binds]
93
+ results
94
+ else
95
+ super(sql, binds, preparable: preparable, &block)
96
+ end
97
+ end
98
+
99
+ private
100
+ def to_sql_and_binds(sql, binds = [])
101
+ sql = sql.arel if sql.respond_to?(:arel)
102
+ connection.send(:to_sql_and_binds, sql, binds)
103
+ end
104
+ end
105
+
80
106
  module ActiveRecord5ClassMethods
81
107
  def find_by_sql(sql, binds = [], preparable: nil, &block)
82
108
  @find_by_sql ||= {
@@ -114,6 +140,16 @@ module ConstantTableSaver
114
140
 
115
141
  super(sql, binds, preparable: preparable, &block)
116
142
  end
143
+
144
+ def _to_sql_with_binds(sql, binds)
145
+ if sql.respond_to?(:to_sql)
146
+ # an arel object
147
+ connection.to_sql(sql, binds)
148
+ else
149
+ # a plain string
150
+ sql
151
+ end
152
+ end
117
153
  end
118
154
 
119
155
  module ActiveRecord4ClassMethods
@@ -148,6 +184,16 @@ module ConstantTableSaver
148
184
 
149
185
  super
150
186
  end
187
+
188
+ def _to_sql_with_binds(sql, binds)
189
+ if sql.respond_to?(:to_sql)
190
+ # an arel object
191
+ connection.to_sql(sql, binds)
192
+ else
193
+ # a plain string
194
+ sql
195
+ end
196
+ end
151
197
  end
152
198
 
153
199
  module NameClassMethods
@@ -1,3 +1,3 @@
1
1
  module ConstantTableSaver
2
- VERSION = '5.1.2'
2
+ VERSION = '5.2.0'
3
3
  end
@@ -152,6 +152,7 @@ class ConstantTableSaverTest < ActiveSupport::TestCase
152
152
  @pies = StandardPie.select("id").all.to_a
153
153
  @pie = StandardPie.select("id").find(1)
154
154
  @second_pie = StandardPie.select("id").find(2)
155
+ ConstantPie.first
155
156
  assert_queries(3) do
156
157
  assert_equal @pies.collect(&:attributes), ConstantPie.select("id").all.collect(&:attributes)
157
158
  assert_equal @pie.attributes, ConstantPie.select("id").find(1).attributes
@@ -177,10 +178,11 @@ class ConstantTableSaverTest < ActiveSupport::TestCase
177
178
  ConstantPie.all.to_a
178
179
  ConstantPie.reset_constant_record_cache!
179
180
 
180
- assert_queries(1) do
181
- @pies = ConstantPie.where(:filling => ["Tasty beef steak"]).retrieve_pies
182
- end
181
+ # retrieve_pies here is to test the behavior of scoped class methods:
182
+ @pies = ConstantPie.where(:filling => ["Tasty beef steak"]).retrieve_pies
183
183
  assert_equal 1, @pies.size
184
+
185
+ assert_equal StandardPie.count, ConstantPie.all.to_a.size
184
186
  end
185
187
 
186
188
  test "it passes the options preventing caching to the underlying query methods" do
data/test_all.rb CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'yaml'
4
4
 
5
- rails_versions = ["5.1.1".."5.1.6", "5.0.2".."5.0.6", "4.2.10"].flat_map {|spec| Array(spec).collect {|v| v.gsub /.0(\d)/, '.\\1'}}
5
+ rails_versions = ["5.2.0", "5.1.1".."5.1.6", "5.0.2".."5.0.6", "4.2.10"].flat_map {|spec| Array(spec).collect {|v| v.gsub /.0(\d)/, '.\\1'}}
6
6
  rails_envs = YAML.load(File.read("test/database.yml")).keys
7
7
 
8
8
  rails_versions.each do |version|
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: 5.1.2
4
+ version: 5.2.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: 2018-06-04 00:00:00.000000000 Z
11
+ date: 2018-07-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -105,7 +105,7 @@ description: |
105
105
  Compatibility
106
106
  =============
107
107
 
108
- Currently tested against Rails 5.1 (5.1.0beta2) and 5.0 (up to 5.0.2) and 4.2 (up to 4.2.7), on Ruby 2.3.4.
108
+ Currently tested against Rails 5.2 (5.2.0), 5.1 (up to 5.1.6) and 5.0 (up to 5.0.6) and 4.2 (up to 4.2.10).
109
109
 
110
110
  For earlier versions of Rails, use an older version of the gem.
111
111
  email: will.bryant@gmail.com
@@ -114,6 +114,7 @@ extensions: []
114
114
  extra_rdoc_files: []
115
115
  files:
116
116
  - ".gitignore"
117
+ - Dockerfile
117
118
  - Gemfile
118
119
  - MIT-LICENSE
119
120
  - README.md
@@ -150,7 +151,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
150
151
  version: '0'
151
152
  requirements: []
152
153
  rubyforge_project:
153
- rubygems_version: 2.5.2
154
+ rubygems_version: 2.7.6
154
155
  signing_key:
155
156
  specification_version: 4
156
157
  summary: Caches the records from fixed tables, and provides convenience methods to
@@ -163,4 +164,3 @@ test_files:
163
164
  - test/fixtures/pies.yml
164
165
  - test/schema.rb
165
166
  - test/test_helper.rb
166
- has_rdoc: false