declare_schema 0.2.0 → 0.3.0.pre.1

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
  SHA256:
3
- metadata.gz: 3f3116a6fbb5a47de5809f3f813c88783284bad601452679fa59784f3598834b
4
- data.tar.gz: 8f4b6fee7b4d17e5b644f32a4b624a3f77526f91826a8c0b82d61b6e25130332
3
+ metadata.gz: 2a82a5e54f5dadf8a733520efe057d8b1a0319e1725ef82d41d97004ee68cf0c
4
+ data.tar.gz: 899e9f135706d4883b448bcffc09b5f962cb84892c1bf1be27e6f2e36209454f
5
5
  SHA512:
6
- metadata.gz: b833000d28d64e7856de05c9ae822c88dcceeb7861f777f13177ea8017e56bf937aba42b9da14ae1ce4810fd275c5462cb08c18c32839255ab46f1de116eee1d
7
- data.tar.gz: c41c8cbdafd96eeb92fcf5c646ff9d18f4ce2374f06ecc6977015fa8406bc4639ab3722354212d1a75274c5af982f10b6c2f4c838de94f4b9436da20f836d5d3
6
+ metadata.gz: 0465ffd5522605f9e0c61beffc2571ab76d077020596d82fe616c60cd631c6663b399c9c5d02c8983d9060223f3bb100932baf411efa69a3a7f236097784ae9e
7
+ data.tar.gz: 4c6b7b49067650cd0791257614dc84e1f6f6a9191346578249fa196dc0d9f621c491d597aaa32f82c64bde8f6e7fe8e18f1974f89c4677ade6c8203aede20ec4
@@ -4,6 +4,13 @@ Inspired by [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
4
4
 
5
5
  Note: this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [0.3.0] - Unreleased
8
+ ### Added
9
+ - Added support for `belongs_to optional:`.
10
+ If given, it is passed through to `ActiveRecord`'s `belong_to`.
11
+ If not given in Rails 5+, the `optional:` value is set equal to the `null:` value (default: `false`) and that
12
+ is is passed to `ActiveRecord`'s `belong_to`.
13
+
7
14
  ## [0.2.0] - 2020-10-26
8
15
  ### Added
9
16
  - Automatically eager_load! all Rails::Engines before generating migrations.
@@ -29,6 +36,7 @@ using the appropriate Rails configuration attributes.
29
36
  ### Added
30
37
  - Initial version from https://github.com/Invoca/hobo_fields v4.1.0.
31
38
 
39
+ [0.3.0]: https://github.com/Invoca/declare_schema/compare/v0.2.0...v0.3.0
32
40
  [0.2.0]: https://github.com/Invoca/declare_schema/compare/v0.1.3...v0.2.0
33
41
  [0.1.3]: https://github.com/Invoca/declare_schema/compare/v0.1.2...v0.1.3
34
42
  [0.1.2]: https://github.com/Invoca/declare_schema/compare/v0.1.1...v0.1.2
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- declare_schema (0.2.0)
4
+ declare_schema (0.3.0.pre.1)
5
5
  rails (>= 4.2)
6
6
 
7
7
  GEM
@@ -106,15 +106,7 @@ module DeclareSchema
106
106
  end
107
107
 
108
108
  # Extend belongs_to so that it creates a FieldSpec for the foreign key
109
- def belongs_to(name, *args, &block)
110
- if args.size == 0 || (args.size == 1 && args[0].is_a?(Proc))
111
- options = {}
112
- args.push(options)
113
- elsif args.size == 1
114
- options = args[0]
115
- else
116
- options = args[1]
117
- end
109
+ def belongs_to(name, scope = nil, **options, &block)
118
110
  column_options = {}
119
111
  column_options[:null] = options.delete(:null) || false
120
112
  column_options[:default] = options.delete(:default) if options.has_key?(:default)
@@ -129,20 +121,27 @@ module DeclareSchema
129
121
  fk_options[:constraint_name] = options.delete(:constraint) if options.has_key?(:constraint)
130
122
  fk_options[:index_name] = index_options[:name]
131
123
 
124
+ fk = options[:foreign_key]&.to_s || "#{name}_id"
125
+
126
+ if !options.has_key?(:optional) && Rails::VERSION::MAJOR >= 5
127
+ options[:optional] = column_options[:null]
128
+ end
129
+
132
130
  fk_options[:dependent] = options.delete(:far_end_dependent) if options.has_key?(:far_end_dependent)
133
- super(name, *args, &block).tap do |_bt|
134
- refl = reflections[name.to_s] or raise "Couldn't find reflection #{name} in #{reflections.keys}"
135
- fkey = refl.foreign_key
136
- declare_field(fkey.to_sym, :integer, column_options)
137
- if refl.options[:polymorphic]
138
- foreign_type = options[:foreign_type] || "#{name}_type"
139
- declare_polymorphic_type_field(foreign_type, column_options)
140
- index([foreign_type, fkey], index_options) if index_options[:name] != false
141
- else
142
- index(fkey, index_options) if index_options[:name] != false
143
- options[:constraint_name] = options
144
- constraint(fkey, fk_options) if fk_options[:constraint_name] != false
145
- end
131
+
132
+ super(name, scope, options)
133
+
134
+ refl = reflections[name.to_s] or raise "Couldn't find reflection #{name} in #{reflections.keys}"
135
+ fkey = refl.foreign_key or raise "Couldn't find foreign_key for #{name} in #{refl.inspect}"
136
+ declare_field(fkey.to_sym, :integer, column_options)
137
+ if refl.options[:polymorphic]
138
+ foreign_type = options[:foreign_type] || "#{name}_type"
139
+ declare_polymorphic_type_field(foreign_type, column_options)
140
+ index([foreign_type, fkey], index_options) if index_options[:name] != false
141
+ else
142
+ index(fkey, index_options) if index_options[:name] != false
143
+ options[:constraint_name] = options
144
+ constraint(fkey, fk_options) if fk_options[:constraint_name] != false
146
145
  end
147
146
  end
148
147
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DeclareSchema
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0.pre.1"
5
5
  end
@@ -129,7 +129,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
129
129
  end
130
130
  end
131
131
 
132
- up, down = Generators::DeclareSchema::Migration::Migrator.run
132
+ up = Generators::DeclareSchema::Migration::Migrator.run.first
133
133
  expect(up).to eq("add_column :adverts, :price, :integer, limit: 2")
134
134
 
135
135
  # Now run the migration, then change the limit:
@@ -154,7 +154,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
154
154
  end
155
155
  end
156
156
 
157
- up, down = Generators::DeclareSchema::Migration::Migrator.run
157
+ up = Generators::DeclareSchema::Migration::Migrator.run.first
158
158
  expect(up).to eq("add_column :adverts, :price, :decimal")
159
159
 
160
160
  # Limits are generally not needed for `text` fields, because by default, `text` fields will use the maximum size
@@ -170,7 +170,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
170
170
  end
171
171
  end
172
172
 
173
- up, down = Generators::DeclareSchema::Migration::Migrator.run
173
+ up = Generators::DeclareSchema::Migration::Migrator.run.first
174
174
  expect(up).to eq(<<~EOS.strip)
175
175
  add_column :adverts, :price, :decimal
176
176
  add_column :adverts, :notes, :text, null: false
@@ -194,7 +194,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
194
194
  end
195
195
  end
196
196
 
197
- up, down = Generators::DeclareSchema::Migration::Migrator.run
197
+ up = Generators::DeclareSchema::Migration::Migrator.run.first
198
198
  expect(up).to eq(<<~EOS.strip)
199
199
  add_column :adverts, :notes, :text, null: false, limit: 4294967295
200
200
  add_column :adverts, :description, :text, null: false, limit: 255
@@ -266,7 +266,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
266
266
  end
267
267
  end
268
268
 
269
- up, down = Generators::DeclareSchema::Migration::Migrator.run
269
+ up = Generators::DeclareSchema::Migration::Migrator.run.first
270
270
  ActiveRecord::Migration.class_eval up
271
271
  Advert.connection.schema_cache.clear!
272
272
  Advert.reset_column_information
@@ -278,6 +278,9 @@ RSpec.describe 'DeclareSchema Migration Generator' do
278
278
 
279
279
  class Category < ActiveRecord::Base; end
280
280
  class Advert < ActiveRecord::Base
281
+ fields do
282
+ name :string, limit: 255, null: true
283
+ end
281
284
  belongs_to :category
282
285
  end
283
286
 
@@ -298,9 +301,10 @@ RSpec.describe 'DeclareSchema Migration Generator' do
298
301
 
299
302
  class Category < ActiveRecord::Base; end
300
303
  class Advert < ActiveRecord::Base
304
+ fields { }
301
305
  belongs_to :category, foreign_key: "c_id", class_name: 'Category'
302
306
  end
303
- up, down = Generators::DeclareSchema::Migration::Migrator.run
307
+ up = Generators::DeclareSchema::Migration::Migrator.run.first
304
308
  expect(up.gsub(/\n+/, "\n")).to eq(<<~EOS.strip)
305
309
  add_column :adverts, :c_id, :integer, limit: 8, null: false
306
310
  add_index :adverts, [:c_id], name: 'on_c_id'
@@ -313,9 +317,10 @@ RSpec.describe 'DeclareSchema Migration Generator' do
313
317
 
314
318
  class Category < ActiveRecord::Base; end
315
319
  class Advert < ActiveRecord::Base
320
+ fields { }
316
321
  belongs_to :category, index: false
317
322
  end
318
- up, down = Generators::DeclareSchema::Migration::Migrator.run
323
+ up = Generators::DeclareSchema::Migration::Migrator.run.first
319
324
  expect(up.gsub(/\n+/, "\n")).to eq("add_column :adverts, :category_id, :integer, limit: 8, null: false")
320
325
 
321
326
  Advert.field_specs.delete(:category_id)
@@ -325,9 +330,10 @@ RSpec.describe 'DeclareSchema Migration Generator' do
325
330
 
326
331
  class Category < ActiveRecord::Base; end
327
332
  class Advert < ActiveRecord::Base
333
+ fields { }
328
334
  belongs_to :category, index: 'my_index'
329
335
  end
330
- up, down = Generators::DeclareSchema::Migration::Migrator.run
336
+ up = Generators::DeclareSchema::Migration::Migrator.run.first
331
337
  expect(up.gsub(/\n+/, "\n")).to eq(<<~EOS.strip)
332
338
  add_column :adverts, :category_id, :integer, limit: 8, null: false
333
339
  add_index :adverts, [:category_id], name: 'my_index'
@@ -372,7 +378,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
372
378
  title :string, index: true, limit: 255, null: true
373
379
  end
374
380
  end
375
- up, down = Generators::DeclareSchema::Migration::Migrator.run
381
+ up = Generators::DeclareSchema::Migration::Migrator.run.first
376
382
  expect(up.gsub(/\n+/, "\n")).to eq(<<~EOS.strip)
377
383
  add_column :adverts, :title, :string, limit: 255
378
384
  add_index :adverts, [:title], name: 'on_title'
@@ -387,7 +393,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
387
393
  title :string, index: true, unique: true, null: true, limit: 255
388
394
  end
389
395
  end
390
- up, down = Generators::DeclareSchema::Migration::Migrator.run
396
+ up = Generators::DeclareSchema::Migration::Migrator.run.first
391
397
  expect(up.gsub(/\n+/, "\n")).to eq(<<~EOS.strip)
392
398
  add_column :adverts, :title, :string, limit: 255
393
399
  add_index :adverts, [:title], unique: true, name: 'on_title'
@@ -402,20 +408,20 @@ RSpec.describe 'DeclareSchema Migration Generator' do
402
408
  title :string, index: 'my_index', limit: 255, null: true
403
409
  end
404
410
  end
405
- up, down = Generators::DeclareSchema::Migration::Migrator.run
411
+ up = Generators::DeclareSchema::Migration::Migrator.run.first
406
412
  expect(up.gsub(/\n+/, "\n")).to eq(<<~EOS.strip)
407
413
  add_column :adverts, :title, :string, limit: 255
408
414
  add_index :adverts, [:title], name: 'my_index'
409
415
  EOS
410
416
 
411
- Advert.index_specs.delete_if {|spec| spec.fields==["title"]}
417
+ Advert.index_specs.delete_if { |spec| spec.fields==["title"] }
412
418
 
413
419
  # You can ask for an index outside of the fields block
414
420
 
415
421
  class Advert < ActiveRecord::Base
416
422
  index :title
417
423
  end
418
- up, down = Generators::DeclareSchema::Migration::Migrator.run
424
+ up = Generators::DeclareSchema::Migration::Migrator.run.first
419
425
  expect(up.gsub(/\n+/, "\n")).to eq(<<~EOS.strip)
420
426
  add_column :adverts, :title, :string, limit: 255
421
427
  add_index :adverts, [:title], name: 'on_title'
@@ -428,7 +434,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
428
434
  class Advert < ActiveRecord::Base
429
435
  index :title, unique: true, name: 'my_index'
430
436
  end
431
- up, down = Generators::DeclareSchema::Migration::Migrator.run
437
+ up = Generators::DeclareSchema::Migration::Migrator.run.first
432
438
  expect(up.gsub(/\n+/, "\n")).to eq(<<~EOS.strip)
433
439
  add_column :adverts, :title, :string, limit: 255
434
440
  add_index :adverts, [:title], unique: true, name: 'my_index'
@@ -441,7 +447,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
441
447
  class Advert < ActiveRecord::Base
442
448
  index [:title, :category_id]
443
449
  end
444
- up, down = Generators::DeclareSchema::Migration::Migrator.run
450
+ up = Generators::DeclareSchema::Migration::Migrator.run.first
445
451
  expect(up.gsub(/\n+/, "\n")).to eq(<<~EOS.strip)
446
452
  add_column :adverts, :title, :string, limit: 255
447
453
  add_index :adverts, [:title, :category_id], name: 'on_title_and_category_id'
@@ -544,7 +550,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
544
550
  title :string, default: "Untitled", limit: 255, null: true
545
551
  end
546
552
  end
547
- up, down = Generators::DeclareSchema::Migration::Migrator.run
553
+ up = Generators::DeclareSchema::Migration::Migrator.run.first
548
554
  ActiveRecord::Migration.class_eval(up)
549
555
 
550
556
  class FancyAdvert < Advert
@@ -613,7 +619,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
613
619
  created_at :datetime
614
620
  end
615
621
  end
616
- up, down = Generators::DeclareSchema::Migration::Migrator.run(adverts: :ads)
622
+ up = Generators::DeclareSchema::Migration::Migrator.run(adverts: :ads).first
617
623
  expect(up.gsub(/\n+/, "\n")).to eq(<<~EOS.strip)
618
624
  rename_table :adverts, :ads
619
625
  add_column :ads, :created_at, :datetime, null: false
@@ -682,5 +688,48 @@ RSpec.describe 'DeclareSchema Migration Generator' do
682
688
  ActiveRecord::Migration.class_eval(up)
683
689
  expect(Ad.field_specs['company'].options[:validates].inspect).to eq("{:presence=>true, :uniqueness=>{:case_sensitive=>false}}")
684
690
  end
685
- end
686
691
 
692
+ describe 'belongs_to' do
693
+ before do
694
+ unless defined?(AdCategory)
695
+ class AdCategory < ActiveRecord::Base
696
+ fields { }
697
+ end
698
+ end
699
+
700
+ class Advert < ActiveRecord::Base
701
+ fields do
702
+ name :string, limit: 255, null: true
703
+ category_id :integer, limit: 8
704
+ nullable_category_id :integer, limit: 8, null: true
705
+ end
706
+ end
707
+ up = Generators::DeclareSchema::Migration::Migrator.run.first
708
+ ActiveRecord::Migration.class_eval(up)
709
+ end
710
+
711
+ it 'passes through optional:, if given' do
712
+ class AdvertBelongsTo < ActiveRecord::Base
713
+ self.table_name = 'adverts'
714
+ fields { }
715
+ reset_column_information
716
+ belongs_to :ad_category, optional: true
717
+ end
718
+ expect(AdvertBelongsTo.reflections['ad_category'].options).to eq(optional: true)
719
+ end
720
+
721
+ [false, true].each do |nullable|
722
+ context "nullable=#{nullable}" do
723
+ it 'infers the optional: argument from null: option' do
724
+ eval <<~EOS
725
+ class AdvertBelongsTo < ActiveRecord::Base
726
+ fields { }
727
+ belongs_to :ad_category, null: #{nullable}
728
+ end
729
+ EOS
730
+ expect(AdvertBelongsTo.reflections['ad_category'].options).to eq(optional: nullable)
731
+ end
732
+ end
733
+ end
734
+ end
735
+ end
@@ -18,6 +18,8 @@ require "#{TESTAPP_PATH}/config/environment"
18
18
  require 'rails/generators'
19
19
  Rails::Generators.configure!(Rails.application.config.generators)
20
20
 
21
+ ActiveRecord::Base.connection.schema_cache.clear!
22
+
21
23
  (ActiveRecord::Base.connection.tables - Generators::DeclareSchema::Migration::Migrator.always_ignore_tables).each do |table|
22
24
  ActiveRecord::Base.connection.execute("DROP TABLE #{ActiveRecord::Base.connection.quote_table_name(table)}")
23
25
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: declare_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0.pre.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Invoca Development adapted from hobo_fields by Tom Locke
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-26 00:00:00.000000000 Z
11
+ date: 2020-10-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails