declare_schema 0.12.1 → 0.13.0.pre.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/Gemfile.lock +1 -1
- data/README.md +21 -1
- data/lib/declare_schema.rb +6 -1
- data/lib/declare_schema/dsl.rb +6 -3
- data/lib/declare_schema/extensions/active_record/fields_declaration.rb +4 -5
- data/lib/declare_schema/model.rb +8 -11
- data/lib/declare_schema/version.rb +1 -1
- data/spec/lib/declare_schema/migration_generator_spec.rb +75 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fef97a705f29e37659ade688c2c64b30f0df1eabc76ab47ceb1272f2eec10845
|
4
|
+
data.tar.gz: 3602d932b5bae67c6cfd367750e399d44ab65afa4fd19c4923ff5e447b79ca40
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 770d10d283e399d3ca37537e7ed4c68d0dde1a6ba28e033148ed7ed86d0e0833256d0b2446688538ef34f39cac9c1fe535914824d4bf7e6de17d11554b876ec2
|
7
|
+
data.tar.gz: 134add2558ab3374d741e04839913257eb48e8b98d4b80a3bc3c35927369032fd9549d9a1dd2768c702386f44b4265c0379475cd01f8097288cad53b479a4ee9
|
data/CHANGELOG.md
CHANGED
@@ -4,6 +4,10 @@ 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.0] - Unreleased
|
8
|
+
### Added
|
9
|
+
- Added support for `default_schema =` to apply a default schema to every model, unless disabld for that model with `declare_schema: false`.
|
10
|
+
|
7
11
|
## [0.12.1] - 2021-05-10
|
8
12
|
### Fixed
|
9
13
|
- 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 +183,7 @@ using the appropriate Rails configuration attributes.
|
|
179
183
|
### Added
|
180
184
|
- Initial version from https://github.com/Invoca/hobo_fields v4.1.0.
|
181
185
|
|
186
|
+
[0.13.0]: https://github.com/Invoca/declare_schema/compare/v0.12.1...v0.13.0
|
182
187
|
[0.12.1]: https://github.com/Invoca/declare_schema/compare/v0.12.0...v0.12.1
|
183
188
|
[0.12.0]: https://github.com/Invoca/declare_schema/compare/v0.11.1...v0.12.0
|
184
189
|
[0.11.1]: https://github.com/Invoca/declare_schema/compare/v0.11.0...v0.11.1
|
data/Gemfile.lock
CHANGED
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
|
-
|
65
|
+
For example:
|
66
66
|
|
67
67
|
```ruby
|
68
68
|
DeclareSchema::Migration::Migrator.before_generating_migration do
|
@@ -70,6 +70,26 @@ 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
|
+
|
73
93
|
### Global Configuration
|
74
94
|
Configurations can be set at the global level to customize default declaration for the following values:
|
75
95
|
|
data/lib/declare_schema.rb
CHANGED
@@ -37,7 +37,7 @@ module DeclareSchema
|
|
37
37
|
|
38
38
|
class << self
|
39
39
|
attr_reader :default_charset, :default_collation, :default_text_limit, :default_string_limit, :default_null,
|
40
|
-
:default_generate_foreign_keys, :default_generate_indexing, :db_migrate_command
|
40
|
+
:default_generate_foreign_keys, :default_generate_indexing, :default_schema, :db_migrate_command
|
41
41
|
|
42
42
|
def to_class(type)
|
43
43
|
case type
|
@@ -85,6 +85,11 @@ module DeclareSchema
|
|
85
85
|
@default_generate_indexing = generate_indexing
|
86
86
|
end
|
87
87
|
|
88
|
+
def default_schema=(default_schema)
|
89
|
+
default_schema.nil? || default_schema.respond_to?(:call) or raise "default_schema must be nil or a block that responds to call"
|
90
|
+
@default_schema = default_schema
|
91
|
+
end
|
92
|
+
|
88
93
|
def db_migrate_command=(db_migrate_command)
|
89
94
|
db_migrate_command.is_a?(String) or raise ArgumentError, "db_migrate_command must be a string (got #{db_migrate_command.inspect})"
|
90
95
|
@db_migrate_command = db_migrate_command
|
data/lib/declare_schema/dsl.rb
CHANGED
@@ -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
|
-
|
36
|
-
|
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(
|
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
|
-
|
41
|
-
|
42
|
-
|
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
|
data/lib/declare_schema/model.rb
CHANGED
@@ -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
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
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
|
-
|
43
|
+
index(ic)
|
48
44
|
end
|
49
|
-
|
45
|
+
super
|
46
|
+
end
|
50
47
|
end
|
51
48
|
end
|
52
49
|
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.default_schema = nil
|
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.
|
4
|
+
version: 0.13.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: 2021-
|
11
|
+
date: 2021-06-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|