activerecord-tablefree 3.1.2 → 3.1.3

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: c9050219b074d636ea977dde8d3c3725f7bae889
4
- data.tar.gz: 3b7fa7a1cadfd4645d4ece6b90b95f1752782473
3
+ metadata.gz: bd641bf62d44d61669232874c115223d670525dc
4
+ data.tar.gz: e77eb3bc55b531d724c863d9368e36a9ff54b66f
5
5
  SHA512:
6
- metadata.gz: 0f759624174cecc91229bf7c2d15d04b7075f24f2f01c7aa76d1b7bc6ae60664391da59722ff0dc39cfa611763c016e35e4dec3bd74edddf8853131892d50bff
7
- data.tar.gz: 36429a10d2a5aa374b3bc86d12d294c2d670565e8557cad39379c26988e47c859feff79a0e8f932667f3015ecfc754a376384ff911325dc810890233b018a65c
6
+ metadata.gz: d747f17d5693c762b2511296a875bf63a71cf378fd62b8c662357cdf37e3343c96968e1cce39df072e3cda1637dd98fe194e87f6ae5defcbf605b70d16bc28b5
7
+ data.tar.gz: 936bf5a5f9ff23a685b55c604687eb8f18c43499c18333f95392c552e5a651022bb3c1f34305fa1ebc7427e21e7c585bef4a81a998299c05c891e7f1fc574065
data/README.md CHANGED
@@ -64,23 +64,25 @@ Define a model like this:
64
64
  has_no_table
65
65
  column :name, :string
66
66
  column :email, :string
67
- validates_presence_of :name, :email
67
+ column :message, :string
68
+ validates_presence_of :name, :email, :message
68
69
  end
69
70
 
70
71
  You can now use the model in a view like this:
71
72
 
72
- <%= form_for :message, @message do |f| %>
73
+ <%= form_for :contact_message, @contact_message do |f| %>
73
74
  Your name: <%= f.text_field :name %>
74
75
  Your email: <%= f.text_field :email %>
76
+ Your message: <%= f.text_field :message %>
75
77
  <% end %>
76
78
 
77
79
  And in the controller:
78
80
 
79
- def message
80
- @message = ContactMessage.new
81
+ def contact_message
82
+ @contact_message = ContactMessage.new
81
83
  if request.post?
82
- @message.attributes = params[:message]
83
- if @message.valid?
84
+ @contact_message.attributes = params[:contact_message]
85
+ if @contact_message.valid?
84
86
  # Process the message...
85
87
  end
86
88
  end
@@ -90,6 +92,34 @@ If you wish (this is not recommended), you can pretend you have a succeeding dat
90
92
 
91
93
  has_no_table :database => :pretend_success
92
94
 
95
+ Associations
96
+ ------------
97
+
98
+ Some model as before, but with an association to a real DB-backed model.
99
+
100
+ ```
101
+ class ContactMessage < ActiveRecord::Base
102
+ has_no_table
103
+ column :message, :string
104
+ column :email, :string
105
+ validates_presence_of :name, :email
106
+ belongs_to :contact, foreign_key: :email, primary_key: :email
107
+ end
108
+
109
+ class Contact < ActiveRecord::Base
110
+ validates_presence_of :name, :email
111
+ has_one :contact_message, foreign_key: :email, primary_key: :email, dependent: nil
112
+ end
113
+ ```
114
+
115
+ Obviously the association is not full-fledged, as some traversals just won't make sense with one side not being loadable from the database. From the `ContactMessage` you can get to the `Contact`, but not vice versa.
116
+
117
+ ```
118
+ >> contact = Contact.new(name: 'Boo', email: 'boo@example.com')
119
+ >> contact_message = ContactMessage.new(contact: contact)
120
+ >> contact_message.email
121
+ => 'boo@example.com'
122
+ ```
93
123
 
94
124
  Development
95
125
  -----------
@@ -1,5 +1,19 @@
1
1
  module ActiveRecord::Tablefree
2
- class Connection
2
+ class Connection < ActiveRecord::ConnectionAdapters::AbstractAdapter
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
11
+ @quoted_column_names, @quoted_table_names = {}, {}
12
+ @visitor = Object.new
13
+ @lock = Object.new
14
+ @prepared_statements = false
15
+ end
16
+
3
17
  def quote_table_name(*_args)
4
18
  ''
5
19
  end
@@ -12,10 +26,6 @@ module ActiveRecord::Tablefree
12
26
  nil
13
27
  end
14
28
 
15
- def schema_cache(*_args)
16
- @_schema_cache ||= ActiveRecord::Tablefree::SchemaCache.new
17
- end
18
-
19
29
  # Fixes Issue #17. https://github.com/softace/activerecord-tablefree/issues/17
20
30
  # The following method is from the ActiveRecord gem:
21
31
  # /lib/active_record/connection_adapters/abstract/database_statements.rb .
@@ -57,14 +67,9 @@ module ActiveRecord::Tablefree
57
67
  {}
58
68
  end
59
69
 
60
- # This is used in the StatementCache object. It returns an object that
61
- # can be used to query the database repeatedly.
70
+ # This is used in the StatementCache object.
62
71
  def cacheable_query(arel) # :nodoc:
63
- if prepared_statements
64
- ActiveRecord::StatementCache.query visitor, arel.ast
65
- else
66
- ActiveRecord::StatementCache.partial_query visitor, arel.ast, collector
67
- end
72
+ ActiveRecord::Tablefree::StatementCache.partial_query visitor, arel.ast, collector
68
73
  end
69
74
  end
70
75
  end
@@ -0,0 +1,22 @@
1
+ module ActiveRecord::Tablefree
2
+ class StatementCache < ActiveRecord::StatementCache
3
+ def self.create(*_args)
4
+ new Object.new, ActiveRecord::Tablefree::StatementCache::BindMap.new
5
+ end
6
+
7
+ def execute
8
+ nil
9
+ end
10
+
11
+ class BindMap
12
+ def initialize(*_args)
13
+ @indexes = []
14
+ @bound_attributes = []
15
+ end
16
+
17
+ def bind(*_args)
18
+ []
19
+ end
20
+ end
21
+ end
22
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ActiveRecord
4
4
  module Tablefree
5
- VERSION = '3.1.2'.freeze
5
+ VERSION = '3.1.3'.freeze
6
6
  end
7
7
  end
@@ -36,6 +36,7 @@ module ActiveRecord
36
36
  module Tablefree
37
37
  class NoDatabase < StandardError; end
38
38
  class Unsupported < StandardError; end
39
+ class InvalidColumnType < ArgumentError; end
39
40
 
40
41
  def self.included(base) #:nodoc:
41
42
  base.send :extend, ActsMethods
@@ -86,7 +87,9 @@ module ActiveRecord
86
87
 
87
88
  # Register a new column.
88
89
  def column(name, sql_type = nil, default = nil, null = true)
89
- cast_type = "ActiveRecord::Type::#{sql_type.to_s.camelize}".constantize.new
90
+ cast_class = "ActiveRecord::Type::#{sql_type.to_s.camelize}".constantize rescue nil
91
+ 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
90
93
  tablefree_options[:columns_hash][name.to_s] = ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, cast_type, sql_type.to_s, null)
91
94
  end
92
95
 
@@ -255,7 +255,6 @@ shared_examples_for 'an instance with succeeding database' do
255
255
  end
256
256
 
257
257
  describe 'ActiveRecord with real database' do
258
- # This is only here to ensure that the shared examples are actually behaving like a real database.
259
258
  before(:context) do
260
259
  FileUtils.mkdir_p 'tmp'
261
260
  ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: 'tmp/test.db')
@@ -276,6 +275,36 @@ describe 'ActiveRecord with real database' do
276
275
  subject { Chair.new(name: 'Jarl') }
277
276
  it_behaves_like 'an instance with succeeding database'
278
277
  end
278
+
279
+ describe 'association' do
280
+ before(:context) do
281
+ # reopen the chair to add an association
282
+ class Chair < ActiveRecord::Base
283
+ has_one :arm_rest, primary_key: :name, foreign_key: :name, dependent: nil
284
+ end
285
+ class ArmRest < ActiveRecord::Base
286
+ has_no_table
287
+ column :name, :string
288
+ belongs_to :chair, primary_key: :name, foreign_key: :name
289
+ end
290
+ end
291
+ it 'can be traversed by setting association' do
292
+ chair = Chair.create(name: 'A')
293
+ arm_rest = ArmRest.new(chair: chair)
294
+ expect(arm_rest.chair).to eq chair
295
+ end
296
+ it 'sets foreign key from association' do
297
+ chair = Chair.create(name: 'A')
298
+ arm_rest = ArmRest.new(chair: chair)
299
+ expect(arm_rest.name).to eq 'A'
300
+ end
301
+ it 'can be traversed by setting foreign key' do
302
+ pending('more effort to implement')
303
+ Chair.create(name: 'B')
304
+ arm_rest = ArmRest.new(name: 'B')
305
+ expect { arm_rest.chair }.to_not raise_error
306
+ end
307
+ end
279
308
  end
280
309
 
281
310
  describe 'Tablefree model with succeeding database' do
@@ -306,3 +335,17 @@ describe 'Tablefree model can access connection transaction' do
306
335
  it { is_expected.to be_a(ActiveRecord::Tablefree::Transaction) }
307
336
  end
308
337
  end
338
+
339
+ describe 'Bad column type' do
340
+ describe 'association' do
341
+ subject { -> {
342
+ class ArmRest < ActiveRecord::Base
343
+ has_no_table
344
+ column :chair_id
345
+ end
346
+ }}
347
+ it 'raises an InvalidColumnType error' do
348
+ expect { subject.call }.to raise_error ActiveRecord::Tablefree::InvalidColumnType, 'sql_type is (NilClass), which is not supported'
349
+ end
350
+ end
351
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-tablefree
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.2
4
+ version: 3.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jarl Friis
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2017-12-20 00:00:00.000000000 Z
14
+ date: 2017-12-21 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activerecord
@@ -284,6 +284,7 @@ files:
284
284
  - lib/activerecord/tablefree/cast_type.rb
285
285
  - lib/activerecord/tablefree/connection.rb
286
286
  - lib/activerecord/tablefree/schema_cache.rb
287
+ - lib/activerecord/tablefree/statement_cache.rb
287
288
  - lib/activerecord/tablefree/transaction.rb
288
289
  - lib/activerecord/tablefree/version.rb
289
290
  - spec/activerecord/tablefree/version_spec.rb