declare_schema 0.12.1 → 0.13.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: 2bace1c4f410e4766d4b9aa1414a5394414e8294db88ccd3d168d6db5d9802f6
4
- data.tar.gz: da2b3467c656b0bad95b85627c341fb4d447872b144a452e3569a85a7369c452
3
+ metadata.gz: 4caf19f1ddb0978b9e9bbc5c55d5941a934ded2e4bbd8adfba52c078e599106b
4
+ data.tar.gz: 2a8d9f9434f6acae5cab5bfe4ffc6eee0d6ce0e31a59b471d0c48b63b0c3260c
5
5
  SHA512:
6
- metadata.gz: cf6eb2e6b67bd3071b9261a5d07eaa56dc019c148b02bbc11e70dd929934855e0b48a0c4c178d348286ec7bba4ed25f90c8041d855150c6b23c1aa81f26af25f
7
- data.tar.gz: 3517a656b7b7edda5ed4164495747b0d879329d96bb44294f5b347acacc96a983e3cb6ab99fad8bf6e7b9fe998634ceb7cbd4ad79383164ab05ea6b273974c9f
6
+ metadata.gz: 6edfafddcd596afd63417d2e8f59aec7bfcd637e35d0fb77fc06f39e6e78d461da5c2e0b6752cb8b18785afa0ac81cfb7569fa0be85ba06ad3432d3911a624e5
7
+ data.tar.gz: 7d2d190f0c6928f7b70f96c8ce58517475ce1b512a31a7c33033314bff0405fb51d88c82c72bd0a8a1b25d8558039ef787b0314b60f7fdefbd75975c3320f57e
data/CHANGELOG.md CHANGED
@@ -4,6 +4,15 @@ 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.13.1] - 2021-08-02
8
+ ### Fixed
9
+ - Fixed migration file indentation bug in Rails 5 where the first line was indented an extra 4 characters.
10
+ And fixed the longstanding triple-spacing bug to use double spacing.
11
+
12
+ ## [0.13.0] - 2021-06-11
13
+ ### Added
14
+ - Added support for `default_schema` block to apply a default schema to every model, unless disabled for that model with `default_schema: false`.
15
+
7
16
  ## [0.12.1] - 2021-05-10
8
17
  ### Fixed
9
18
  - When an `enum` type field is declared, there is now enforcement that its `limit:` must be an array of 1 or more Symbols,
@@ -179,6 +188,8 @@ using the appropriate Rails configuration attributes.
179
188
  ### Added
180
189
  - Initial version from https://github.com/Invoca/hobo_fields v4.1.0.
181
190
 
191
+ [0.13.1]: https://github.com/Invoca/declare_schema/compare/v0.13.0...v0.13.1
192
+ [0.13.0]: https://github.com/Invoca/declare_schema/compare/v0.12.1...v0.13.0
182
193
  [0.12.1]: https://github.com/Invoca/declare_schema/compare/v0.12.0...v0.12.1
183
194
  [0.12.0]: https://github.com/Invoca/declare_schema/compare/v0.11.1...v0.12.0
184
195
  [0.11.1]: https://github.com/Invoca/declare_schema/compare/v0.11.0...v0.11.1
data/Gemfile.lock CHANGED
@@ -1,49 +1,49 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- declare_schema (0.12.1)
4
+ declare_schema (0.13.1)
5
5
  rails (>= 4.2)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- actioncable (5.2.4.5)
11
- actionpack (= 5.2.4.5)
10
+ actioncable (5.2.6)
11
+ actionpack (= 5.2.6)
12
12
  nio4r (~> 2.0)
13
13
  websocket-driver (>= 0.6.1)
14
- actionmailer (5.2.4.5)
15
- actionpack (= 5.2.4.5)
16
- actionview (= 5.2.4.5)
17
- activejob (= 5.2.4.5)
14
+ actionmailer (5.2.6)
15
+ actionpack (= 5.2.6)
16
+ actionview (= 5.2.6)
17
+ activejob (= 5.2.6)
18
18
  mail (~> 2.5, >= 2.5.4)
19
19
  rails-dom-testing (~> 2.0)
20
- actionpack (5.2.4.5)
21
- actionview (= 5.2.4.5)
22
- activesupport (= 5.2.4.5)
20
+ actionpack (5.2.6)
21
+ actionview (= 5.2.6)
22
+ activesupport (= 5.2.6)
23
23
  rack (~> 2.0, >= 2.0.8)
24
24
  rack-test (>= 0.6.3)
25
25
  rails-dom-testing (~> 2.0)
26
26
  rails-html-sanitizer (~> 1.0, >= 1.0.2)
27
- actionview (5.2.4.5)
28
- activesupport (= 5.2.4.5)
27
+ actionview (5.2.6)
28
+ activesupport (= 5.2.6)
29
29
  builder (~> 3.1)
30
30
  erubi (~> 1.4)
31
31
  rails-dom-testing (~> 2.0)
32
32
  rails-html-sanitizer (~> 1.0, >= 1.0.3)
33
- activejob (5.2.4.5)
34
- activesupport (= 5.2.4.5)
33
+ activejob (5.2.6)
34
+ activesupport (= 5.2.6)
35
35
  globalid (>= 0.3.6)
36
- activemodel (5.2.4.5)
37
- activesupport (= 5.2.4.5)
38
- activerecord (5.2.4.5)
39
- activemodel (= 5.2.4.5)
40
- activesupport (= 5.2.4.5)
36
+ activemodel (5.2.6)
37
+ activesupport (= 5.2.6)
38
+ activerecord (5.2.6)
39
+ activemodel (= 5.2.6)
40
+ activesupport (= 5.2.6)
41
41
  arel (>= 9.0)
42
- activestorage (5.2.4.5)
43
- actionpack (= 5.2.4.5)
44
- activerecord (= 5.2.4.5)
45
- marcel (~> 0.3.1)
46
- activesupport (5.2.4.5)
42
+ activestorage (5.2.6)
43
+ actionpack (= 5.2.6)
44
+ activerecord (= 5.2.6)
45
+ marcel (~> 1.0.0)
46
+ activesupport (5.2.6)
47
47
  concurrent-ruby (~> 1.0, >= 1.0.2)
48
48
  i18n (>= 0.7, < 2)
49
49
  minitest (~> 5.1)
@@ -60,33 +60,31 @@ GEM
60
60
  byebug (11.1.3)
61
61
  climate_control (0.2.0)
62
62
  coderay (1.1.3)
63
- concurrent-ruby (1.1.8)
63
+ concurrent-ruby (1.1.9)
64
64
  crass (1.0.6)
65
65
  diff-lcs (1.4.4)
66
66
  erubi (1.10.0)
67
- ffi (1.14.2)
67
+ ffi (1.15.1)
68
68
  globalid (0.4.2)
69
69
  activesupport (>= 4.2.0)
70
- i18n (1.8.9)
70
+ i18n (1.8.10)
71
71
  concurrent-ruby (~> 1.0)
72
- listen (3.4.1)
72
+ listen (3.5.1)
73
73
  rb-fsevent (~> 0.10, >= 0.10.3)
74
74
  rb-inotify (~> 0.9, >= 0.9.10)
75
- loofah (2.9.0)
75
+ loofah (2.10.0)
76
76
  crass (~> 1.0.2)
77
77
  nokogiri (>= 1.5.9)
78
78
  mail (2.7.1)
79
79
  mini_mime (>= 0.1.1)
80
- marcel (0.3.3)
81
- mimemagic (~> 0.3.2)
80
+ marcel (1.0.1)
82
81
  method_source (1.0.0)
83
- mimemagic (0.3.5)
84
- mini_mime (1.0.2)
85
- mini_portile2 (2.5.0)
86
- minitest (5.14.3)
82
+ mini_mime (1.1.0)
83
+ mini_portile2 (2.5.3)
84
+ minitest (5.14.4)
87
85
  msgpack (1.4.2)
88
- nio4r (2.5.5)
89
- nokogiri (1.11.1)
86
+ nio4r (2.5.7)
87
+ nokogiri (1.11.7)
90
88
  mini_portile2 (~> 2.5.0)
91
89
  racc (~> 1.4)
92
90
  parallel (1.19.2)
@@ -102,33 +100,33 @@ GEM
102
100
  rack (2.2.3)
103
101
  rack-test (1.1.0)
104
102
  rack (>= 1.0, < 3)
105
- rails (5.2.4.5)
106
- actioncable (= 5.2.4.5)
107
- actionmailer (= 5.2.4.5)
108
- actionpack (= 5.2.4.5)
109
- actionview (= 5.2.4.5)
110
- activejob (= 5.2.4.5)
111
- activemodel (= 5.2.4.5)
112
- activerecord (= 5.2.4.5)
113
- activestorage (= 5.2.4.5)
114
- activesupport (= 5.2.4.5)
103
+ rails (5.2.6)
104
+ actioncable (= 5.2.6)
105
+ actionmailer (= 5.2.6)
106
+ actionpack (= 5.2.6)
107
+ actionview (= 5.2.6)
108
+ activejob (= 5.2.6)
109
+ activemodel (= 5.2.6)
110
+ activerecord (= 5.2.6)
111
+ activestorage (= 5.2.6)
112
+ activesupport (= 5.2.6)
115
113
  bundler (>= 1.3.0)
116
- railties (= 5.2.4.5)
114
+ railties (= 5.2.6)
117
115
  sprockets-rails (>= 2.0.0)
118
116
  rails-dom-testing (2.0.3)
119
117
  activesupport (>= 4.2.0)
120
118
  nokogiri (>= 1.6)
121
119
  rails-html-sanitizer (1.3.0)
122
120
  loofah (~> 2.3)
123
- railties (5.2.4.5)
124
- actionpack (= 5.2.4.5)
125
- activesupport (= 5.2.4.5)
121
+ railties (5.2.6)
122
+ actionpack (= 5.2.6)
123
+ activesupport (= 5.2.6)
126
124
  method_source
127
125
  rake (>= 0.8.7)
128
126
  thor (>= 0.19.0, < 2.0)
129
127
  rainbow (3.0.0)
130
128
  rake (13.0.3)
131
- rb-fsevent (0.10.4)
129
+ rb-fsevent (0.11.0)
132
130
  rb-inotify (0.10.1)
133
131
  ffi (~> 1.0)
134
132
  regexp_parser (1.8.0)
@@ -174,7 +172,7 @@ GEM
174
172
  tzinfo (1.2.9)
175
173
  thread_safe (~> 0.1)
176
174
  unicode-display_width (1.7.0)
177
- websocket-driver (0.7.3)
175
+ websocket-driver (0.7.4)
178
176
  websocket-extensions (>= 0.1.0)
179
177
  websocket-extensions (0.1.5)
180
178
  yard (0.9.26)
data/README.md CHANGED
@@ -62,7 +62,7 @@ trigger the `eager_load!` on the `Rails` application and all `Rails::Engine`s lo
62
62
  into scope. If you need to generate migrations for models that aren't automatically loaded by `eager_load!`,
63
63
  load them in the `before_generating_migration` block.
64
64
 
65
- **Example Configuration**
65
+ For example:
66
66
 
67
67
  ```ruby
68
68
  DeclareSchema::Migration::Migrator.before_generating_migration do
@@ -70,6 +70,32 @@ DeclareSchema::Migration::Migrator.before_generating_migration do
70
70
  end
71
71
  ```
72
72
 
73
+ ### default_schema
74
+ If there are default columns you would like in the schema for every model, you can define them in a block that is registered with
75
+ `DeclareSchema.default_schema`. For example:
76
+
77
+ ```ruby
78
+ DeclareSchema.default_schema do
79
+ timestamps
80
+ optimistic_lock
81
+ end
82
+ ```
83
+ This will add these fields to the schema of each model (if not already there).
84
+ If you have a model where you don't want the defaults applied, that can be set with the `default_schema:` boolean option to `declare_schema` (the default value is true). For example:
85
+ ```ruby
86
+ class User < ActiveRecord::Base
87
+ declare_schema default_schema: false do
88
+ ...
89
+ end
90
+ end
91
+ ```
92
+
93
+ ### clear_default_schema
94
+ This method clears out any previously declared `default_schema`.
95
+ ```ruby
96
+ DeclareSchema.clear_default_schema
97
+ ```
98
+
73
99
  ### Global Configuration
74
100
  Configurations can be set at the global level to customize default declaration for the following values:
75
101
 
@@ -85,6 +85,19 @@ module DeclareSchema
85
85
  @default_generate_indexing = generate_indexing
86
86
  end
87
87
 
88
+ def default_schema(&block)
89
+ if block.nil?
90
+ @default_schema # equivalent to attr_reader :default_schema
91
+ else
92
+ block.respond_to?(:call) or raise "default_schema must be passed a block that responds to call"
93
+ @default_schema = block
94
+ end
95
+ end
96
+
97
+ def clear_default_schema
98
+ @default_schema = nil
99
+ end
100
+
88
101
  def db_migrate_command=(db_migrate_command)
89
102
  db_migrate_command.is_a?(String) or raise ArgumentError, "db_migrate_command must be a string (got #{db_migrate_command.inspect})"
90
103
  @db_migrate_command = db_migrate_command
@@ -7,7 +7,7 @@ module DeclareSchema
7
7
  include ::Kernel # but we need the basic class methods
8
8
 
9
9
  instance_methods.each do |m|
10
- unless m.to_s.starts_with?('__') || m.in?([:object_id, :instance_eval])
10
+ unless m.to_s.starts_with?('__') || m.in?([:object_id, :instance_eval, :instance_exec])
11
11
  undef_method(m)
12
12
  end
13
13
  end
@@ -32,8 +32,11 @@ module DeclareSchema
32
32
  @model.declare_field(name, type, *(args + [@options.merge(options)]))
33
33
  end
34
34
 
35
- def method_missing(type, name, *args)
36
- field(name, type, *args)
35
+ # TODO: make [:required] just another option. Either 'required: true] or 'optional: false'?
36
+ def method_missing(*args, **options)
37
+ args.count(&:itself) >= 2 or raise ::ArgumentError, "fields in declare_schema block must be declared as: type name, [:required], options (got #{args.inspect}, #{options.inspect})"
38
+ type, name, *required = args
39
+ field(name, type, *required, options)
37
40
  end
38
41
  end
39
42
  end
@@ -27,7 +27,7 @@ module DeclareSchema
27
27
  end
28
28
  deprecate :fields, deprecator: ActiveSupport::Deprecation.new('1.0', 'DeclareSchema')
29
29
 
30
- def declare_schema(table_options = {}, &block)
30
+ def declare_schema(default_schema: true, **table_options, &block)
31
31
  # Any model that calls 'fields' gets DeclareSchema::Model behavior
32
32
  DeclareSchema::Model.mix_in(self)
33
33
 
@@ -37,10 +37,9 @@ module DeclareSchema
37
37
 
38
38
  if block
39
39
  dsl = DeclareSchema::Dsl.new(self, null: false)
40
- if block.arity == 1
41
- yield dsl
42
- else
43
- dsl.instance_eval(&block)
40
+ dsl.instance_eval(&block)
41
+ if default_schema && DeclareSchema.default_schema
42
+ dsl.instance_exec(&DeclareSchema.default_schema)
44
43
  end
45
44
  end
46
45
  end
@@ -34,19 +34,16 @@ module DeclareSchema
34
34
  # supported options include :charset and :collation
35
35
  inheriting_cattr_reader table_options: HashWithIndifferentAccess.new
36
36
 
37
- # eval avoids the ruby 1.9.2 "super from singleton method ..." error
38
-
39
- eval <<~EOS
40
- def self.inherited(klass)
41
- unless klass.field_specs.has_key?(inheritance_column)
42
- declare_schema do |f|
43
- f.field(inheritance_column, :string, limit: 255, null: true)
44
- end
45
- index(inheritance_column)
37
+ def self.inherited(klass)
38
+ unless klass.field_specs.has_key?(inheritance_column)
39
+ ic = inheritance_column
40
+ declare_schema do
41
+ field(ic, :string, limit: 255, null: true)
46
42
  end
47
- super
43
+ index(ic)
48
44
  end
49
- EOS
45
+ super
46
+ end
50
47
  end
51
48
  end
52
49
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DeclareSchema
4
- VERSION = "0.12.1"
4
+ VERSION = "0.13.1"
5
5
  end
@@ -299,7 +299,7 @@ module Generators
299
299
  up = migration_commands.map(&:up ).select(&:present?)
300
300
  down = migration_commands.map(&:down).select(&:present?).reverse
301
301
 
302
- [up * "\n\n", down * "\n\n"]
302
+ [up * "\n", down * "\n"]
303
303
  end
304
304
 
305
305
  def create_table_options(model, disable_auto_increment)
@@ -1,9 +1,9 @@
1
1
  class <%= @migration_class_name %> < (ActiveSupport::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[4.2] : ActiveRecord::Migration)
2
2
  def self.up
3
- <%= @up.presence or raise "no @up given!" %>
3
+ <%= @up.presence or raise "no @up given!" %>
4
4
  end
5
5
 
6
6
  def self.down
7
- <%= @down.presence or raise "no @down given!" %>
7
+ <%= @down.presence or raise "no @down given!" %>
8
8
  end
9
9
  end
@@ -1860,6 +1860,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
1860
1860
  class SuperFancyAdvert < FancyAdvert
1861
1861
  end
1862
1862
 
1863
+ expect(Generators::DeclareSchema::Migration::Migrator.run.first).to be_present
1864
+
1863
1865
  up, _ = Generators::DeclareSchema::Migration::Migrator.run do |migrations|
1864
1866
  expect(migrations).to(
1865
1867
  migrate_up(<<~EOS.strip)
@@ -2491,5 +2493,78 @@ RSpec.describe 'DeclareSchema Migration Generator' do
2491
2493
  end
2492
2494
  end
2493
2495
  end
2496
+
2497
+ context 'default_schema' do
2498
+ let(:default_schema_block) { nil }
2499
+ let(:declare_model) do
2500
+ -> do
2501
+ class Advert < active_record_base_class.constantize
2502
+ declare_schema do
2503
+ integer :price, limit: 8
2504
+ end
2505
+ end
2506
+ end
2507
+ end
2508
+
2509
+ before do
2510
+ DeclareSchema.default_schema(&default_schema_block)
2511
+ end
2512
+
2513
+ after do
2514
+ DeclareSchema.clear_default_schema
2515
+ end
2516
+
2517
+ context 'when unset' do
2518
+ it 'adds nothing' do
2519
+ declare_model.call
2520
+
2521
+ expect(Advert.field_specs.keys).to eq(['price'])
2522
+ end
2523
+ end
2524
+
2525
+ context 'when set to a block' do
2526
+ let(:default_schema_block) do
2527
+ -> do
2528
+ timestamps
2529
+ field :lock_version, :integer, default: 1
2530
+ end
2531
+ end
2532
+
2533
+ it 'adds the fields in that block' do
2534
+ declare_model.call
2535
+
2536
+ expect(Advert.field_specs.keys).to eq(['price', 'created_at', 'updated_at', 'lock_version'])
2537
+ end
2538
+
2539
+ context 'and the model sets default_schema: false' do
2540
+ before do
2541
+ class Advert < active_record_base_class.constantize
2542
+ declare_schema default_schema: false do
2543
+ integer :price, limit: 8
2544
+ end
2545
+ end
2546
+ end
2547
+
2548
+ it 'does not add the default schema fields' do
2549
+ expect(Advert.field_specs.keys).to eq(['price'])
2550
+ end
2551
+ end
2552
+
2553
+ context 'and the block has redundant fields' do
2554
+ before do
2555
+ class Advert < active_record_base_class.constantize
2556
+ declare_schema do
2557
+ integer :price, limit: 8
2558
+ timestamps
2559
+ end
2560
+ end
2561
+ end
2562
+
2563
+ it 'is a no-op' do
2564
+ expect(Advert.field_specs.keys).to eq(['price', 'created_at', 'updated_at', 'lock_version'])
2565
+ end
2566
+ end
2567
+ end
2568
+ end
2494
2569
  end
2495
2570
  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.12.1
4
+ version: 0.13.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: 2021-05-10 00:00:00.000000000 Z
11
+ date: 2021-08-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails