activerecord-tablefree 3.1.7 → 3.1.9

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.
@@ -4,6 +4,11 @@ module ActiveRecord::Tablefree
4
4
  true
5
5
  end
6
6
 
7
+ # Needed for Rails 5.2 when using numericality validator
8
+ def value_constructed_by_mass_assignment?(_value)
9
+ false
10
+ end
11
+
7
12
  # Needed for Rails 5.0
8
13
  def serialize(args)
9
14
  args
@@ -17,6 +22,16 @@ module ActiveRecord::Tablefree
17
22
  args
18
23
  end
19
24
 
25
+ # Needed for ActiveRecord 7.2+
26
+ def serialized?
27
+ false
28
+ end
29
+
30
+ # Needed for ActiveRecord 7.2+
31
+ def mutable?
32
+ false
33
+ end
34
+
20
35
  def changed?(*_args)
21
36
  false
22
37
  end
@@ -27,7 +42,7 @@ module ActiveRecord::Tablefree
27
42
 
28
43
  def force_equality?(value)
29
44
  case value
30
- when ::Array, ::Range then
45
+ when ::Array, ::Range
31
46
  true
32
47
  else
33
48
  false
@@ -1,13 +1,13 @@
1
1
  module ActiveRecord::Tablefree
2
2
  class Connection < ActiveRecord::ConnectionAdapters::AbstractAdapter
3
3
  def initialize
4
- @connection = Object.new # The Raw Connection
5
- @owner = nil
6
- @instrumenter = ActiveSupport::Notifications.instrumenter
7
- @logger = Object.new
8
- @config = Object.new
9
- @pool = nil
10
- @schema_cache = ActiveRecord::Tablefree::SchemaCache.new
4
+ @connection = Object.new # The Raw Connection
5
+ @owner = nil
6
+ @instrumenter = ActiveSupport::Notifications.instrumenter
7
+ @logger = Object.new
8
+ @config = Object.new
9
+ @pool = nil
10
+ @schema_cache = ActiveRecord::Tablefree::SchemaCache.new
11
11
  @quoted_column_names, @quoted_table_names = {}, {}
12
12
  @visitor = Object.new
13
13
  @lock = Object.new
@@ -15,11 +15,11 @@ module ActiveRecord::Tablefree
15
15
  end
16
16
 
17
17
  def quote_table_name(*_args)
18
- ''
18
+ ""
19
19
  end
20
20
 
21
21
  def quote_column_name(*_args)
22
- ''
22
+ ""
23
23
  end
24
24
 
25
25
  def substitute_at(*_args)
@@ -41,8 +41,8 @@ module ActiveRecord::Tablefree
41
41
  def sanitize_limit(limit)
42
42
  if limit.is_a?(Integer) || limit.is_a?(Arel::Nodes::SqlLiteral)
43
43
  limit
44
- elsif limit.to_s.include?(',')
45
- Arel.sql limit.to_s.split(',').map { |i| Integer(i) }.join(',')
44
+ elsif limit.to_s.include?(",")
45
+ Arel.sql limit.to_s.split(",").map { |i| Integer(i) }.join(",")
46
46
  else
47
47
  Integer(limit)
48
48
  end
@@ -52,10 +52,18 @@ module ActiveRecord::Tablefree
52
52
  # Returns a SQL string with the from, join, where, and having clauses,
53
53
  # in addition to the limit and offset.
54
54
  def combine_bind_parameters(**_args)
55
- ''
55
+ ""
56
56
  end
57
57
 
58
- def lookup_cast_type_from_column(*_args)
58
+ def lookup_cast_type_from_column(column = nil, *_args)
59
+ type = column.respond_to?(:type) ? column.type : nil
60
+ if type
61
+ # ActiveModel's registry needs no adapter (and so no database connection).
62
+ ActiveModel::Type.lookup(type)
63
+ else
64
+ @_cast_type ||= ActiveRecord::Tablefree::CastType.new
65
+ end
66
+ rescue ArgumentError
59
67
  @_cast_type ||= ActiveRecord::Tablefree::CastType.new
60
68
  end
61
69
 
@@ -67,6 +75,15 @@ module ActiveRecord::Tablefree
67
75
  {}
68
76
  end
69
77
 
78
+ # Tablefree models never persist, so transactions just run their block.
79
+ def transaction(*_args, **_kwargs)
80
+ yield
81
+ end
82
+
83
+ def pool
84
+ @_pool ||= ActiveRecord::Tablefree::ConnectionPool.new(self)
85
+ end
86
+
70
87
  # This is used in the StatementCache object.
71
88
  def cacheable_query(arel) # :nodoc:
72
89
  ActiveRecord::Tablefree::StatementCache.partial_query visitor, arel.ast, collector
@@ -0,0 +1,28 @@
1
+ module ActiveRecord::Tablefree
2
+ # Minimal stand-in for ActiveRecord's connection pool. Newer ActiveRecord
3
+ # (7.2+) reaches the connection through the pool for scoping, alias
4
+ # tracking, and transactions.
5
+ class ConnectionPool
6
+ attr_reader :connection
7
+
8
+ def initialize(connection)
9
+ @connection = connection
10
+ end
11
+
12
+ def lease_connection
13
+ connection
14
+ end
15
+
16
+ def with_connection(**)
17
+ yield connection
18
+ end
19
+
20
+ def with_pool_transaction_isolation_level(*)
21
+ yield
22
+ end
23
+
24
+ def schema_cache
25
+ connection.schema_cache
26
+ end
27
+ end
28
+ end
@@ -2,6 +2,12 @@
2
2
 
3
3
  module ActiveRecord
4
4
  module Tablefree
5
- VERSION = '3.1.7'.freeze
5
+ # Version namespace for this gem.
6
+ module Version
7
+ # Current gem version.
8
+ VERSION = "3.1.9"
9
+ end
10
+ # Current gem version exposed at the traditional constant location.
11
+ VERSION = Version::VERSION # Traditional Constant Location
6
12
  end
7
13
  end
@@ -1,11 +1,13 @@
1
- require 'cgi'
2
- require 'active_record'
1
+ require_relative "tablefree/version"
3
2
 
4
- require 'activerecord/tablefree/version'
5
- require 'activerecord/tablefree/cast_type'
6
- require 'activerecord/tablefree/schema_cache'
7
- require 'activerecord/tablefree/connection'
8
- require 'activerecord/tablefree/transaction'
3
+ require "cgi"
4
+ require "active_record"
5
+
6
+ require "activerecord/tablefree/cast_type"
7
+ require "activerecord/tablefree/schema_cache"
8
+ require "activerecord/tablefree/connection"
9
+ require "activerecord/tablefree/connection_pool"
10
+ require "activerecord/tablefree/transaction"
9
11
 
10
12
  module ActiveRecord
11
13
  # = ActiveRecord::Tablefree
@@ -45,18 +47,19 @@ module ActiveRecord
45
47
  module ActsMethods #:nodoc:
46
48
  # A model that needs to be tablefree will call this method to indicate
47
49
  # it.
48
- def has_no_table(options = { database: :fail_fast })
50
+ def has_no_table(options = {database: :fail_fast})
49
51
  raise ArgumentError, "Invalid database option '#{options[:database]}'" unless %i[fail_fast pretend_success].member? options[:database]
50
52
  # keep our options handy
51
53
  class_attribute :tablefree_options
52
54
  self.tablefree_options = {
53
55
  database: options[:database],
54
- columns_hash: {}
56
+ columns_hash: {},
57
+ cast_types: {}
55
58
  }
56
59
 
57
60
  # extend
58
- extend ActiveRecord::Tablefree::SingletonMethods
59
- extend ActiveRecord::Tablefree::ClassMethods
61
+ extend ActiveRecord::Tablefree::SingletonMethods
62
+ extend ActiveRecord::Tablefree::ClassMethods
60
63
 
61
64
  # include
62
65
  include ActiveRecord::Tablefree::InstanceMethods
@@ -78,7 +81,8 @@ module ActiveRecord
78
81
  @columns_hash.each do |name, column|
79
82
  define_attribute(
80
83
  name,
81
- connection.lookup_cast_type_from_column(column),
84
+ # Hash#fetch, not Thread#fetch.
85
+ tablefree_options[:cast_types].fetch(name) { column.sql_type_metadata }, # rubocop:disable Lint/LtsRuby/UnavailableMethod
82
86
  default: column.default,
83
87
  user_provided_default: false
84
88
  )
@@ -86,11 +90,32 @@ module ActiveRecord
86
90
  end
87
91
 
88
92
  # Register a new column.
89
- def column(name, sql_type = nil, default = nil, null = true)
90
- cast_class = "ActiveRecord::Type::#{sql_type.to_s.camelize}".constantize rescue nil
93
+ def column(name, sql_type = nil, default = nil, null = true, cast_class_params = nil)
94
+ cast_class = if sql_type.is_a?(Class) && sql_type < ActiveModel::Type::Value
95
+ sql_type
96
+ else
97
+ begin
98
+ "ActiveRecord::Type::#{sql_type.to_s.camelize}".constantize
99
+ rescue
100
+ nil
101
+ end
102
+ end
91
103
  raise InvalidColumnType, "sql_type is #{sql_type} (#{sql_type.class}), which is not supported" unless cast_class.respond_to?(:new)
92
- cast_type = cast_class.new
93
- tablefree_options[:columns_hash][name.to_s] = ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, cast_type, sql_type.to_s, null)
104
+ cast_type = cast_class.new(*cast_class_params)
105
+ tablefree_options[:cast_types][name.to_s] = cast_type
106
+ # Hash#[]= and String#to_s on column names, not Matrix/Vector#[]= or Set#to_s.
107
+ # rubocop:disable Lint/LtsRuby/UnavailableMethod
108
+ tablefree_options[:columns_hash][name.to_s] =
109
+ if ActiveRecord.gem_version >= Gem::Version.new("8.1")
110
+ ActiveRecord::ConnectionAdapters::Column.new(name.to_s, cast_type, default, nil, null)
111
+ elsif ActiveRecord::VERSION::MAJOR >= 6
112
+ type_name = cast_type.respond_to?(:type) ? cast_type.type : nil
113
+ metadata = type_name && ActiveRecord::ConnectionAdapters::SqlTypeMetadata.new(sql_type: type_name.to_s, type: type_name)
114
+ ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, metadata, null)
115
+ else
116
+ ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, cast_type, null)
117
+ end
118
+ # rubocop:enable Lint/LtsRuby/UnavailableMethod
94
119
  end
95
120
 
96
121
  # Register a set of columns with the same SQL type
@@ -109,6 +134,12 @@ module ActiveRecord
109
134
  end
110
135
  end
111
136
 
137
+ def all(*_args)
138
+ return [] if tablefree_options[:database] == :pretend_success
139
+
140
+ super
141
+ end
142
+
112
143
  def destroy_all(*_args)
113
144
  case tablefree_options[:database]
114
145
  when :pretend_success
@@ -118,18 +149,13 @@ module ActiveRecord
118
149
  end
119
150
  end
120
151
 
121
- case ActiveRecord::VERSION::MAJOR
122
- when 5
123
- def find_by_sql(*_args)
124
- case tablefree_options[:database]
125
- when :pretend_success
126
- []
127
- when :fail_fast
128
- raise NoDatabase, "Can't #find_by_sql on Tablefree class"
129
- end
152
+ def find_by_sql(*_args)
153
+ case tablefree_options[:database]
154
+ when :pretend_success
155
+ []
156
+ when :fail_fast
157
+ raise NoDatabase, "Can't #find_by_sql on Tablefree class"
130
158
  end
131
- else
132
- raise Unsupported, 'Unsupported ActiveRecord version'
133
159
  end
134
160
 
135
161
  def transaction
@@ -156,9 +182,9 @@ module ActiveRecord
156
182
  if query_string.blank?
157
183
  new
158
184
  else
159
- params = query_string.split('&').collect do |chunk|
185
+ params = query_string.split("&").collect do |chunk|
160
186
  next if chunk.empty?
161
- key, value = chunk.split('=', 2)
187
+ key, value = chunk.split("=", 2)
162
188
  next if key.empty?
163
189
  value = value.nil? ? nil : CGI.unescape(value)
164
190
  [CGI.unescape(key), value]
@@ -171,15 +197,27 @@ module ActiveRecord
171
197
  def connection
172
198
  @_connection ||= ActiveRecord::Tablefree::Connection.new
173
199
  end
200
+
201
+ def lease_connection
202
+ connection
203
+ end
204
+
205
+ def with_connection(**)
206
+ yield connection
207
+ end
208
+
209
+ def connection_pool
210
+ connection.pool
211
+ end
174
212
  end
175
213
 
176
214
  module InstanceMethods
177
215
  def to_query_string(prefix = nil)
178
- attributes.to_a.collect { |(name, value)| escaped_var_name(name, prefix) + '=' + escape_for_url(value) if value }.compact.join('&')
216
+ attributes.to_a.collect { |(name, value)| escaped_var_name(name, prefix) + "=" + escape_for_url(value) if value }.compact.join("&")
179
217
  end
180
218
 
181
219
  def quote_value(_value, _column = nil)
182
- ''
220
+ ""
183
221
  end
184
222
 
185
223
  %w[create create_record _create_record update update_record _update_record].each do |method_name|
@@ -212,7 +250,8 @@ module ActiveRecord
212
250
  end
213
251
  end
214
252
 
215
- def add_to_transaction; end
253
+ def add_to_transaction(*_args)
254
+ end
216
255
 
217
256
  private
218
257
 
@@ -222,13 +261,13 @@ module ActiveRecord
222
261
 
223
262
  def escape_for_url(value)
224
263
  case value
225
- when true then '1'
226
- when false then '0'
227
- when nil then ''
264
+ when true then "1"
265
+ when false then "0"
266
+ when nil then ""
228
267
  else CGI.escape(value.to_s)
229
268
  end
230
269
  rescue
231
- ''
270
+ ""
232
271
  end
233
272
  end
234
273
  end
@@ -1,2 +1,8 @@
1
1
  # legacy automatic require support
2
- require 'activerecord/tablefree'
2
+ require "activerecord/tablefree"
3
+ require "version_gem"
4
+ require_relative "activerecord/tablefree/version"
5
+
6
+ ActiveRecord::Tablefree::Version.class_eval do
7
+ extend VersionGem::Basic
8
+ end
@@ -0,0 +1,21 @@
1
+ module ActiveRecord
2
+ module Tablefree
3
+ module ActsMethods
4
+ module Version
5
+ VERSION: String
6
+ end
7
+ VERSION: String
8
+ end
9
+
10
+ module Version
11
+ VERSION: String
12
+ end
13
+ VERSION: String
14
+ module NoDatabase
15
+ module Version
16
+ VERSION: String
17
+ end
18
+ VERSION: String
19
+ end
20
+ end
21
+ end
data.tar.gz.sig ADDED
Binary file